[Phorum] Minor enhancements: add cache purge support and give a friendly HTML is...
[htmlpurifier.git] / benchmarks / Lexer.php
blob6d9cc5b259fc9af06a4a540172856df64264a212
1 <?php
3 // emulates inserting a dir called HTMLPurifier into your class dir
4 set_include_path(get_include_path() . PATH_SEPARATOR . '../library/');
6 @include_once '../test-settings.php';
8 require_once 'HTMLPurifier/ConfigSchema.php';
9 require_once 'HTMLPurifier/Config.php';
10 require_once 'HTMLPurifier/Context.php';
12 $LEXERS = array();
13 $RUNS = isset($GLOBALS['HTMLPurifierTest']['Runs'])
14 ? $GLOBALS['HTMLPurifierTest']['Runs'] : 2;
16 require_once 'HTMLPurifier/Lexer/DirectLex.php';
17 $LEXERS['DirectLex'] = new HTMLPurifier_Lexer_DirectLex();
19 if (!empty($GLOBALS['HTMLPurifierTest']['PEAR'])) {
20 require_once 'HTMLPurifier/Lexer/PEARSax3.php';
21 $LEXERS['PEARSax3'] = new HTMLPurifier_Lexer_PEARSax3();
22 } else {
23 exit('PEAR required to perform benchmark.');
26 if (version_compare(PHP_VERSION, '5', '>=')) {
27 require_once 'HTMLPurifier/Lexer/DOMLex.php';
28 $LEXERS['DOMLex'] = new HTMLPurifier_Lexer_DOMLex();
31 // PEAR
32 require_once 'Benchmark/Timer.php'; // to do the timing
33 require_once 'Text/Password.php'; // for generating random input
35 // custom class to aid unit testing
36 class RowTimer extends Benchmark_Timer
39 var $name;
41 function RowTimer($name, $auto = false) {
42 $this->name = htmlentities($name);
43 $this->Benchmark_Timer($auto);
46 function getOutput() {
48 $total = $this->TimeElapsed();
49 $result = $this->getProfiling();
50 $dashes = '';
52 $out = '<tr>';
54 $out .= "<td>{$this->name}</td>";
56 $standard = false;
58 foreach ($result as $k => $v) {
59 if ($v['name'] == 'Start' || $v['name'] == 'Stop') continue;
61 //$perc = (($v['diff'] * 100) / $total);
62 //$tperc = (($v['total'] * 100) / $total);
64 //$out .= '<td align="right">' . $v['diff'] . '</td>';
66 if ($standard == false) $standard = $v['diff'];
68 $perc = $v['diff'] * 100 / $standard;
69 $bad_run = ($v['diff'] < 0);
71 $out .= '<td align="right"'.
72 ($bad_run ? ' style="color:#AAA;"' : '').
73 '>' . number_format($perc, 2, '.', '') .
74 '%</td><td>'.number_format($v['diff'],4,'.','').'</td>';
78 $out .= '</tr>';
80 return $out;
84 function print_lexers() {
85 global $LEXERS;
86 $first = true;
87 foreach ($LEXERS as $key => $value) {
88 if (!$first) echo ' / ';
89 echo htmlspecialchars($key);
90 $first = false;
94 function do_benchmark($name, $document) {
95 global $LEXERS, $RUNS;
97 $config = HTMLPurifier_Config::createDefault();
98 $context = new HTMLPurifier_Context();
100 $timer = new RowTimer($name);
101 $timer->start();
103 foreach($LEXERS as $key => $lexer) {
104 for ($i=0; $i<$RUNS; $i++) $tokens = $lexer->tokenizeHTML($document, $config, $context);
105 $timer->setMarker($key);
108 $timer->stop();
109 $timer->display();
113 <html>
114 <head>
115 <title>Benchmark: <?php print_lexers(); ?></title>
116 </head>
117 <body>
118 <h1>Benchmark: <?php print_lexers(); ?></h1>
119 <table border="1">
120 <tr><th>Case</th><?php
121 foreach ($LEXERS as $key => $value) {
122 echo '<th colspan="2">' . htmlspecialchars($key) . '</th>';
124 ?></tr>
125 <?php
127 // ************************************************************************** //
129 // sample of html pages
131 $dir = 'samples/Lexer';
132 $dh = opendir($dir);
133 while (false !== ($filename = readdir($dh))) {
135 if (strpos($filename, '.html') !== strlen($filename) - 5) continue;
136 $document = file_get_contents($dir . '/' . $filename);
137 do_benchmark("File: $filename", $document);
141 // crashers, caused infinite loops before
143 $snippets = array();
144 $snippets[] = '<a href="foo>';
145 $snippets[] = '<a "=>';
147 foreach ($snippets as $snippet) {
148 do_benchmark($snippet, $snippet);
151 // random input
153 $random = Text_Password::create(80, 'unpronounceable', 'qwerty <>="\'');
155 do_benchmark('Random input', $random);
157 ?></table>
159 <?php
161 echo '<div>Random input was: ' .
162 '<span colspan="4" style="font-family:monospace;">' .
163 htmlspecialchars($random) . '</span></div>';
168 </body></html>