parse-result.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * Parses the CSV result into more handy JSON
  4. *
  5. * @created 26.04.2024
  6. * @author smiley <smiley@chillerlan.net>
  7. * @copyright 2024 smiley
  8. * @license MIT
  9. */
  10. declare(strict_types=1);
  11. namespace chillerlan\QRCodeBenchmark;
  12. use function array_combine;
  13. use function array_map;
  14. use function array_shift;
  15. use function explode;
  16. use function file_get_contents;
  17. use function file_put_contents;
  18. use function floatval;
  19. use function intval;
  20. use function json_encode;
  21. use function str_replace;
  22. use function str_starts_with;
  23. use function trim;
  24. use const JSON_PRESERVE_ZERO_FRACTION;
  25. use const JSON_PRETTY_PRINT;
  26. require_once __DIR__.'/parse-common.php';
  27. const SEPARATOR = '|';
  28. const BOOLEANS = ['has_baseline', 'env_php_xdebug', 'env_opcache_extension_loaded', 'env_opcache_enabled'];
  29. const INTEGERS = [
  30. 'variant_index', 'variant_revs', 'variant_iterations', 'iteration_index', 'result_time_net',
  31. 'result_time_revs', 'result_mem_peak', 'result_mem_real', 'result_mem_final',
  32. ];
  33. const FLOATS = [
  34. 'env_sampler_nothing', 'env_sampler_md5', 'env_sampler_file_rw', 'result_time_avg',
  35. 'result_comp_z_value', 'result_comp_deviation',
  36. ];
  37. const ARRAYS = ['subject_groups', 'variant_params'];
  38. function parseLine(string $line):array{
  39. return array_map(fn(string $str):string => trim($str, "\ \n\r\t\v\0\""), explode(SEPARATOR, $line));
  40. }
  41. $csv = explode("\n", trim(file_get_contents(FILE.'.csv')));
  42. $header = parseLine(array_shift($csv));
  43. $parsed = array_map(fn(string $line):array => array_combine($header, parseLine($line)), $csv);
  44. $json = ['env' => [], 'suite' => [], 'benchmark' => []];
  45. foreach($parsed as $i => $result){
  46. // booleans
  47. foreach(BOOLEANS as $bool){
  48. $result[$bool] = (bool)$result[$bool];
  49. }
  50. // integers
  51. foreach(INTEGERS as $int){
  52. $result[$int] = intval($result[$int]);
  53. }
  54. // floats
  55. foreach(FLOATS as $float){
  56. $result[$float] = floatval($result[$float]);
  57. }
  58. // arrays
  59. foreach(ARRAYS as $array){
  60. $val = trim($result[$array], '"[]');
  61. if($val === ''){
  62. $result[$array] = [];
  63. continue;
  64. }
  65. $val = array_map('trim', explode(',', $val));
  66. $result[$array] = $val;
  67. }
  68. // rename some things to avoid bloat
  69. $result['subject_revs'] = $result['variant_revs'];
  70. $result['subject_iterations'] = $result['variant_iterations'];
  71. $result['result_index'] = $result['iteration_index'];
  72. unset($result['variant_revs'], $result['variant_iterations'], $result['iteration_index'], $result['result_time_revs']);
  73. // add the class name
  74. $json['benchmark'][$result['benchmark_name']]['class'] = $result['benchmark_class'];
  75. foreach($result as $k => $v){
  76. // the environment info is only needed once
  77. if(str_starts_with($k, 'env_')){
  78. if($i === 0){
  79. $json['env'][str_replace('env_', '', $k)] = $v;
  80. }
  81. continue;
  82. }
  83. // suite info is needed only once
  84. if(str_starts_with($k, 'suite_')){
  85. if($i === 0){
  86. $json['suite'][str_replace('suite_', '', $k)] = $v;
  87. }
  88. continue;
  89. }
  90. // test subject info once per test subject
  91. if(str_starts_with($k, 'subject_')){
  92. // skip the name as it is the key
  93. if($k === 'subject_name'){
  94. continue;
  95. }
  96. // phpcs:ignore
  97. $json['benchmark'][$result['benchmark_name']]['subjects'][$result['subject_name']][str_replace('subject_', '', $k)] = $v;
  98. }
  99. // add variants
  100. if(str_starts_with($k, 'variant_')){
  101. // phpcs:ignore
  102. $json['benchmark'][$result['benchmark_name']]['subjects'][$result['subject_name']]['variants'][$result['variant_index']][str_replace('variant_', '', $k)] = $v;
  103. }
  104. // add benchmark results per variant
  105. if(str_starts_with($k, 'result_')){
  106. // phpcs:ignore
  107. $json['benchmark'][$result['benchmark_name']]['subjects'][$result['subject_name']]['variants'][$result['variant_index']]['results'][$result['result_index']][str_replace('result_', '', $k)] = $v;
  108. }
  109. }
  110. }
  111. file_put_contents(FILE.'.json', json_encode($json, (JSON_PRESERVE_ZERO_FRACTION | JSON_PRETTY_PRINT)));