aboutsummaryrefslogtreecommitdiffstats
path: root/lib/htmlpurifier/benchmarks/Lexer.php
diff options
context:
space:
mode:
authorfriendica <info@friendica.com>2012-05-12 17:57:41 -0700
committerfriendica <info@friendica.com>2012-07-18 20:40:31 +1000
commit7a40f4354b32809af3d0cfd6e3af0eda02ab0e0a (patch)
treea9c3d91209cff770bb4b613b1b95e61a7bbc5a2b /lib/htmlpurifier/benchmarks/Lexer.php
parentcd727cb26b78a1dade09d510b071446898477356 (diff)
downloadvolse-hubzilla-7a40f4354b32809af3d0cfd6e3af0eda02ab0e0a.tar.gz
volse-hubzilla-7a40f4354b32809af3d0cfd6e3af0eda02ab0e0a.tar.bz2
volse-hubzilla-7a40f4354b32809af3d0cfd6e3af0eda02ab0e0a.zip
some important stuff we'll need
Diffstat (limited to 'lib/htmlpurifier/benchmarks/Lexer.php')
-rw-r--r--lib/htmlpurifier/benchmarks/Lexer.php158
1 files changed, 158 insertions, 0 deletions
diff --git a/lib/htmlpurifier/benchmarks/Lexer.php b/lib/htmlpurifier/benchmarks/Lexer.php
new file mode 100644
index 000000000..686ef63e6
--- /dev/null
+++ b/lib/htmlpurifier/benchmarks/Lexer.php
@@ -0,0 +1,158 @@
+<?php
+
+require_once '../library/HTMLPurifier.auto.php';
+@include_once '../test-settings.php';
+
+// PEAR
+require_once 'Benchmark/Timer.php'; // to do the timing
+require_once 'Text/Password.php'; // for generating random input
+
+$LEXERS = array();
+$RUNS = isset($GLOBALS['HTMLPurifierTest']['Runs'])
+ ? $GLOBALS['HTMLPurifierTest']['Runs'] : 2;
+
+require_once 'HTMLPurifier/Lexer/DirectLex.php';
+$LEXERS['DirectLex'] = new HTMLPurifier_Lexer_DirectLex();
+
+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
+{
+
+ var $name;
+
+ function RowTimer($name, $auto = false) {
+ $this->name = htmlentities($name);
+ $this->Benchmark_Timer($auto);
+ }
+
+ function getOutput() {
+
+ $total = $this->TimeElapsed();
+ $result = $this->getProfiling();
+ $dashes = '';
+
+ $out = '<tr>';
+
+ $out .= "<td>{$this->name}</td>";
+
+ $standard = false;
+
+ foreach ($result as $k => $v) {
+ if ($v['name'] == 'Start' || $v['name'] == 'Stop') continue;
+
+ //$perc = (($v['diff'] * 100) / $total);
+ //$tperc = (($v['total'] * 100) / $total);
+
+ //$out .= '<td align="right">' . $v['diff'] . '</td>';
+
+ if ($standard == false) $standard = $v['diff'];
+
+ $perc = $v['diff'] * 100 / $standard;
+ $bad_run = ($v['diff'] < 0);
+
+ $out .= '<td align="right"'.
+ ($bad_run ? ' style="color:#AAA;"' : '').
+ '>' . number_format($perc, 2, '.', '') .
+ '%</td><td>'.number_format($v['diff'],4,'.','').'</td>';
+
+ }
+
+ $out .= '</tr>';
+
+ 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) {
+ global $LEXERS, $RUNS;
+
+ $config = HTMLPurifier_Config::createDefault();
+ $context = new HTMLPurifier_Context();
+
+ $timer = new RowTimer($name);
+ $timer->start();
+
+ foreach($LEXERS as $key => $lexer) {
+ for ($i=0; $i<$RUNS; $i++) $tokens = $lexer->tokenizeHTML($document, $config, $context);
+ $timer->setMarker($key);
+ }
+
+ $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) {
+ echo '<th colspan="2">' . htmlspecialchars($key) . '</th>';
+}
+?></tr>
+<?php
+
+// ************************************************************************** //
+
+// sample of html pages
+
+$dir = 'samples/Lexer';
+$dh = opendir($dir);
+while (false !== ($filename = readdir($dh))) {
+
+ if (strpos($filename, '.html') !== strlen($filename) - 5) continue;
+ $document = file_get_contents($dir . '/' . $filename);
+ do_benchmark("File: $filename", $document);
+
+}
+
+// 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>';
+
+?>
+
+
+</body></html>
+<?php
+
+// vim: et sw=4 sts=4