parse-result.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. function parseLine(string $line):array{
  29. return array_map(fn(string $str):string => trim($str, "\ \n\r\t\v\0\""), explode(SEPARATOR, $line));
  30. }
  31. $csv = explode("\n", trim(file_get_contents(FILE.'.csv')));
  32. $header = parseLine(array_shift($csv));
  33. $parsed = array_map(fn(string $line):array => array_combine($header, parseLine($line)), $csv);
  34. $json = ['env' => [], 'suite' => [], 'benchmark' => []];
  35. foreach($parsed as $i => $result){
  36. // booleans
  37. foreach(['has_baseline', 'env_php_xdebug', 'env_opcache_extension_loaded', 'env_opcache_enabled'] as $bool){
  38. $result[$bool] = (bool)$result[$bool];
  39. }
  40. // integers
  41. foreach(['variant_index', 'variant_revs', 'variant_iterations', 'iteration_index', 'result_time_net', 'result_time_revs', 'result_mem_peak', 'result_mem_real', 'result_mem_final'] as $int){
  42. $result[$int] = intval($result[$int]);
  43. }
  44. // floats
  45. foreach(['env_sampler_nothing', 'env_sampler_md5', 'env_sampler_file_rw', 'result_time_avg', 'result_comp_z_value', 'result_comp_deviation'] as $float){
  46. $result[$float] = floatval($result[$float]);
  47. }
  48. // arrays
  49. foreach(['subject_groups', 'variant_params'] as $array){
  50. $val = trim($result[$array], '"[]');
  51. if($val === ''){
  52. $result[$array] = [];
  53. continue;
  54. }
  55. $val = array_map('trim', explode(',', $val));
  56. $result[$array] = $val;
  57. }
  58. // rename some things to avoid bloat
  59. $result['subject_revs'] = $result['variant_revs'];
  60. $result['subject_iterations'] = $result['variant_iterations'];
  61. $result['result_index'] = $result['iteration_index'];
  62. unset($result['variant_revs'], $result['variant_iterations'], $result['iteration_index'], $result['result_time_revs']);
  63. // add the class name
  64. $json['benchmark'][$result['benchmark_name']]['class'] = $result['benchmark_class'];
  65. foreach($result as $k => $v){
  66. // the environment info is only needed once
  67. if(str_starts_with($k, 'env_')){
  68. if($i === 0){
  69. $json['env'][str_replace('env_', '', $k)] = $v;
  70. }
  71. continue;
  72. }
  73. // suite info is needed only once
  74. if(str_starts_with($k, 'suite_')){
  75. if($i === 0){
  76. $json['suite'][str_replace('suite_', '', $k)] = $v;
  77. }
  78. continue;
  79. }
  80. // test subject info once per test subject
  81. if(str_starts_with($k, 'subject_')){
  82. // skip the name as it is the key
  83. if($k === 'subject_name'){
  84. continue;
  85. }
  86. $json['benchmark'][$result['benchmark_name']]['subjects'][$result['subject_name']][str_replace('subject_', '', $k)] = $v;
  87. }
  88. // add variants
  89. if(str_starts_with($k, 'variant_')){
  90. $json['benchmark'][$result['benchmark_name']]['subjects'][$result['subject_name']]['variants'][$result['variant_index']][str_replace('variant_', '', $k)] = $v;
  91. }
  92. // add benchmark results per variant
  93. if(str_starts_with($k, 'result_')){
  94. $json['benchmark'][$result['benchmark_name']]['subjects'][$result['subject_name']]['variants'][$result['variant_index']]['results'][$result['result_index']][str_replace('result_', '', $k)] = $v;
  95. }
  96. }
  97. }
  98. file_put_contents(FILE.'.json', json_encode($json, (JSON_PRESERVE_ZERO_FRACTION|JSON_PRETTY_PRINT)));