Prepended constants with C_ to avoid reserved words as per https://www.php.net/manual...
[htmlpurifier/darkodev.git] / benchmarks / Lexer.php
blobc44e0a5fe7a71e4fa4845a55f75f6b23046cc37c
1 <?php
3 require_once '../library/HTMLPurifier.auto.php';
4 @include_once '../test-settings.php';
6 // PEAR
7 require_once 'Benchmark/Timer.php'; // to do the timing
8 require_once 'Text/Password.php'; // for generating random input
10 $LEXERS = array();
11 $RUNS = isset($GLOBALS['HTMLPurifierTest']['Runs'])
12 ? $GLOBALS['HTMLPurifierTest']['Runs'] : 2;
14 require_once 'HTMLPurifier/Lexer/DirectLex.php';
15 $LEXERS['DirectLex'] = new HTMLPurifier_Lexer_DirectLex();
17 if (version_compare(PHP_VERSION, '5', '>=')) {
18 require_once 'HTMLPurifier/Lexer/DOMLex.php';
19 $LEXERS['DOMLex'] = new HTMLPurifier_Lexer_DOMLex();
22 // custom class to aid unit testing
23 class RowTimer extends Benchmark_Timer
26 public $name;
28 public function __construct($name, $auto = false)
30 $this->name = htmlentities($name);
31 $this->Benchmark_Timer($auto);
34 public function getOutput()
36 $total = $this->TimeElapsed();
37 $result = $this->getProfiling();
38 $dashes = '';
40 $out = '<tr>';
42 $out .= "<td>{$this->name}</td>";
44 $standard = false;
46 foreach ($result as $k => $v) {
47 if ($v['name'] == 'Start' || $v['name'] == 'Stop') continue;
49 //$perc = (($v['diff'] * 100) / $total);
50 //$tperc = (($v['total'] * 100) / $total);
52 //$out .= '<td align="right">' . $v['diff'] . '</td>';
54 if ($standard == false) $standard = $v['diff'];
56 $perc = $v['diff'] * 100 / $standard;
57 $bad_run = ($v['diff'] < 0);
59 $out .= '<td align="right"'.
60 ($bad_run ? ' style="color:#AAA;"' : '').
61 '>' . number_format($perc, 2, '.', '') .
62 '%</td><td>'.number_format($v['diff'],4,'.','').'</td>';
66 $out .= '</tr>';
68 return $out;
72 function print_lexers()
74 global $LEXERS;
75 $first = true;
76 foreach ($LEXERS as $key => $value) {
77 if (!$first) echo ' / ';
78 echo htmlspecialchars($key);
79 $first = false;
83 function do_benchmark($name, $document)
85 global $LEXERS, $RUNS;
87 $config = HTMLPurifier_Config::createDefault();
88 $context = new HTMLPurifier_Context();
90 $timer = new RowTimer($name);
91 $timer->start();
93 foreach($LEXERS as $key => $lexer) {
94 for ($i=0; $i<$RUNS; $i++) $tokens = $lexer->tokenizeHTML($document, $config, $context);
95 $timer->setMarker($key);
98 $timer->stop();
99 $timer->display();
103 <html>
104 <head>
105 <title>Benchmark: <?php print_lexers(); ?></title>
106 </head>
107 <body>
108 <h1>Benchmark: <?php print_lexers(); ?></h1>
109 <table border="1">
110 <tr><th>Case</th><?php
111 foreach ($LEXERS as $key => $value) {
112 echo '<th colspan="2">' . htmlspecialchars($key) . '</th>';
114 ?></tr>
115 <?php
117 // ************************************************************************** //
119 // sample of html pages
121 $dir = 'samples/Lexer';
122 $dh = opendir($dir);
123 while (false !== ($filename = readdir($dh))) {
125 if (strpos($filename, '.html') !== strlen($filename) - 5) continue;
126 $document = file_get_contents($dir . '/' . $filename);
127 do_benchmark("File: $filename", $document);
131 // crashers, caused infinite loops before
133 $snippets = array();
134 $snippets[] = '<a href="foo>';
135 $snippets[] = '<a "=>';
137 foreach ($snippets as $snippet) {
138 do_benchmark($snippet, $snippet);
141 // random input
143 $random = Text_Password::create(80, 'unpronounceable', 'qwerty <>="\'');
145 do_benchmark('Random input', $random);
147 ?></table>
149 <?php
151 echo '<div>Random input was: ' .
152 '<span colspan="4" style="font-family:monospace;">' .
153 htmlspecialchars($random) . '</span></div>';
158 </body></html>
159 <?php
161 // vim: et sw=4 sts=4