From 9e455b80d6804575fb12ffb7feaa4346ed235397 Mon Sep 17 00:00:00 2001 From: Sami Vaarala Date: Sat, 13 Feb 2016 02:44:11 +0200 Subject: [PATCH] Perf testcases for JSON/JX --- tests/perf/test-json-serialize-forceslow.js | 38 +++++++++++++++++++ .../test-jx-serialize-bufobj-forceslow.js | 36 ++++++++++++++++++ tests/perf/test-jx-serialize-bufobj.js | 35 +++++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 tests/perf/test-json-serialize-forceslow.js create mode 100644 tests/perf/test-jx-serialize-bufobj-forceslow.js create mode 100644 tests/perf/test-jx-serialize-bufobj.js diff --git a/tests/perf/test-json-serialize-forceslow.js b/tests/perf/test-json-serialize-forceslow.js new file mode 100644 index 00000000..7b63c222 --- /dev/null +++ b/tests/perf/test-json-serialize-forceslow.js @@ -0,0 +1,38 @@ +if (typeof print !== 'function') { print = console.log; } + +function build() { + var obj = {}; + + obj.key1 = 'foo'; + obj.key2 = 'bar'; + obj.key3 = 'quux'; + obj.key4 = 'baz'; + obj.key5 = 'quuux'; + obj.key6 = [ 'foo', 'bar', 'quux', 'baz', 'quuux' ]; + obj.key7 = [ undefined, null, true, 123.456, 1e200, {}, {}, {} ]; + + // Force to slow path (at least in Duktape 1.5.x): + // .toJSON() causes a bailout, but because the value + // is not callable it has no impact on the slow path. + Object.defineProperty(obj, 'toJSON', { value: 123, enumerable: false }); + + return obj; +} + +function test() { + var obj; + var i; + var ignore; + + obj = build(); + for (i = 0; i < 4e5; i++) { + ignore = JSON.stringify(obj); + } +} + +try { + test(); +} catch (e) { + print(e.stack || e); + throw e; +} diff --git a/tests/perf/test-jx-serialize-bufobj-forceslow.js b/tests/perf/test-jx-serialize-bufobj-forceslow.js new file mode 100644 index 00000000..42cfce34 --- /dev/null +++ b/tests/perf/test-jx-serialize-bufobj-forceslow.js @@ -0,0 +1,36 @@ +if (typeof print !== 'function') { print = console.log; } + +function build() { + var obj = {}; + var ab = new ArrayBuffer(32); + var vw = new Uint32Array(ab, 4, 3); + var i; + for (i = 0; i < ab.length; i++) { + ab[i] = i; + } + + return { + foo: [ ab, ab, ab, ab, ab, ab, ab, ab, ab, ab, ab, ab, ab, ab, ab, ab ], + bar: [ vw, vw, vw, vw, vw, vw, vw, vw, vw, vw, vw, vw, vw, vw, vw, vw ], + toJSON: 123 // forces slow path in Duktape 1.5.x, but not called + }; +} + +function test() { + var obj; + var i; + var ignore; + + obj = build(); + for (i = 0; i < 3e5; i++) { + ignore = Duktape.enc('jx', obj); + } + //print(ignore); +} + +try { + test(); +} catch (e) { + print(e.stack || e); + throw e; +} diff --git a/tests/perf/test-jx-serialize-bufobj.js b/tests/perf/test-jx-serialize-bufobj.js new file mode 100644 index 00000000..31f2ef68 --- /dev/null +++ b/tests/perf/test-jx-serialize-bufobj.js @@ -0,0 +1,35 @@ +if (typeof print !== 'function') { print = console.log; } + +function build() { + var obj = {}; + var ab = new ArrayBuffer(32); + var vw = new Uint32Array(ab, 4, 3); + var i; + for (i = 0; i < ab.length; i++) { + ab[i] = i; + } + + return { + foo: [ ab, ab, ab, ab, ab, ab, ab, ab, ab, ab, ab, ab, ab, ab, ab, ab ], + bar: [ vw, vw, vw, vw, vw, vw, vw, vw, vw, vw, vw, vw, vw, vw, vw, vw ] + }; +} + +function test() { + var obj; + var i; + var ignore; + + obj = build(); + for (i = 0; i < 3e5; i++) { + ignore = Duktape.enc('jx', obj); + } + //print(ignore); +} + +try { + test(); +} catch (e) { + print(e.stack || e); + throw e; +}