-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from omgaz/benchmark
[minor] add performance benchmarks
- Loading branch information
Showing
3 changed files
with
113 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.`); |