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';
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();
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();
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
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();
54 $out .= "<td>{$this->name}</td>";
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>';
84 function print_lexers() {
87 foreach ($LEXERS as $key => $value) {
88 if (!$first) echo ' / ';
89 echo htmlspecialchars($key);
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);
103 foreach($LEXERS as $key => $lexer) {
104 for ($i=0; $i<$RUNS; $i++
) $tokens = $lexer->tokenizeHTML($document, $config, $context);
105 $timer->setMarker($key);
115 <title
>Benchmark
: <?php
print_lexers(); ?
></title
>
118 <h1
>Benchmark
: <?php
print_lexers(); ?
></h1
>
120 <tr
><th
>Case</th
><?php
121 foreach ($LEXERS as $key => $value) {
122 echo '<th colspan="2">' . htmlspecialchars($key) . '</th>';
127 // ************************************************************************** //
129 // sample of html pages
131 $dir = 'samples/Lexer';
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
144 $snippets[] = '<a href="foo>';
145 $snippets[] = '<a "=>';
147 foreach ($snippets as $snippet) {
148 do_benchmark($snippet, $snippet);
153 $random = Text_Password
::create(80, 'unpronounceable', 'qwerty <>="\'');
155 do_benchmark('Random input', $random);
161 echo '<div>Random input was: ' .
162 '<span colspan="4" style="font-family:monospace;">' .
163 htmlspecialchars($random) . '</span></div>';