Skip to content

Commit

Permalink
Merge pull request #14 from omgaz/benchmark
Browse files Browse the repository at this point in the history
[minor] add performance benchmarks
  • Loading branch information
omgaz authored Jul 23, 2019
2 parents ed47fd3 + a631f24 commit 594648d
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
18 changes: 18 additions & 0 deletions tests/benchmarker.js
Original file line number Diff line number Diff line change
@@ -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;
}
93 changes: 93 additions & 0 deletions tests/performance.js
Original file line number Diff line number Diff line change
@@ -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.`);

0 comments on commit 594648d

Please sign in to comment.