From a631f24392ddfc32ae6387a6db2536c7f53489fe Mon Sep 17 00:00:00 2001 From: Gary Chisholm Date: Mon, 22 Jul 2019 20:03:24 +0100 Subject: [PATCH] [minor] add performance benchmarks --- package.json | 3 +- tests/benchmarker.js | 18 +++++++++ tests/performance.js | 93 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 tests/benchmarker.js create mode 100644 tests/performance.js diff --git a/package.json b/package.json index 25bf86e..d6fbbe4 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,8 @@ "mocha": "^6.2.0" }, "scripts": { - "test": "echo \"Running Mocha Tests\" && mocha ./tests/index.js" + "test": "echo \"Running Mocha Tests\" && mocha ./tests/index.js", + "bench": "echo \"Running Performance Benchmark\" && node ./tests/performance.js" }, "repository": { "type": "git", diff --git a/tests/benchmarker.js b/tests/benchmarker.js new file mode 100644 index 0000000..64d560b --- /dev/null +++ b/tests/benchmarker.js @@ -0,0 +1,18 @@ +exports.bench = function (f) { + let ops = 0; + const startMs = Date.now(); + const end = startMs + 1000; + while(Date.now() < end) { + f(); + ops++; + } + return ops; +}; + +exports.bench10 = function(f) { + let ops = 0; + for(let i = 0; i < 10; i++) { + ops += exports.bench(f); + } + return ops / 10; +} diff --git a/tests/performance.js b/tests/performance.js new file mode 100644 index 0000000..b1d3e75 --- /dev/null +++ b/tests/performance.js @@ -0,0 +1,93 @@ +const benchmarker = require('./benchmarker'); +const diffler = require('../src'); + +const suite = function() { + const testObjectA = { + "id": 157538, + "date": "2017-07-21T10:30:34", + "date_gmt": "2017-07-21T17:30:34", + "guid": { + "rendered": "https://www.sitepoint.com/?p=157538" + }, + "modified": "2017-07-23T21:56:35", + "modified_gmt": "2017-07-24T04:56:35", + "slug": "why-the-iot-threatens-your-wordpress-site-and-how-to-fix-it", + "status": "publish", + "type": "post", + "link": "https://www.sitepoint.com/why-the-iot-threatens-your-wordpress-site-and-how-to-fix-it/", + "title": { + "rendered": "Why the IoT Threatens Your WordPress Site (and How to Fix It)" + }, + "content": { + }, + "excerpt": { + }, + "author": 72546, + "featured_media": 157542, + "comment_status": "open", + "ping_status": "closed", + "sticky": false, + "template": "", + "format": "standard", + "meta": [], + "categories": [ + 6132 + ], + "tags": [ + 1798, + 6298 + ] + }; + const testObjectB = { + "id": 157538, + "date": "2017-07-21T10:30:34", + "date_gmt": "2017-07-21T17:30:34", + "guid": { + "rendered": "https://www.sitepoint.com/?p=157538" + }, + "modified": "2017-07-23T21:56:35", + "modified_gmt": "2017-07-24T04:56:35", + "slug": "why-the-iot-threatens-your-wordpress-site-and-how-to-fix-it", + "status": "publish", + "type": "post", + "link": "https://www.sitepoint.com/why-the-iot-threatens-your-wordpress-site-and-how-to-fix-it/", + "title": { + "rendered": "Why the IoT Threatens Your WordPress Site (and How to Fix It)" + }, + "content": { + }, + "excerpt": { + }, + "author": 72546, + "featured_media": 157542, + "comment_status": "open", + "ping_status": "closed", + "sticky": false, + "template": "", + "format": "standard", + "meta": [], + "categories": [ + 6132 + ], + "tags": [ + 1798, + 6298 + ] + }; + diffler(testObjectA, testObjectB); +} + +const ops = benchmarker.bench10(suite); +const benchmark = 236941; // update value to set new benchmark +console.info(`Executed ${ops} ops/s`); +const diff = Math.round(100 - ((benchmark / ops) * 100)); + +if (diff === 0) { + console.log('Bench complete: Code hasn\'t changed.'); + return; +} else if (diff < -10) { + console.error(`Bench complete: Code ran ${Math.abs(diff)}% slower, it's time to do something.`); + return; +} +const message = diff > 0 ? 'faster': 'slower'; +console.info(`Bench complete: Code ran ${Math.abs(diff)}% ${message} than benchmark.`);