generate-markdown.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * Generates a benchmark report in Markdown
  4. *
  5. * @created 27.04.2024
  6. * @author smiley <smiley@chillerlan.net>
  7. * @copyright 2024 smiley
  8. * @license MIT
  9. *
  10. * @phan-file-suppress PhanTypeArraySuspiciousNullable
  11. */
  12. declare(strict_types=1);
  13. namespace chillerlan\QRCodeBenchmark;
  14. use RuntimeException;
  15. use function array_keys;
  16. use function file_exists;
  17. use function file_get_contents;
  18. use function file_put_contents;
  19. use function implode;
  20. use function is_bool;
  21. use function is_dir;
  22. use function json_decode;
  23. use function mkdir;
  24. use function sprintf;
  25. use function strtolower;
  26. require_once __DIR__.'/parse-common.php';
  27. if(!file_exists(FILE.'.json')){
  28. throw new RuntimeException('invalid benchmark report');
  29. }
  30. $json = json_decode(file_get_contents(FILE.'.json'), true);
  31. $markdown = [];
  32. // General information/overview
  33. $env = $json['env'];
  34. $suite = $json['suite'];
  35. $markdown['Readme'][] = "# Benchmark results\n";
  36. $markdown['Readme'][] = "## Environment\n";
  37. $markdown['Readme'][] = '| Name | Value |';
  38. $markdown['Readme'][] = '|------|-------|';
  39. $markdown['Readme'][] = sprintf('| date | %s %s |', $suite['date'], $suite['time']);
  40. $markdown['Readme'][] = sprintf('| environment | %s %s, %s |', $env['uname_os'], $env['uname_version'], $env['uname_machine']);
  41. $markdown['Readme'][] = sprintf('| tag | %s |', $suite['tag']);
  42. foreach(['php_version', 'php_ini', 'php_extensions', 'php_xdebug', 'opcache_extension_loaded', 'opcache_enabled'] as $field){
  43. // prettify the boolean values
  44. if(is_bool($env[$field])){
  45. $env[$field] = ($env[$field]) ? '✓' : '✗';
  46. }
  47. $markdown['Readme'][] = sprintf('| %s | %s |', $field, $env[$field]);
  48. }
  49. // list indiviidual reports
  50. $markdown['Readme'][] = '';
  51. $markdown['Readme'][] = '## Reports';
  52. $markdown['Readme'][] = '';
  53. $list = [];
  54. foreach(array_keys($json['benchmark']) as $benchmark){
  55. // add a file & header
  56. $markdown[$benchmark][] = sprintf("# %s\n", $benchmark);
  57. $markdown[$benchmark][] = sprintf("`%s`\n", $json['benchmark'][$benchmark]['class']);
  58. foreach(array_keys($json['benchmark'][$benchmark]['subjects']) as $subject){
  59. // list item
  60. $list[$benchmark][] = $subject;
  61. $subj = $json['benchmark'][$benchmark]['subjects'][$subject];
  62. $markdown[$benchmark][] = sprintf("## %s\n", $subject);
  63. $markdown[$benchmark][] = sprintf("**Revs: %s, Iterations: %s**\n", $subj['revs'], $subj['iterations']);
  64. $markdown[$benchmark][] = parseVariants($subj['variants']);
  65. $markdown[$benchmark][] = '';
  66. }
  67. $markdown[$benchmark][] = '[back to overview](./Benchmark.md)';
  68. }
  69. // create overview list
  70. foreach($list as $benchmark => $subjects){
  71. // list item
  72. $markdown['Readme'][] = sprintf('- [%1$s](./%1$s.md)', $benchmark);
  73. foreach($subjects as $subject){
  74. // list sub-item
  75. $markdown['Readme'][] = sprintf(' - [%2$s](./%1$s.md#%3$s)', $benchmark, $subject, strtolower($subject));
  76. }
  77. }
  78. if(!is_dir(BUILDDIR.'/markdown/')){
  79. mkdir(BUILDDIR.'/markdown/');
  80. }
  81. foreach($markdown as $file => $content){
  82. file_put_contents(BUILDDIR.'/markdown/'.$file.'.md', implode("\n", $content));
  83. }