[1.1.1]
[htmlpurifier.git] / benchmarks / Lexer.php
blob45b2fcc50a9acc95e577aa5ca8cf221baebb8d40
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;
69 $out .= '<td align="right">' . number_format($perc, 2, '.', '') .
70 '%</td><td>'.number_format($v['diff'],4,'.','').'</td>';
74 $out .= '</tr>';
76 return $out;
80 function print_lexers() {
81 global $LEXERS;
82 $first = true;
83 foreach ($LEXERS as $key => $value) {
84 if (!$first) echo ' / ';
85 echo htmlspecialchars($key);
86 $first = false;
90 function do_benchmark($name, $document) {
91 global $LEXERS, $RUNS;
93 $timer = new RowTimer($name);
94 $timer->start();
96 foreach($LEXERS as $key => $lexer) {
97 for ($i=0; $i<$RUNS; $i++) $tokens = $lexer->tokenizeHTML($document);
98 $timer->setMarker($key);
101 $timer->stop();
102 $timer->display();
106 <html>
107 <head>
108 <title>Benchmark: <?php print_lexers(); ?></title>
109 </head>
110 <body>
111 <h1>Benchmark: <?php print_lexers(); ?></h1>
112 <table border="1">
113 <tr><th>Case</th><?php
114 foreach ($LEXERS as $key => $value) {
115 echo '<th colspan="2">' . htmlspecialchars($key) . '</th>';
117 ?></tr>
118 <?php
120 // ************************************************************************** //
122 // sample of html pages
124 $dir = 'samples/Lexer';
125 $dh = opendir($dir);
126 while (false !== ($filename = readdir($dh))) {
128 if (strpos($filename, '.html') !== strlen($filename) - 5) continue;
129 $document = file_get_contents($dir . '/' . $filename);
130 do_benchmark("File: $filename", $document);
134 // crashers, caused infinite loops before
136 $snippets = array();
137 $snippets[] = '<a href="foo>';
138 $snippets[] = '<a "=>';
140 foreach ($snippets as $snippet) {
141 do_benchmark($snippet, $snippet);
144 // random input
146 $random = Text_Password::create(80, 'unpronounceable', 'qwerty <>="\'');
148 do_benchmark('Random input', $random);
150 ?></table>
152 <?php
154 echo '<div>Random input was: ' .
155 '<span colspan="4" style="font-family:monospace;">' .
156 htmlspecialchars($random) . '</span></div>';
161 </body></html>