From a7189c0177938545050421a600fb12c21db3858f Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Wed, 28 Jun 2017 20:34:19 +0200 Subject: [PATCH] benchmark: fix and extend assert benchmarks The benchmarks had the strict and non strict labels switched. This is fixed and the benchmarks were extended to check more possible input types and function calls. PR-URL: https://github.com/nodejs/node/pull/14147 Refs: https://github.com/nodejs/node/pull/13973 Reviewed-By: Refael Ackermann Reviewed-By: James M Snell --- benchmark/assert/deepequal-buffer.js | 35 +++++- benchmark/assert/deepequal-object.js | 73 +++++++++++ .../deepequal-prims-and-objs-big-array-set.js | 119 ++++++++++++++++++ .../deepequal-prims-and-objs-big-array.js | 55 -------- .../deepequal-prims-and-objs-big-loop.js | 27 +++- benchmark/assert/deepequal-typedarrays.js | 29 ++++- benchmark/assert/throws.js | 57 +++++++++ 7 files changed, 328 insertions(+), 67 deletions(-) create mode 100644 benchmark/assert/deepequal-object.js create mode 100644 benchmark/assert/deepequal-prims-and-objs-big-array-set.js delete mode 100644 benchmark/assert/deepequal-prims-and-objs-big-array.js create mode 100644 benchmark/assert/throws.js diff --git a/benchmark/assert/deepequal-buffer.js b/benchmark/assert/deepequal-buffer.js index 2a7d9e3bed7c38..9e86aa231e69d2 100644 --- a/benchmark/assert/deepequal-buffer.js +++ b/benchmark/assert/deepequal-buffer.js @@ -1,10 +1,16 @@ 'use strict'; const common = require('../common.js'); const assert = require('assert'); + const bench = common.createBenchmark(main, { - n: [1e3], - len: [1e2], - method: ['strict', 'nonstrict'] + n: [1e5], + len: [1e2, 1e4], + method: [ + 'deepEqual', + 'deepStrictEqual', + 'notDeepEqual', + 'notDeepStrictEqual' + ] }); function main(conf) { @@ -12,14 +18,16 @@ function main(conf) { const len = +conf.len; var i; - const data = Buffer.allocUnsafe(len); + const data = Buffer.allocUnsafe(len + 1); const actual = Buffer.alloc(len); const expected = Buffer.alloc(len); + const expectedWrong = Buffer.alloc(len + 1); data.copy(actual); data.copy(expected); + data.copy(expectedWrong); switch (conf.method) { - case 'strict': + case 'deepEqual': bench.start(); for (i = 0; i < n; ++i) { // eslint-disable-next-line no-restricted-properties @@ -27,13 +35,28 @@ function main(conf) { } bench.end(n); break; - case 'nonstrict': + case 'deepStrictEqual': bench.start(); for (i = 0; i < n; ++i) { assert.deepStrictEqual(actual, expected); } bench.end(n); break; + case 'notDeepEqual': + bench.start(); + for (i = 0; i < n; ++i) { + // eslint-disable-next-line no-restricted-properties + assert.notDeepEqual(actual, expectedWrong); + } + bench.end(n); + break; + case 'notDeepStrictEqual': + bench.start(); + for (i = 0; i < n; ++i) { + assert.notDeepStrictEqual(actual, expectedWrong); + } + bench.end(n); + break; default: throw new Error('Unsupported method'); } diff --git a/benchmark/assert/deepequal-object.js b/benchmark/assert/deepequal-object.js new file mode 100644 index 00000000000000..d1a6cb69944055 --- /dev/null +++ b/benchmark/assert/deepequal-object.js @@ -0,0 +1,73 @@ +'use strict'; + +const common = require('../common.js'); +const assert = require('assert'); + +const bench = common.createBenchmark(main, { + n: [1e6], + size: [1e2, 1e3, 1e4], + method: [ + 'deepEqual', + 'deepStrictEqual', + 'notDeepEqual', + 'notDeepStrictEqual' + ] +}); + +function createObj(source, add = '') { + return source.map((n) => ({ + foo: 'yarp', + nope: { + bar: `123${add}`, + a: [1, 2, 3], + baz: n + } + })); +} + +function main(conf) { + const size = +conf.size; + // TODO: Fix this "hack" + const n = (+conf.n) / size; + var i; + + const source = Array.apply(null, Array(size)); + const actual = createObj(source); + const expected = createObj(source); + const expectedWrong = createObj(source, '4'); + + switch (conf.method) { + case 'deepEqual': + bench.start(); + for (i = 0; i < n; ++i) { + // eslint-disable-next-line no-restricted-properties + assert.deepEqual(actual, expected); + } + bench.end(n); + break; + case 'deepStrictEqual': + bench.start(); + for (i = 0; i < n; ++i) { + assert.deepStrictEqual(actual, expected); + } + bench.end(n); + break; + case 'notDeepEqual': + bench.start(); + for (i = 0; i < n; ++i) { + // eslint-disable-next-line no-restricted-properties + assert.notDeepEqual(actual, expectedWrong); + } + bench.end(n); + break; + case 'notDeepStrictEqual': + bench.start(); + for (i = 0; i < n; ++i) { + assert.notDeepStrictEqual(actual, expectedWrong); + } + bench.end(n); + break; + default: + throw new Error('Unsupported method'); + } +} diff --git a/benchmark/assert/deepequal-prims-and-objs-big-array-set.js b/benchmark/assert/deepequal-prims-and-objs-big-array-set.js new file mode 100644 index 00000000000000..1e356cea51efd6 --- /dev/null +++ b/benchmark/assert/deepequal-prims-and-objs-big-array-set.js @@ -0,0 +1,119 @@ +'use strict'; + +const common = require('../common.js'); +const assert = require('assert'); + +const primValues = { + 'null': null, + 'undefined': undefined, + 'string': 'a', + 'number': 1, + 'boolean': true, + 'object': { 0: 'a' }, + 'array': [1, 2, 3], + 'new-array': new Array([1, 2, 3]) +}; + +const bench = common.createBenchmark(main, { + prim: Object.keys(primValues), + n: [25], + len: [1e5], + method: [ + 'deepEqual_Array', + 'deepStrictEqual_Array', + 'notDeepEqual_Array', + 'notDeepStrictEqual_Array', + 'deepEqual_Set', + 'deepStrictEqual_Set', + 'notDeepEqual_Set', + 'notDeepStrictEqual_Set' + ] +}); + +function main(conf) { + const prim = primValues[conf.prim]; + const n = +conf.n; + const len = +conf.len; + const actual = []; + const expected = []; + const expectedWrong = []; + var i; + + for (var x = 0; x < len; x++) { + actual.push(prim); + expected.push(prim); + expectedWrong.push(prim); + } + expectedWrong.pop(); + expectedWrong.push('b'); + + // Note: primitives are only added once to a set + const actualSet = new Set(actual); + const expectedSet = new Set(expected); + const expectedWrongSet = new Set(expectedWrong); + + switch (conf.method) { + case 'deepEqual_Array': + bench.start(); + for (i = 0; i < n; ++i) { + // eslint-disable-next-line no-restricted-properties + assert.deepEqual(actual, expected); + } + bench.end(n); + break; + case 'deepStrictEqual_Array': + bench.start(); + for (i = 0; i < n; ++i) { + assert.deepStrictEqual(actual, expected); + } + bench.end(n); + break; + case 'notDeepEqual_Array': + bench.start(); + for (i = 0; i < n; ++i) { + // eslint-disable-next-line no-restricted-properties + assert.notDeepEqual(actual, expectedWrong); + } + bench.end(n); + break; + case 'notDeepStrictEqual_Array': + bench.start(); + for (i = 0; i < n; ++i) { + assert.notDeepStrictEqual(actual, expectedWrong); + } + bench.end(n); + break; + case 'deepEqual_Set': + bench.start(); + for (i = 0; i < n; ++i) { + // eslint-disable-next-line no-restricted-properties + assert.deepEqual(actualSet, expectedSet); + } + bench.end(n); + break; + case 'deepStrictEqual_Set': + bench.start(); + for (i = 0; i < n; ++i) { + assert.deepStrictEqual(actualSet, expectedSet); + } + bench.end(n); + break; + case 'notDeepEqual_Set': + bench.start(); + for (i = 0; i < n; ++i) { + // eslint-disable-next-line no-restricted-properties + assert.notDeepEqual(actualSet, expectedWrongSet); + } + bench.end(n); + break; + case 'notDeepStrictEqual_Set': + bench.start(); + for (i = 0; i < n; ++i) { + assert.notDeepStrictEqual(actualSet, expectedWrongSet); + } + bench.end(n); + break; + default: + throw new Error('Unsupported method'); + } +} diff --git a/benchmark/assert/deepequal-prims-and-objs-big-array.js b/benchmark/assert/deepequal-prims-and-objs-big-array.js deleted file mode 100644 index 69eda8af087dfa..00000000000000 --- a/benchmark/assert/deepequal-prims-and-objs-big-array.js +++ /dev/null @@ -1,55 +0,0 @@ -'use strict'; -const common = require('../common.js'); -const assert = require('assert'); - -const primValues = { - 'null': null, - 'undefined': undefined, - 'string': 'a', - 'number': 1, - 'boolean': true, - 'object': { 0: 'a' }, - 'array': [1, 2, 3], - 'new-array': new Array([1, 2, 3]) -}; - -const bench = common.createBenchmark(main, { - prim: Object.keys(primValues), - n: [25], - len: [1e5], - method: ['strict', 'nonstrict'] -}); - -function main(conf) { - const prim = primValues[conf.prim]; - const n = +conf.n; - const len = +conf.len; - const actual = []; - const expected = []; - var i; - - for (var x = 0; x < len; x++) { - actual.push(prim); - expected.push(prim); - } - - switch (conf.method) { - case 'strict': - bench.start(); - for (i = 0; i < n; ++i) { - // eslint-disable-next-line no-restricted-properties - assert.deepEqual(actual, expected); - } - bench.end(n); - break; - case 'nonstrict': - bench.start(); - for (i = 0; i < n; ++i) { - assert.deepStrictEqual(actual, expected); - } - bench.end(n); - break; - default: - throw new Error('Unsupported method'); - } -} diff --git a/benchmark/assert/deepequal-prims-and-objs-big-loop.js b/benchmark/assert/deepequal-prims-and-objs-big-loop.js index 781c5ad754e723..2800b51c491bcf 100644 --- a/benchmark/assert/deepequal-prims-and-objs-big-loop.js +++ b/benchmark/assert/deepequal-prims-and-objs-big-loop.js @@ -16,7 +16,12 @@ const primValues = { const bench = common.createBenchmark(main, { prim: Object.keys(primValues), n: [1e6], - method: ['strict', 'nonstrict'] + method: [ + 'deepEqual', + 'deepStrictEqual', + 'notDeepEqual', + 'notDeepStrictEqual' + ] }); function main(conf) { @@ -24,11 +29,12 @@ function main(conf) { const n = +conf.n; const actual = prim; const expected = prim; + const expectedWrong = 'b'; var i; // Creates new array to avoid loop invariant code motion switch (conf.method) { - case 'strict': + case 'deepEqual': bench.start(); for (i = 0; i < n; ++i) { // eslint-disable-next-line no-restricted-properties @@ -36,13 +42,28 @@ function main(conf) { } bench.end(n); break; - case 'nonstrict': + case 'deepStrictEqual': bench.start(); for (i = 0; i < n; ++i) { assert.deepStrictEqual([actual], [expected]); } bench.end(n); break; + case 'notDeepEqual': + bench.start(); + for (i = 0; i < n; ++i) { + // eslint-disable-next-line no-restricted-properties + assert.notDeepEqual([actual], [expectedWrong]); + } + bench.end(n); + break; + case 'notDeepStrictEqual': + bench.start(); + for (i = 0; i < n; ++i) { + assert.notDeepStrictEqual([actual], [expectedWrong]); + } + bench.end(n); + break; default: throw new Error('Unsupported method'); } diff --git a/benchmark/assert/deepequal-typedarrays.js b/benchmark/assert/deepequal-typedarrays.js index 00c6ca5adf2835..5d51d53d13e70f 100644 --- a/benchmark/assert/deepequal-typedarrays.js +++ b/benchmark/assert/deepequal-typedarrays.js @@ -1,6 +1,7 @@ 'use strict'; const common = require('../common.js'); const assert = require('assert'); + const bench = common.createBenchmark(main, { type: [ 'Int8Array', @@ -14,7 +15,12 @@ const bench = common.createBenchmark(main, { 'Uint8ClampedArray', ], n: [1], - method: ['strict', 'nonstrict'], + method: [ + 'deepEqual', + 'deepStrictEqual', + 'notDeepEqual', + 'notDeepStrictEqual' + ], len: [1e6] }); @@ -26,10 +32,12 @@ function main(conf) { const actual = new clazz(len); const expected = new clazz(len); + const expectedWrong = Buffer.alloc(len); + expectedWrong[100] = 123; var i; switch (conf.method) { - case 'strict': + case 'deepEqual': bench.start(); for (i = 0; i < n; ++i) { // eslint-disable-next-line no-restricted-properties @@ -37,13 +45,28 @@ function main(conf) { } bench.end(n); break; - case 'nonstrict': + case 'deepStrictEqual': bench.start(); for (i = 0; i < n; ++i) { assert.deepStrictEqual(actual, expected); } bench.end(n); break; + case 'notDeepEqual': + bench.start(); + for (i = 0; i < n; ++i) { + // eslint-disable-next-line no-restricted-properties + assert.notDeepEqual(actual, expectedWrong); + } + bench.end(n); + break; + case 'notDeepStrictEqual': + bench.start(); + for (i = 0; i < n; ++i) { + assert.notDeepStrictEqual(actual, expectedWrong); + } + bench.end(n); + break; default: throw new Error('Unsupported method'); } diff --git a/benchmark/assert/throws.js b/benchmark/assert/throws.js new file mode 100644 index 00000000000000..9043799256e196 --- /dev/null +++ b/benchmark/assert/throws.js @@ -0,0 +1,57 @@ +'use strict'; + +const common = require('../common.js'); +const assert = require('assert'); + +const bench = common.createBenchmark(main, { + n: [1e6], + method: [ + 'doesNotThrow', + 'throws', + 'throws_TypeError', + 'throws_RegExp' + ] +}); + +function main(conf) { + const n = +conf.n; + const throws = () => { throw new TypeError('foobar'); }; + const doesNotThrow = () => { return 'foobar'; }; + const regExp = /foobar/; + const message = 'failure'; + var i; + + switch (conf.method) { + case 'doesNotThrow': + bench.start(); + for (i = 0; i < n; ++i) { + assert.doesNotThrow(doesNotThrow); + } + bench.end(n); + break; + case 'throws': + bench.start(); + for (i = 0; i < n; ++i) { + // eslint-disable-next-line no-restricted-syntax + assert.throws(throws); + } + bench.end(n); + break; + case 'throws_TypeError': + bench.start(); + for (i = 0; i < n; ++i) { + assert.throws(throws, TypeError, message); + } + bench.end(n); + break; + case 'throws_RegExp': + bench.start(); + for (i = 0; i < n; ++i) { + assert.throws(throws, regExp, message); + } + bench.end(n); + break; + default: + throw new Error(`Unsupported method ${conf.method}`); + } +}