Fix typos in AttrDef/Lang.php involving lowercasing uppercased language strings.
[htmlpurifier.git] / benchmarks / Lexer.php
blob9e13b54b7d0241cdb6c32d91efed0264e2481304
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';
11 $LEXERS = array();
12 $RUNS = isset($GLOBALS['HTMLPurifierTest']['Runs'])
13 ? $GLOBALS['HTMLPurifierTest']['Runs'] : 2;
15 require_once 'HTMLPurifier/Lexer/DirectLex.php';
16 $LEXERS['DirectLex'] = new HTMLPurifier_Lexer_DirectLex();
18 if (!empty($GLOBALS['HTMLPurifierTest']['PEAR'])) {
19 require_once 'HTMLPurifier/Lexer/PEARSax3.php';
20 $LEXERS['PEARSax3'] = new HTMLPurifier_Lexer_PEARSax3();
21 } else {
22 exit('PEAR required to perform benchmark.');
25 if (version_compare(PHP_VERSION, '5', '>=')) {
26 require_once 'HTMLPurifier/Lexer/DOMLex.php';
27 $LEXERS['DOMLex'] = new HTMLPurifier_Lexer_DOMLex();
30 // PEAR
31 require_once 'Benchmark/Timer.php'; // to do the timing
32 require_once 'Text/Password.php'; // for generating random input
34 // custom class to aid unit testing
35 class RowTimer extends Benchmark_Timer
38 var $name;
40 function RowTimer($name, $auto = false) {
41 $this->name = htmlentities($name);
42 $this->Benchmark_Timer($auto);
45 function getOutput() {
47 $total = $this->TimeElapsed();
48 $result = $this->getProfiling();
49 $dashes = '';
51 $out = '<tr>';
53 $out .= "<td>{$this->name}</td>";
55 $standard = false;
57 foreach ($result as $k => $v) {
58 if ($v['name'] == 'Start' || $v['name'] == 'Stop') continue;
60 //$perc = (($v['diff'] * 100) / $total);
61 //$tperc = (($v['total'] * 100) / $total);
63 //$out .= '<td align="right">' . $v['diff'] . '</td>';
65 if ($standard == false) $standard = $v['diff'];
67 $perc = $v['diff'] * 100 / $standard;
68 $bad_run = ($v['diff'] < 0);
70 $out .= '<td align="right"'.
71 ($bad_run ? ' style="color:#AAA;"' : '').
72 '>' . number_format($perc, 2, '.', '') .
73 '%</td><td>'.number_format($v['diff'],4,'.','').'</td>';
77 $out .= '</tr>';
79 return $out;
83 function print_lexers() {
84 global $LEXERS;
85 $first = true;
86 foreach ($LEXERS as $key => $value) {
87 if (!$first) echo ' / ';
88 echo htmlspecialchars($key);
89 $first = false;
93 function do_benchmark($name, $document) {
94 global $LEXERS, $RUNS;
96 $timer = new RowTimer($name);
97 $timer->start();
99 foreach($LEXERS as $key => $lexer) {
100 for ($i=0; $i<$RUNS; $i++) $tokens = $lexer->tokenizeHTML($document);
101 $timer->setMarker($key);
104 $timer->stop();
105 $timer->display();
109 <html>
110 <head>
111 <title>Benchmark: <?php print_lexers(); ?></title>
112 </head>
113 <body>
114 <h1>Benchmark: <?php print_lexers(); ?></h1>
115 <table border="1">
116 <tr><th>Case</th><?php
117 foreach ($LEXERS as $key => $value) {
118 echo '<th colspan="2">' . htmlspecialchars($key) . '</th>';
120 ?></tr>
121 <?php
123 // ************************************************************************** //
125 // sample of html pages
127 $dir = 'samples/Lexer';
128 $dh = opendir($dir);
129 while (false !== ($filename = readdir($dh))) {
131 if (strpos($filename, '.html') !== strlen($filename) - 5) continue;
132 $document = file_get_contents($dir . '/' . $filename);
133 do_benchmark("File: $filename", $document);
137 // crashers, caused infinite loops before
139 $snippets = array();
140 $snippets[] = '<a href="foo>';
141 $snippets[] = '<a "=>';
143 foreach ($snippets as $snippet) {
144 do_benchmark($snippet, $snippet);
147 // random input
149 $random = Text_Password::create(80, 'unpronounceable', 'qwerty <>="\'');
151 do_benchmark('Random input', $random);
153 ?></table>
155 <?php
157 echo '<div>Random input was: ' .
158 '<span colspan="4" style="font-family:monospace;">' .
159 htmlspecialchars($random) . '</span></div>';
164 </body></html>