Release 3.3.0.
[htmlpurifier/darkodev.git] / benchmarks / Lexer.php
blob686ef63e68746636e6ba0a5db89622b283c9d59c
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 var $name;
28 function RowTimer($name, $auto = false) {
29 $this->name = htmlentities($name);
30 $this->Benchmark_Timer($auto);
33 function getOutput() {
35 $total = $this->TimeElapsed();
36 $result = $this->getProfiling();
37 $dashes = '';
39 $out = '<tr>';
41 $out .= "<td>{$this->name}</td>";
43 $standard = false;
45 foreach ($result as $k => $v) {
46 if ($v['name'] == 'Start' || $v['name'] == 'Stop') continue;
48 //$perc = (($v['diff'] * 100) / $total);
49 //$tperc = (($v['total'] * 100) / $total);
51 //$out .= '<td align="right">' . $v['diff'] . '</td>';
53 if ($standard == false) $standard = $v['diff'];
55 $perc = $v['diff'] * 100 / $standard;
56 $bad_run = ($v['diff'] < 0);
58 $out .= '<td align="right"'.
59 ($bad_run ? ' style="color:#AAA;"' : '').
60 '>' . number_format($perc, 2, '.', '') .
61 '%</td><td>'.number_format($v['diff'],4,'.','').'</td>';
65 $out .= '</tr>';
67 return $out;
71 function print_lexers() {
72 global $LEXERS;
73 $first = true;
74 foreach ($LEXERS as $key => $value) {
75 if (!$first) echo ' / ';
76 echo htmlspecialchars($key);
77 $first = false;
81 function do_benchmark($name, $document) {
82 global $LEXERS, $RUNS;
84 $config = HTMLPurifier_Config::createDefault();
85 $context = new HTMLPurifier_Context();
87 $timer = new RowTimer($name);
88 $timer->start();
90 foreach($LEXERS as $key => $lexer) {
91 for ($i=0; $i<$RUNS; $i++) $tokens = $lexer->tokenizeHTML($document, $config, $context);
92 $timer->setMarker($key);
95 $timer->stop();
96 $timer->display();
100 <html>
101 <head>
102 <title>Benchmark: <?php print_lexers(); ?></title>
103 </head>
104 <body>
105 <h1>Benchmark: <?php print_lexers(); ?></h1>
106 <table border="1">
107 <tr><th>Case</th><?php
108 foreach ($LEXERS as $key => $value) {
109 echo '<th colspan="2">' . htmlspecialchars($key) . '</th>';
111 ?></tr>
112 <?php
114 // ************************************************************************** //
116 // sample of html pages
118 $dir = 'samples/Lexer';
119 $dh = opendir($dir);
120 while (false !== ($filename = readdir($dh))) {
122 if (strpos($filename, '.html') !== strlen($filename) - 5) continue;
123 $document = file_get_contents($dir . '/' . $filename);
124 do_benchmark("File: $filename", $document);
128 // crashers, caused infinite loops before
130 $snippets = array();
131 $snippets[] = '<a href="foo>';
132 $snippets[] = '<a "=>';
134 foreach ($snippets as $snippet) {
135 do_benchmark($snippet, $snippet);
138 // random input
140 $random = Text_Password::create(80, 'unpronounceable', 'qwerty <>="\'');
142 do_benchmark('Random input', $random);
144 ?></table>
146 <?php
148 echo '<div>Random input was: ' .
149 '<span colspan="4" style="font-family:monospace;">' .
150 htmlspecialchars($random) . '</span></div>';
155 </body></html>
156 <?php
158 // vim: et sw=4 sts=4