generate-markdown.php 3.1 KB

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