summaryrefslogtreecommitdiffstats
path: root/node_modules/worker-farm/examples/pi
diff options
context:
space:
mode:
authorGravatar Piotr Russ <mail@pruss.it> 2020-11-16 00:10:28 +0100
committerGravatar Piotr Russ <mail@pruss.it> 2020-11-16 00:10:28 +0100
commite06ec920f7a5d784e674c4c4b4e6d1da3dc7391d (patch)
tree55713f725f77b44ebfec86e4eec3ce33e71458ca /node_modules/worker-farm/examples/pi
downloadwebsite_creator-e06ec920f7a5d784e674c4c4b4e6d1da3dc7391d.tar.gz
website_creator-e06ec920f7a5d784e674c4c4b4e6d1da3dc7391d.tar.bz2
website_creator-e06ec920f7a5d784e674c4c4b4e6d1da3dc7391d.zip
api, login, auth
Diffstat (limited to 'node_modules/worker-farm/examples/pi')
-rw-r--r--node_modules/worker-farm/examples/pi/calc.js22
-rw-r--r--node_modules/worker-farm/examples/pi/index.js41
2 files changed, 63 insertions, 0 deletions
diff --git a/node_modules/worker-farm/examples/pi/calc.js b/node_modules/worker-farm/examples/pi/calc.js
new file mode 100644
index 0000000..df9e5ba
--- /dev/null
+++ b/node_modules/worker-farm/examples/pi/calc.js
@@ -0,0 +1,22 @@
+'use strict'
+
+/* A simple π estimation function using a Monte Carlo method
+ * For 0 to `points`, take 2 random numbers < 1, square and add them to
+ * find the area under that point in a 1x1 square. If that area is <= 1
+ * then it's *within* a quarter-circle, otherwise it's outside.
+ * Take the number of points <= 1 and multiply it by 4 and you have an
+ * estimate!
+ * Do this across multiple processes and average the results to
+ * increase accuracy.
+ */
+
+module.exports = function (points, callback) {
+ let inside = 0
+ , i = points
+
+ while (i--)
+ if (Math.pow(Math.random(), 2) + Math.pow(Math.random(), 2) <= 1)
+ inside++
+
+ callback(null, (inside / points) * 4)
+}
diff --git a/node_modules/worker-farm/examples/pi/index.js b/node_modules/worker-farm/examples/pi/index.js
new file mode 100644
index 0000000..b7b2683
--- /dev/null
+++ b/node_modules/worker-farm/examples/pi/index.js
@@ -0,0 +1,41 @@
+'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)
+})