[Pkg-javascript-commits] [ltx] 209/469: browser benchmark
Jonas Smedegaard
dr at jones.dk
Wed Aug 31 13:03:04 UTC 2016
This is an automated email from the git hooks/post-receive script.
js pushed a commit to branch master
in repository ltx.
commit 34d72471886d051e39d1b2cb6f46e843162a48e3
Author: Astro <astro at spaceboyz.net>
Date: Fri Mar 16 14:53:49 2012 +0100
browser benchmark
---
benchmark/browser_benchmark.js | 121 +++++++++++++++++++++++++++++++++++++++++
benchmark/build.js | 11 ++++
benchmark/index.html | 9 +++
3 files changed, 141 insertions(+)
diff --git a/benchmark/browser_benchmark.js b/benchmark/browser_benchmark.js
new file mode 100644
index 0000000..3729534
--- /dev/null
+++ b/benchmark/browser_benchmark.js
@@ -0,0 +1,121 @@
+var ltx = require("ltx");
+var strophe = require('Strophe.js');
+var util = require('util');
+
+function now() {
+ return new Date().getTime();
+}
+
+function Test() {
+ this.timings = {};
+}
+Test.prototype = {
+ record: function(name, fun) {
+ var t1 = now();
+ var res = fun();
+ var t2 = now();
+
+ if (!this.timings.hasOwnProperty(name))
+ this.timings[name] = { i: 0, t: 0 };
+ this.timings[name].i++;
+ this.timings[name].t += t2 - t1;
+ },
+
+ report: function() {
+ var s = [];
+ var html = "<div style='float: left; min-width: 25em'><h2>" + this.name + "</h2><dl>";
+ for(var k in this.timings) {
+ var t = this.timings[k].t / this.timings[k].i;
+ html += "<dt>" + k + "</dt><dd class='" + k + "'>" + t + " ms </dd>";
+ }
+ html += "</dl></div>\n";
+ return html;
+ }
+};
+
+function LtxTest(saxParser) {
+ Test.call(this);
+ this.saxParser = saxParser;
+ this.name = "LTX/" + saxParser.name;
+}
+util.inherits(LtxTest, Test);
+
+LtxTest.prototype.parse = function(s) {
+ return ltx.parse(s, this.saxParser);
+};
+
+LtxTest.prototype.serialize = function(el) {
+ return el.toString();
+};
+
+LtxTest.prototype.traverse = function(node) {
+ while(node.children && node.children[0])
+ node = node.children[0];
+};
+
+function StropheTest() {
+ Test.call(this);
+ this.name = "Strophe.js";
+}
+util.inherits(StropheTest, Test);
+
+StropheTest.prototype.parse = function(s) {
+ return Strophe.xmlHtmlNode(s).firstChild;
+};
+
+StropheTest.prototype.serialize = Strophe.serialize;
+
+StropheTest.prototype.traverse = function(node) {
+ while(node.firstChild)
+ node = node.firstChild;
+};
+
+var tests = [new StropheTest()].concat(ltx.availableSaxParsers.map(function(saxParser) {
+ return new LtxTest(saxParser);
+}));
+var messages = [
+ "<message/>",
+ "<message foo='bar'/>",
+ "<message foo='bar'><foo/><bar>fnord<baz/>fnord</bar></message>"
+];
+messages[3] = "<message>";
+for(var i = 0; i < 10; i++) {
+ messages[3] += "fnord fnord fnord<foo bar='baz'>";
+}
+for(var i = 0; i < 10; i++) {
+ messages[3] += "</foo>fnordfnordfnordfnord<quux foo='bar'/>";
+}
+messages[3] += "</message>";
+
+var iteration = 0;
+function runTests() {
+ iteration++;
+ for(var i = 0; i < 10; i++) {
+ tests.forEach(function(test) {
+ for(var j = 0; j < messages.length; j++) {
+ var parsed, serialized;
+ test.record('parse' + j, function() {
+ parsed = test.parse(messages[j]);
+ });
+ test.record('serialize' + j, function() {
+ serialized = test.serialize(parsed);
+ });
+ test.record('traverse' + j, function() {
+ test.traverse(parsed);
+ });
+ }
+ });
+ }
+}
+
+setTimeout(function() {
+ window.setInterval(function() {
+ runTests();
+
+ document.body.innerHTML = "<style>.parse0, .parse1, .parse2, .parse3 { color: red; } .serialize1, .serialize2, .serialize3, .serialize4 { color: blue; }</style>\n" +
+ "<h1>Iteration " + iteration + "<h1>\n";
+ tests.forEach(function(test) {
+ document.write(test.report() + "<br>");
+ });
+ }, 1);
+}, 1000);
diff --git a/benchmark/build.js b/benchmark/build.js
new file mode 100644
index 0000000..99d76b0
--- /dev/null
+++ b/benchmark/build.js
@@ -0,0 +1,11 @@
+var browserify = require('browserify');
+var path = require('path');
+var b = browserify({
+ debug: true,
+ require: ["ltx/lib/sax_saxjs.js", "ltx/lib/sax_easysax.js"]
+});
+b.addEntry('../benchmark/browser_benchmark.js');
+
+var fs = require('fs');
+
+fs.writeFileSync('index.js', b.bundle());
diff --git a/benchmark/index.html b/benchmark/index.html
new file mode 100644
index 0000000..44a50c8
--- /dev/null
+++ b/benchmark/index.html
@@ -0,0 +1,9 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <script src="index.js"></script>
+ </head>
+ <body>
+ </body>
+</html>
+
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/ltx.git
More information about the Pkg-javascript-commits
mailing list