generate-html.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * Generates a benchmark report in HTML
  4. *
  5. * @created 30.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 copy;
  17. use function file_exists;
  18. use function file_get_contents;
  19. use function file_put_contents;
  20. use function htmlspecialchars;
  21. use function implode;
  22. use function is_bool;
  23. use function is_dir;
  24. use function json_decode;
  25. use function mkdir;
  26. use function sprintf;
  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. $htmlHead = '<!DOCTYPE html>
  33. <html lang="en">
  34. <head>
  35. <meta charset="UTF-8">
  36. <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  37. <link rel="stylesheet" href="./benchmark.css">
  38. <title>%s results</title>
  39. </head>
  40. <body>';
  41. $htmlFoot = '</body>
  42. </html>';
  43. $html = [];
  44. // General information/overview
  45. $env = $json['env'];
  46. $suite = $json['suite'];
  47. $html['index'][] = sprintf($htmlHead, 'Benchmark');
  48. $html['index'][] = '<h1>Benchmark results</h1>';
  49. $html['index'][] = '<h2>Environment</h2>';
  50. $html['index'][] = '<table><thead>';
  51. $html['index'][] = '<tr><th>Name</th><th>Value</th></tr>';
  52. $html['index'][] = '</thead><tbody>';
  53. $html['index'][] = sprintf('<tr><td>date</td><td style="text-align: left;">%s %s</td></tr>', $suite['date'], $suite['time']);
  54. $html['index'][] = sprintf('<tr><td>environment</td><td style="text-align: left;">%s %s, %s</td></tr>', $env['uname_os'], $env['uname_version'], $env['uname_machine']);
  55. $html['index'][] = sprintf('<tr><td>tag</td><td style="text-align: left;">%s</td></tr>', htmlspecialchars($suite['tag']));
  56. foreach(['php_version', 'php_ini', 'php_extensions', 'php_xdebug', 'opcache_extension_loaded', 'opcache_enabled'] as $field){
  57. // prettify the boolean values
  58. if(is_bool($env[$field])){
  59. $env[$field] = ($env[$field]) ? '✓' : '✗';
  60. }
  61. $html['index'][] = sprintf('<tr><td>%s</td><td style="text-align: left;">%s</td></tr>', $field, $env[$field]);
  62. }
  63. $html['index'][] = '</tbody></table>';
  64. // list indiviidual reports
  65. $html['index'][] = '<h2>Reports</h2>';
  66. $list = [];
  67. foreach(array_keys($json['benchmark']) as $benchmark){
  68. // add a file & header
  69. $html[$benchmark][] = sprintf($htmlHead, $benchmark);
  70. $html[$benchmark][] = sprintf('<h1>%s</h1>', $benchmark);
  71. $html[$benchmark][] = sprintf('<code>%s</code>', $json['benchmark'][$benchmark]['class']);
  72. foreach(array_keys($json['benchmark'][$benchmark]['subjects']) as $subject){
  73. // list item
  74. $list[$benchmark][] = $subject;
  75. $subj = $json['benchmark'][$benchmark]['subjects'][$subject];
  76. $html[$benchmark][] = sprintf('<h2 id="%1$s">%1$s</h2>', $subject);
  77. $html[$benchmark][] = sprintf('<div>Revs: %s, Iterations: %s</div>', $subj['revs'], $subj['iterations']);
  78. $html[$benchmark][] = parseVariants($subj['variants']);
  79. }
  80. // close document
  81. $html[$benchmark][] = '<a class="return" href="./index.html">back to overview</a>';
  82. $html[$benchmark][] = $htmlFoot;
  83. }
  84. // create overview list
  85. $html['index'][] = '<ul>';
  86. foreach($list as $benchmark => $subjects){
  87. // list item
  88. $html['index'][] = sprintf('<li><a href="./%1$s.html">%1$s</a><ul>', $benchmark);
  89. foreach($subjects as $subject){
  90. // list sub-item
  91. $html['index'][] = sprintf('<li><a href="./%1$s.html#%2$s">%2$s</a></li>', $benchmark, $subject);
  92. }
  93. $html['index'][] = '</ul></li>';
  94. }
  95. $html['index'][] = '</ul>';
  96. // close document
  97. $html['index'][] = $htmlFoot;
  98. if(!is_dir(BUILDDIR.'/html/')){
  99. mkdir(BUILDDIR.'/html/');
  100. }
  101. copy('./benchmark.css', BUILDDIR.'/html/benchmark.css');
  102. foreach($html as $file => $content){
  103. file_put_contents(BUILDDIR.'/html/'.$file.'.html', implode("\n", $content));
  104. }