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';
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();
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();
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
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();
53 $out .= "<td>{$this->name}</td>";
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>';
83 function print_lexers() {
86 foreach ($LEXERS as $key => $value) {
87 if (!$first) echo ' / ';
88 echo htmlspecialchars($key);
93 function do_benchmark($name, $document) {
94 global $LEXERS, $RUNS;
96 $timer = new RowTimer($name);
99 foreach($LEXERS as $key => $lexer) {
100 for ($i=0; $i<$RUNS; $i++
) $tokens = $lexer->tokenizeHTML($document);
101 $timer->setMarker($key);
111 <title
>Benchmark
: <?php
print_lexers(); ?
></title
>
114 <h1
>Benchmark
: <?php
print_lexers(); ?
></h1
>
116 <tr
><th
>Case</th
><?php
117 foreach ($LEXERS as $key => $value) {
118 echo '<th colspan="2">' . htmlspecialchars($key) . '</th>';
123 // ************************************************************************** //
125 // sample of html pages
127 $dir = 'samples/Lexer';
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
140 $snippets[] = '<a href="foo>';
141 $snippets[] = '<a "=>';
143 foreach ($snippets as $snippet) {
144 do_benchmark($snippet, $snippet);
149 $random = Text_Password
::create(80, 'unpronounceable', 'qwerty <>="\'');
151 do_benchmark('Random input', $random);
157 echo '<div>Random input was: ' .
158 '<span colspan="4" style="font-family:monospace;">' .
159 htmlspecialchars($random) . '</span></div>';