Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[minor] add performance benchmarks #14

Merged
merged 1 commit into from
Jul 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.`);