generate-markdown.php 3.1 KB

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