Skip to content

Instantly share code, notes, and snippets.

@cpojer
Created January 5, 2017 17:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cpojer/488c85f2cf86cacc3f4cc378be3f68eb to your computer and use it in GitHub Desktop.
Save cpojer/488c85f2cf86cacc3f4cc378be3f68eb to your computer and use it in GitHub Desktop.
Perf test.
node_modules
'use strict';
const path = require('path');
const workerFarm = require('worker-farm');
function promisify(fn) {
return function() {
const args = Array.prototype.slice.call(arguments);
return new Promise((resolve, reject) => {
args.push((err, res) => {
if (err) {
reject(err);
} else {
resolve(res);
}
});
fn.apply(this, args);
});
};
};
let count = 0;
let total = 0;
const runWithData = data => {
const farm = workerFarm({
autoStart: true,
maxConcurrentCallsPerWorker: 1,
maxConcurrentWorkers: require('os').cpus().length,
maxRetries: 2, // Allow for a couple of transient errors.
}, path.resolve('./worker.js'));
const f = promisify(farm);
const run = data => f(data);
const promises = [];
const start = Date.now();
for (let i = 0; i < 1000; i++) {
promises.push(run(data));
}
return Promise.all(promises).then(() => {
workerFarm.end(farm);
const end = (Date.now() - start);
console.log('run ' + (++count) + ' done in ' + end + 'ms');
total += end;
});
};
const objects = [{a: 1}];
const object = {};
for (let i = 0; i < 100; i++) {
const x = {};
for (let j = 0; j < 10; j++) {
x[String(Math.random())] = [[[[]]], 5, 3, ['a']];
}
object[String(Math.random())] = x;
}
objects.push(object);
let promise = Promise.resolve();
const TIMES = 5;
objects.forEach(object => {
for (let i = 0; i < TIMES; i++) {
promise = promise.then(() => runWithData(object));
}
promise = promise
.then(() => {
console.log('> ' + (total / TIMES) + ' avg');
console.log('------');
count = 0;
total = 0;
});
});
promise = promise.catch(e => {
console.log(e);
});
{
"name": "test-bed",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"worker-farm": "^1.3.1"
}
}
module.exports = (data, callback) => {
setTimeout(() => callback(null, data), 0);
};
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
"errno@>=0.1.1 <0.2.0-0":
version "0.1.4"
resolved "https://registry.yarnpkg.com/errno/-/errno-0.1.4.tgz#b896e23a9e5e8ba33871fc996abd3635fc9a1c7d"
dependencies:
prr "~0.0.0"
prr@~0.0.0:
version "0.0.0"
resolved "https://registry.yarnpkg.com/prr/-/prr-0.0.0.tgz#1a84b85908325501411853d0081ee3fa86e2926a"
worker-farm@^1.3.1:
version "1.3.1"
resolved "https://registry.yarnpkg.com/worker-farm/-/worker-farm-1.3.1.tgz#4333112bb49b17aa050b87895ca6b2cacf40e5ff"
dependencies:
errno ">=0.1.1 <0.2.0-0"
xtend ">=4.0.0 <4.1.0-0"
"xtend@>=4.0.0 <4.1.0-0":
version "4.0.1"
resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.1.tgz#a5c6d532be656e23db820efb943a1f04998d63af"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment