- Update install docs with PHP version compatibility not
[htmlpurifier.git] / benchmarks / Lexer.php
blobda112fe4efa5dbae4b0a7c8c3644409bb1218b8c
1 <?php
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';
11 $LEXERS = array(
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();
21 // PEAR
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
29 var $name;
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();
40 $dashes = '';
42 $out = '<tr>';
44 $out .= "<td>{$this->name}</td>";
46 $standard = false;
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, '.', '') .
61 '%</td>';
65 $out .= '</tr>';
67 return $out;
71 function print_lexers() {
72 global $LEXERS;
73 $first = true;
74 foreach ($LEXERS as $key => $value) {
75 if (!$first) echo ' / ';
76 echo htmlspecialchars($key);
77 $first = false;
81 function do_benchmark($name, $document) {
82 global $LEXERS;
84 $timer = new RowTimer($name);
85 $timer->start();
87 foreach($LEXERS as $key => $lexer) {
88 $tokens = $lexer->tokenizeHTML($document);
89 $timer->setMarker($key);
92 $timer->stop();
93 $timer->display();
97 <html>
98 <head>
99 <title>Benchmark: <?php print_lexers(); ?></title>
100 </head>
101 <body>
102 <h1>Benchmark: <?php print_lexers(); ?></h1>
103 <table border="1">
104 <tr><th>Case</th><?php
105 foreach ($LEXERS as $key => $value) {
106 echo '<th>' . htmlspecialchars($key) . '</th>';
108 ?></tr>
109 <?php
111 // ************************************************************************** //
113 // sample of html pages
115 $dir = 'samples/Lexer';
116 $dh = opendir($dir);
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
127 $snippets = array();
128 $snippets[] = '<a href="foo>';
129 $snippets[] = '<a "=>';
131 foreach ($snippets as $snippet) {
132 do_benchmark($snippet, $snippet);
135 // random input
137 $random = Text_Password::create(80, 'unpronounceable', 'qwerty <>="\'');
139 do_benchmark('Random input', $random);
141 ?></table>
143 <?php
145 echo '<div>Random input was: ' .
146 '<span colspan="4" style="font-family:monospace;">' .
147 htmlspecialchars($random) . '</span></div>';
152 </body></html>