const Benchmark = require('benchmark')
const suite = new Benchmark.Suite()
const FixedThreadPool = require('../lib/fixed')
+const DynamicThreadPool = require('../lib/dynamic')
const Pool = require('worker-threads-pool')
-const size = 80
+const size = 40
const externalPool = new Pool({ max: size })
const fixedPool = new FixedThreadPool(size,
'./yourWorker.js', { maxTasks: 10000 })
-// const dynamicPool = new DynamicThreadPool(size, size * 2, './yourWorker.js', { maxTasks: 10000 })
+const dynamicPool = new DynamicThreadPool(size, size * 2, './yourWorker.js', { maxTasks: 10000 })
const workerData = { proof: 'ok' }
let executions = 0
+let executions1 = 0
// wait some seconds before start, my pools need to load threads !!!
setTimeout(async () => {
executions++
await fixedPool.execute(workerData)
})
+
.add('ExternalPool', async function () {
await new Promise((resolve, reject) => {
externalPool.acquire('./externalWorker.js', { workerData: workerData }, (err, worker) => {
if (err) {
return reject(err)
}
+ executions1++
worker.on('error', reject)
worker.on('message', res => {
resolve(res)
})
})
})
- /*
.add('PioardiDynamicPool', async function () {
await dynamicPool.execute(workerData)
- }) */
+ })
// add listeners
.on('cycle', function (event) {
console.log(String(event.target))
})
.on('complete', function () {
console.log(executions)
+ console.log(executions1)
this.filter('fastest').map('name')
console.log('Fastest is ' + this.filter('fastest').map('name'))
})
// run async
.run({ async: true })
}
-
-process.on('SIGKILL', () => {
- fixedPool.destroy()
- externalPool.destroy()
-})
--- /dev/null
+const FixedThreadPool = require('../lib/fixed')
+const DynamicThreadPool = require('../lib/dynamic')
+const Pool = require('worker-threads-pool')
+const tasks = 1000
+const size = 10
+
+// pools
+const externalPool = new Pool({ max: size })
+const fixedPool = new FixedThreadPool(size, './yourWorker.js', { maxTasks: 10000 })
+const dynamicPool = new DynamicThreadPool(size, size * 2, './yourWorker.js', { maxTasks: 10000 })
+
+// data
+const workerData = { proof: 'ok' }
+
+// fixed pool proof
+async function fixedTest () {
+ let executions = 0
+ const time = Date.now()
+ for (let i = 0; i < tasks; i++) {
+ await fixedPool.execute(workerData)
+ executions++
+ }
+ console.log(`Fixed pool take ${Date.now() - time} to work on ${executions} tasks`)
+}
+
+async function dynamicTest () {
+ let executions = 0
+ const time = Date.now()
+ for (let i = 0; i < tasks; i++) {
+ await dynamicPool.execute(workerData)
+ executions++
+ }
+ console.log(`Dynamic pool take ${Date.now() - time} to work on ${executions} tasks`)
+}
+
+async function externalPoolTest () {
+ let executions = 0
+ const time = Date.now()
+ for (let i = 0; i < tasks; i++) {
+ await new Promise((resolve, reject) => {
+ externalPool.acquire('./externalWorker.js', { workerData: workerData }, (err, worker) => {
+ if (err) {
+ return reject(err)
+ }
+ worker.on('error', reject)
+ worker.on('message', res => {
+ executions++
+ resolve(res)
+ })
+ })
+ })
+ }
+ console.log(`External pool take ${Date.now() - time} to work on ${executions} tasks`)
+}
+
+async function test () {
+ await fixedTest()
+ await dynamicTest()
+ await externalPoolTest()
+}
+
+test()
const DynamicThreadPool = require('../lib/dynamic')
let resolved = 0
let maxReached = 0
-const pool = new DynamicThreadPool(100, 200, './yourWorker.js', { errorHandler: (e) => console.error(e), onlineHandler: () => console.log('worker is online') })
+const pool = new DynamicThreadPool(10, 20, './yourWorker.js', { errorHandler: (e) => console.error(e), onlineHandler: () => console.log('worker is online') })
pool.emitter.on('FullPool', () => maxReached++)
const start = Date.now()
+++ /dev/null
-/**
- * Contains utility functions
- * @author Alessandro Pio Ardizio
- * @since 0.0.1
- */
-
-const uuid = require('uuid/v1')
-/**
- * Return an id to be associated to a node.
- */
-module.exports.generateID = () => {
- return uuid()
-}
"url": "https://github.com/pioardi/node-thread-pool/issues"
},
"homepage": "https://github.com/pioardi/node-thread-pool#readme",
- "dependencies": {
- "uuid": "^3.4.0"
- },
"devDependencies": {
"benchmark": "^2.1.4",
"coveralls": "^3.0.9",
+++ /dev/null
-const expect = require('expect')
-const { generateID } = require('../lib/util')
-
-describe('Utility Tests ', () => {
- it('Generate an id', () => {
- const res = generateID()
- expect(res).toBeTruthy()
- expect(typeof res).toBe('string')
- })
-})