2006-07-23 00:11:03 +00:00
|
|
|
<?php
|
|
|
|
|
2007-11-23 21:59:04 +00:00
|
|
|
require_once '../library/HTMLPurifier.auto.php';
|
2006-09-24 19:40:28 +00:00
|
|
|
@include_once '../test-settings.php';
|
2006-09-24 18:32:41 +00:00
|
|
|
|
2007-11-23 21:59:04 +00:00
|
|
|
// PEAR
|
|
|
|
require_once 'Benchmark/Timer.php'; // to do the timing
|
|
|
|
require_once 'Text/Password.php'; // for generating random input
|
2006-09-24 18:32:41 +00:00
|
|
|
|
|
|
|
$LEXERS = array();
|
2006-09-24 19:40:28 +00:00
|
|
|
$RUNS = isset($GLOBALS['HTMLPurifierTest']['Runs'])
|
2008-12-06 07:28:20 +00:00
|
|
|
? $GLOBALS['HTMLPurifierTest']['Runs'] : 2;
|
2006-09-24 18:32:41 +00:00
|
|
|
|
2006-07-23 00:11:03 +00:00
|
|
|
require_once 'HTMLPurifier/Lexer/DirectLex.php';
|
2006-09-24 18:32:41 +00:00
|
|
|
$LEXERS['DirectLex'] = new HTMLPurifier_Lexer_DirectLex();
|
2006-07-23 00:11:03 +00:00
|
|
|
|
|
|
|
if (version_compare(PHP_VERSION, '5', '>=')) {
|
|
|
|
require_once 'HTMLPurifier/Lexer/DOMLex.php';
|
|
|
|
$LEXERS['DOMLex'] = new HTMLPurifier_Lexer_DOMLex();
|
|
|
|
}
|
|
|
|
|
|
|
|
// custom class to aid unit testing
|
|
|
|
class RowTimer extends Benchmark_Timer
|
|
|
|
{
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-07-23 00:11:03 +00:00
|
|
|
var $name;
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-07-23 00:11:03 +00:00
|
|
|
function RowTimer($name, $auto = false) {
|
|
|
|
$this->name = htmlentities($name);
|
|
|
|
$this->Benchmark_Timer($auto);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-07-23 00:11:03 +00:00
|
|
|
function getOutput() {
|
|
|
|
|
|
|
|
$total = $this->TimeElapsed();
|
|
|
|
$result = $this->getProfiling();
|
|
|
|
$dashes = '';
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-07-23 00:11:03 +00:00
|
|
|
$out = '<tr>';
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-07-23 00:11:03 +00:00
|
|
|
$out .= "<td>{$this->name}</td>";
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-07-23 18:18:11 +00:00
|
|
|
$standard = false;
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-07-23 00:11:03 +00:00
|
|
|
foreach ($result as $k => $v) {
|
|
|
|
if ($v['name'] == 'Start' || $v['name'] == 'Stop') continue;
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-07-23 00:11:03 +00:00
|
|
|
//$perc = (($v['diff'] * 100) / $total);
|
|
|
|
//$tperc = (($v['total'] * 100) / $total);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-07-23 18:18:11 +00:00
|
|
|
//$out .= '<td align="right">' . $v['diff'] . '</td>';
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-07-23 18:18:11 +00:00
|
|
|
if ($standard == false) $standard = $v['diff'];
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-07-23 18:18:11 +00:00
|
|
|
$perc = $v['diff'] * 100 / $standard;
|
2006-09-24 19:48:29 +00:00
|
|
|
$bad_run = ($v['diff'] < 0);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-09-24 19:48:29 +00:00
|
|
|
$out .= '<td align="right"'.
|
|
|
|
($bad_run ? ' style="color:#AAA;"' : '').
|
|
|
|
'>' . number_format($perc, 2, '.', '') .
|
2006-09-24 18:45:24 +00:00
|
|
|
'%</td><td>'.number_format($v['diff'],4,'.','').'</td>';
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-07-23 00:11:03 +00:00
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-07-23 00:11:03 +00:00
|
|
|
$out .= '</tr>';
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-07-23 00:11:03 +00:00
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function print_lexers() {
|
|
|
|
global $LEXERS;
|
|
|
|
$first = true;
|
|
|
|
foreach ($LEXERS as $key => $value) {
|
|
|
|
if (!$first) echo ' / ';
|
|
|
|
echo htmlspecialchars($key);
|
|
|
|
$first = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function do_benchmark($name, $document) {
|
2006-09-24 18:45:24 +00:00
|
|
|
global $LEXERS, $RUNS;
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-02-13 20:51:47 +00:00
|
|
|
$config = HTMLPurifier_Config::createDefault();
|
|
|
|
$context = new HTMLPurifier_Context();
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-07-23 00:11:03 +00:00
|
|
|
$timer = new RowTimer($name);
|
|
|
|
$timer->start();
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-07-23 00:11:03 +00:00
|
|
|
foreach($LEXERS as $key => $lexer) {
|
2007-02-13 20:51:47 +00:00
|
|
|
for ($i=0; $i<$RUNS; $i++) $tokens = $lexer->tokenizeHTML($document, $config, $context);
|
2006-07-23 00:11:03 +00:00
|
|
|
$timer->setMarker($key);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-07-23 00:11:03 +00:00
|
|
|
$timer->stop();
|
|
|
|
$timer->display();
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>Benchmark: <?php print_lexers(); ?></title>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<h1>Benchmark: <?php print_lexers(); ?></h1>
|
|
|
|
<table border="1">
|
|
|
|
<tr><th>Case</th><?php
|
|
|
|
foreach ($LEXERS as $key => $value) {
|
2006-09-24 18:36:32 +00:00
|
|
|
echo '<th colspan="2">' . htmlspecialchars($key) . '</th>';
|
2006-07-23 00:11:03 +00:00
|
|
|
}
|
|
|
|
?></tr>
|
|
|
|
<?php
|
|
|
|
|
|
|
|
// ************************************************************************** //
|
|
|
|
|
|
|
|
// sample of html pages
|
|
|
|
|
|
|
|
$dir = 'samples/Lexer';
|
|
|
|
$dh = opendir($dir);
|
|
|
|
while (false !== ($filename = readdir($dh))) {
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-07-23 00:11:03 +00:00
|
|
|
if (strpos($filename, '.html') !== strlen($filename) - 5) continue;
|
|
|
|
$document = file_get_contents($dir . '/' . $filename);
|
|
|
|
do_benchmark("File: $filename", $document);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-07-23 00:11:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// crashers, caused infinite loops before
|
|
|
|
|
|
|
|
$snippets = array();
|
|
|
|
$snippets[] = '<a href="foo>';
|
|
|
|
$snippets[] = '<a "=>';
|
|
|
|
|
|
|
|
foreach ($snippets as $snippet) {
|
|
|
|
do_benchmark($snippet, $snippet);
|
|
|
|
}
|
|
|
|
|
|
|
|
// random input
|
|
|
|
|
|
|
|
$random = Text_Password::create(80, 'unpronounceable', 'qwerty <>="\'');
|
|
|
|
|
|
|
|
do_benchmark('Random input', $random);
|
|
|
|
|
|
|
|
?></table>
|
|
|
|
|
|
|
|
<?php
|
|
|
|
|
|
|
|
echo '<div>Random input was: ' .
|
|
|
|
'<span colspan="4" style="font-family:monospace;">' .
|
|
|
|
htmlspecialchars($random) . '</span></div>';
|
|
|
|
|
|
|
|
?>
|
|
|
|
|
|
|
|
|
2007-06-27 13:58:32 +00:00
|
|
|
</body></html>
|
2008-12-06 09:24:59 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
// vim: et sw=4 sts=4
|