3 require_once '../library/HTMLPurifier.auto.php';
4 @include_once
'../test-settings.php';
7 require_once 'Benchmark/Timer.php'; // to do the timing
8 require_once 'Text/Password.php'; // for generating random input
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
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();
41 $out .= "<td>{$this->name}</td>";
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>';
71 function print_lexers() {
74 foreach ($LEXERS as $key => $value) {
75 if (!$first) echo ' / ';
76 echo htmlspecialchars($key);
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);
90 foreach($LEXERS as $key => $lexer) {
91 for ($i=0; $i<$RUNS; $i++
) $tokens = $lexer->tokenizeHTML($document, $config, $context);
92 $timer->setMarker($key);
102 <title
>Benchmark
: <?php
print_lexers(); ?
></title
>
105 <h1
>Benchmark
: <?php
print_lexers(); ?
></h1
>
107 <tr
><th
>Case</th
><?php
108 foreach ($LEXERS as $key => $value) {
109 echo '<th colspan="2">' . htmlspecialchars($key) . '</th>';
114 // ************************************************************************** //
116 // sample of html pages
118 $dir = 'samples/Lexer';
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
131 $snippets[] = '<a href="foo>';
132 $snippets[] = '<a "=>';
134 foreach ($snippets as $snippet) {
135 do_benchmark($snippet, $snippet);
140 $random = Text_Password
::create(80, 'unpronounceable', 'qwerty <>="\'');
142 do_benchmark('Random input', $random);
148 echo '<div>Random input was: ' .
149 '<span colspan="4" style="font-family:monospace;">' .
150 htmlspecialchars($random) . '</span></div>';
158 // vim: et sw=4 sts=4