Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

162 lines
3.6KB

  1. <?php
  2. require_once '../library/HTMLPurifier.auto.php';
  3. @include_once '../test-settings.php';
  4. // PEAR
  5. require_once 'Benchmark/Timer.php'; // to do the timing
  6. require_once 'Text/Password.php'; // for generating random input
  7. $LEXERS = array();
  8. $RUNS = isset($GLOBALS['HTMLPurifierTest']['Runs'])
  9. ? $GLOBALS['HTMLPurifierTest']['Runs'] : 2;
  10. require_once 'HTMLPurifier/Lexer/DirectLex.php';
  11. $LEXERS['DirectLex'] = new HTMLPurifier_Lexer_DirectLex();
  12. if (version_compare(PHP_VERSION, '5', '>=')) {
  13. require_once 'HTMLPurifier/Lexer/DOMLex.php';
  14. $LEXERS['DOMLex'] = new HTMLPurifier_Lexer_DOMLex();
  15. }
  16. // custom class to aid unit testing
  17. class RowTimer extends Benchmark_Timer
  18. {
  19. public $name;
  20. public function RowTimer($name, $auto = false)
  21. {
  22. $this->name = htmlentities($name);
  23. $this->Benchmark_Timer($auto);
  24. }
  25. public function getOutput()
  26. {
  27. $total = $this->TimeElapsed();
  28. $result = $this->getProfiling();
  29. $dashes = '';
  30. $out = '<tr>';
  31. $out .= "<td>{$this->name}</td>";
  32. $standard = false;
  33. foreach ($result as $k => $v) {
  34. if ($v['name'] == 'Start' || $v['name'] == 'Stop') continue;
  35. //$perc = (($v['diff'] * 100) / $total);
  36. //$tperc = (($v['total'] * 100) / $total);
  37. //$out .= '<td align="right">' . $v['diff'] . '</td>';
  38. if ($standard == false) $standard = $v['diff'];
  39. $perc = $v['diff'] * 100 / $standard;
  40. $bad_run = ($v['diff'] < 0);
  41. $out .= '<td align="right"'.
  42. ($bad_run ? ' style="color:#AAA;"' : '').
  43. '>' . number_format($perc, 2, '.', '') .
  44. '%</td><td>'.number_format($v['diff'],4,'.','').'</td>';
  45. }
  46. $out .= '</tr>';
  47. return $out;
  48. }
  49. }
  50. function print_lexers()
  51. {
  52. global $LEXERS;
  53. $first = true;
  54. foreach ($LEXERS as $key => $value) {
  55. if (!$first) echo ' / ';
  56. echo htmlspecialchars($key);
  57. $first = false;
  58. }
  59. }
  60. function do_benchmark($name, $document)
  61. {
  62. global $LEXERS, $RUNS;
  63. $config = HTMLPurifier_Config::createDefault();
  64. $context = new HTMLPurifier_Context();
  65. $timer = new RowTimer($name);
  66. $timer->start();
  67. foreach($LEXERS as $key => $lexer) {
  68. for ($i=0; $i<$RUNS; $i++) $tokens = $lexer->tokenizeHTML($document, $config, $context);
  69. $timer->setMarker($key);
  70. }
  71. $timer->stop();
  72. $timer->display();
  73. }
  74. ?>
  75. <html>
  76. <head>
  77. <title>Benchmark: <?php print_lexers(); ?></title>
  78. </head>
  79. <body>
  80. <h1>Benchmark: <?php print_lexers(); ?></h1>
  81. <table border="1">
  82. <tr><th>Case</th><?php
  83. foreach ($LEXERS as $key => $value) {
  84. echo '<th colspan="2">' . htmlspecialchars($key) . '</th>';
  85. }
  86. ?></tr>
  87. <?php
  88. // ************************************************************************** //
  89. // sample of html pages
  90. $dir = 'samples/Lexer';
  91. $dh = opendir($dir);
  92. while (false !== ($filename = readdir($dh))) {
  93. if (strpos($filename, '.html') !== strlen($filename) - 5) continue;
  94. $document = file_get_contents($dir . '/' . $filename);
  95. do_benchmark("File: $filename", $document);
  96. }
  97. // crashers, caused infinite loops before
  98. $snippets = array();
  99. $snippets[] = '<a href="foo>';
  100. $snippets[] = '<a "=>';
  101. foreach ($snippets as $snippet) {
  102. do_benchmark($snippet, $snippet);
  103. }
  104. // random input
  105. $random = Text_Password::create(80, 'unpronounceable', 'qwerty <>="\'');
  106. do_benchmark('Random input', $random);
  107. ?></table>
  108. <?php
  109. echo '<div>Random input was: ' .
  110. '<span colspan="4" style="font-family:monospace;">' .
  111. htmlspecialchars($random) . '</span></div>';
  112. ?>
  113. </body></html>
  114. <?php
  115. // vim: et sw=4 sts=4