Skip to content

Instantly share code, notes, and snippets.

@cpojer
Created January 5, 2017 17:16
Show Gist options
  • Select an option

  • Save cpojer/488c85f2cf86cacc3f4cc378be3f68eb to your computer and use it in GitHub Desktop.

Select an option

Save cpojer/488c85f2cf86cacc3f4cc378be3f68eb to your computer and use it in GitHub Desktop.

Revisions

  1. cpojer revised this gist Jan 5, 2017. 4 changed files with 117 additions and 3 deletions.
    79 changes: 79 additions & 0 deletions index.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,79 @@
    '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);
    });
    14 changes: 11 additions & 3 deletions package.json
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,15 @@
    {
    "name": "yarn-test-bed",
    "version": "1.3.1",
    "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": {
    "create-react-app": "^0.5.0"
    "worker-farm": "^1.3.1"
    }
    }
    3 changes: 3 additions & 0 deletions worker.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    module.exports = (data, callback) => {
    setTimeout(() => callback(null, data), 0);
    };
    24 changes: 24 additions & 0 deletions yarn.lock
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    # 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"
  2. cpojer revised this gist Oct 5, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion package.json
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    {
    "name": "yarn-test-bed",
    "version": "1.3.0",
    "version": "1.3.1",
    "dependencies": {
    "create-react-app": "^0.5.0"
    }
  3. cpojer revised this gist Oct 5, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion package.json
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    {
    "name": "yarn-test-bed",
    "version": "1.2.3",
    "version": "1.3.0",
    "dependencies": {
    "create-react-app": "^0.5.0"
    }
  4. cpojer created this gist Oct 5, 2016.
    1 change: 1 addition & 0 deletions .gitignore
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    node_modules
    7 changes: 7 additions & 0 deletions package.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    {
    "name": "yarn-test-bed",
    "version": "1.2.3",
    "dependencies": {
    "create-react-app": "^0.5.0"
    }
    }