benchmark-cream
Wrap a promise and send it to loop N times and receive back a time period in seconds. Use the result for benchmarking purposes.
#Motivation
When developing some Node.js module, I wanted to check the speed of different methods. So before writing the code I intended, I decided to publish this script first.
#Example
var Promise = require('bluebird');
var screaper = require('screaper');
var Cream = require('./benchmark-cream');
var options = {selector: "h2", class: "entry-title", endTag: "a", length: "250"};
var giveMeOnePromise = function () {
return new Promise(function (resolve, reject) {
screaper.action('http://www.prometod.eu/en/', options).then(function (data) {
resolve(data)
}).catch(console.log)
})
};
Cream.main(100, giveMeOnePromise, function (result) {
console.log(result)
})
-giveMeOnePromise - a common pattern to wrap your asynchronous function with new promise
-screaper.action - thenable method under benchmarking.
#API
##main(numberLoops, giveMeOnePromise, callback)
-- numberLoops - number iterations
-- giveMeOnePromise - thenable
-- callback - it returns an object
###.time - time as a number - the period between the start and the finish of benchmarking
###.timeString - time as a string
example output - '38 seconds'
###.data - array of resolved within giveMeOnePromise values
For example, if you resolve a string, you will get array of strings
example output - [resolvedDataStep1,resolvedDataStep2, ... resolvedDataStepN]
##returnTime
same as main but returns only time as number
##each
same as main but uses async's each and returns time as number
##times
same as main but uses async's times and returns an array:
index 0 => time as number
index 1 => array of resolved within giveMeOnePromise values
#Tests
The tests file is not only show the library in actin, but also shows how cool testing is with Unexpected.