Sha256: 578023b4154e6fbceb8b41e272f22597de1c54a9f6f374010d9a9d93adef30ab

Contents?: true

Size: 1.18 KB

Versions: 31

Compression:

Stored size: 1.18 KB

Contents

'use strict'

const CHILDREN         = 500
    , POINTS_PER_CHILD = 1000000
    , FARM_OPTIONS     = {
          maxConcurrentWorkers        : require('os').cpus().length
        , maxCallsPerWorker           : Infinity
        , maxConcurrentCallsPerWorker : 1
      }

let workerFarm = require('../../')
  , calcDirect = require('./calc')
  , calcWorker = workerFarm(FARM_OPTIONS, require.resolve('./calc'))

  , ret
  , start

  , tally = function (finish, err, avg) {
      ret.push(avg)
      if (ret.length == CHILDREN) {
        let pi  = ret.reduce(function (a, b) { return a + b }) / ret.length
          , end = +new Date()
        console.log('π ≈', pi, '\t(' + Math.abs(pi - Math.PI), 'away from actual!)')
        console.log('took', end - start, 'milliseconds')
        if (finish)
          finish()
      }
    }

  , calc = function (method, callback) {
      ret   = []
      start = +new Date()
      for (let i = 0; i < CHILDREN; i++)
        method(POINTS_PER_CHILD, tally.bind(null, callback))
    }

console.log('Doing it the slow (single-process) way...')
calc(calcDirect, function () {
  console.log('Doing it the fast (multi-process) way...')
  calc(calcWorker, process.exit)
})

Version data entries

31 entries across 30 versions & 10 rubygems

Version Path
optimacms-0.1.61 spec/dummy/node_modules/worker-farm/examples/pi/index.js
disco_app-0.18.0 test/dummy/node_modules/worker-farm/examples/pi/index.js
disco_app-0.18.2 test/dummy/node_modules/worker-farm/examples/pi/index.js
disco_app-0.16.1 test/dummy/node_modules/worker-farm/examples/pi/index.js
disco_app-0.15.2 test/dummy/node_modules/worker-farm/examples/pi/index.js
disco_app-0.18.4 test/dummy/node_modules/worker-farm/examples/pi/index.js
disco_app-0.18.1 test/dummy/node_modules/worker-farm/examples/pi/index.js
disco_app-0.12.7.pre.puma.pre.3 test/dummy/node_modules/worker-farm/examples/pi/index.js
disco_app-0.14.0 test/dummy/node_modules/worker-farm/examples/pi/index.js
disco_app-0.13.6.pre.puma.pre.3 test/dummy/node_modules/worker-farm/examples/pi/index.js
tang-0.2.1 spec/tang_app/node_modules/worker-farm/examples/pi/index.js
groonga-client-model-6.0.0 test/apps/rails6.0.3.5/node_modules/worker-farm/examples/pi/index.js
groonga-client-model-6.0.0 test/apps/rails6.1.3/node_modules/worker-farm/examples/pi/index.js
ruby2js-4.0.4 lib/tasks/testrails/node_modules/worker-farm/examples/pi/index.js
ruby2js-4.0.3 lib/tasks/testrails/node_modules/worker-farm/examples/pi/index.js
tang-0.2.0 spec/tang_app/node_modules/worker-farm/examples/pi/index.js
tang-0.1.0 spec/tang_app/node_modules/worker-farm/examples/pi/index.js
tang-0.0.9 spec/tang_app/node_modules/worker-farm/examples/pi/index.js
enju_library-0.3.8 spec/dummy/node_modules/worker-farm/examples/pi/index.js
jester-data-8.0.0 node_modules/worker-farm/examples/pi/index.js