3 // emulates inserting a dir called HTMLPurifier into your class dir
4 set_include_path(get_include_path() . PATH_SEPARATOR
. '../library/');
6 require_once 'HTMLPurifier/ConfigDef.php';
7 require_once 'HTMLPurifier/Config.php';
8 require_once 'HTMLPurifier/Lexer/DirectLex.php';
9 require_once 'HTMLPurifier/Lexer/PEARSax3.php';
12 'DirectLex' => new HTMLPurifier_Lexer_DirectLex(),
13 'PEARSax3' => new HTMLPurifier_Lexer_PEARSax3()
16 if (version_compare(PHP_VERSION
, '5', '>=')) {
17 require_once 'HTMLPurifier/Lexer/DOMLex.php';
18 $LEXERS['DOMLex'] = new HTMLPurifier_Lexer_DOMLex();
22 require_once 'Benchmark/Timer.php'; // to do the timing
23 require_once 'Text/Password.php'; // for generating random input
25 // custom class to aid unit testing
26 class RowTimer
extends Benchmark_Timer
31 function RowTimer($name, $auto = false) {
32 $this->name
= htmlentities($name);
33 $this->Benchmark_Timer($auto);
36 function getOutput() {
38 $total = $this->TimeElapsed();
39 $result = $this->getProfiling();
44 $out .= "<td>{$this->name}</td>";
48 foreach ($result as $k => $v) {
49 if ($v['name'] == 'Start' ||
$v['name'] == 'Stop') continue;
51 //$perc = (($v['diff'] * 100) / $total);
52 //$tperc = (($v['total'] * 100) / $total);
54 //$out .= '<td align="right">' . $v['diff'] . '</td>';
56 if ($standard == false) $standard = $v['diff'];
58 $perc = $v['diff'] * 100 / $standard;
60 $out .= '<td align="right">' . number_format($perc, 2, '.', '') .
71 function print_lexers() {
74 foreach ($LEXERS as $key => $value) {
75 if (!$first) echo ' / ';
76 echo htmlspecialchars($key);
81 function do_benchmark($name, $document) {
84 $timer = new RowTimer($name);
87 foreach($LEXERS as $key => $lexer) {
88 $tokens = $lexer->tokenizeHTML($document);
89 $timer->setMarker($key);
99 <title
>Benchmark
: <?php
print_lexers(); ?
></title
>
102 <h1
>Benchmark
: <?php
print_lexers(); ?
></h1
>
104 <tr
><th
>Case</th
><?php
105 foreach ($LEXERS as $key => $value) {
106 echo '<th>' . htmlspecialchars($key) . '</th>';
111 // ************************************************************************** //
113 // sample of html pages
115 $dir = 'samples/Lexer';
117 while (false !== ($filename = readdir($dh))) {
119 if (strpos($filename, '.html') !== strlen($filename) - 5) continue;
120 $document = file_get_contents($dir . '/' . $filename);
121 do_benchmark("File: $filename", $document);
125 // crashers, caused infinite loops before
128 $snippets[] = '<a href="foo>';
129 $snippets[] = '<a "=>';
131 foreach ($snippets as $snippet) {
132 do_benchmark($snippet, $snippet);
137 $random = Text_Password
::create(80, 'unpronounceable', 'qwerty <>="\'');
139 do_benchmark('Random input', $random);
145 echo '<div>Random input was: ' .
146 '<span colspan="4" style="font-family:monospace;">' .
147 htmlspecialchars($random) . '</span></div>';