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;
69 $out .= '<td align="right">' . number_format($perc, 2, '.', '') .
70 '%</td><td>'.number_format($v['diff'],4,'.','').'</td>';
80 function print_lexers() {
83 foreach ($LEXERS as $key => $value) {
84 if (!$first) echo ' / ';
85 echo htmlspecialchars($key);
90 function do_benchmark($name, $document) {
91 global $LEXERS, $RUNS;
93 $timer = new RowTimer($name);
96 foreach($LEXERS as $key => $lexer) {
97 for ($i=0; $i<$RUNS; $i++
) $tokens = $lexer->tokenizeHTML($document);
98 $timer->setMarker($key);
108 <title
>Benchmark
: <?php
print_lexers(); ?
></title
>
111 <h1
>Benchmark
: <?php
print_lexers(); ?
></h1
>
113 <tr
><th
>Case</th
><?php
114 foreach ($LEXERS as $key => $value) {
115 echo '<th colspan="2">' . htmlspecialchars($key) . '</th>';
120 // ************************************************************************** //
122 // sample of html pages
124 $dir = 'samples/Lexer';
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
137 $snippets[] = '<a href="foo>';
138 $snippets[] = '<a "=>';
140 foreach ($snippets as $snippet) {
141 do_benchmark($snippet, $snippet);
146 $random = Text_Password
::create(80, 'unpronounceable', 'qwerty <>="\'');
148 do_benchmark('Random input', $random);
154 echo '<div>Random input was: ' .
155 '<span colspan="4" style="font-family:monospace;">' .
156 htmlspecialchars($random) . '</span></div>';