ecmaVersion: 2020,
sourceType: 'module'
},
- plugins: ['@typescript-eslint', 'prettierx'],
+ plugins: ['@typescript-eslint', 'promise', 'prettierx'],
extends: [
'standard',
'eslint:recommended',
'plugin:import/errors',
'plugin:import/warnings',
'plugin:import/typescript',
+ 'plugin:promise/recommended',
'plugin:prettierx/standardx',
- 'plugin:prettierx/@typescript-eslint',
- 'prettier',
- 'prettier/standard',
- 'prettier/@typescript-eslint'
+ 'plugin:prettierx/@typescript-eslint'
],
rules: {
'no-void': 'off',
overrides: [
{
files: ['*.js'],
+ extends: 'plugin:node/recommended',
rules: {
'@typescript-eslint/no-unused-vars': 'off',
'@typescript-eslint/no-var-requires': 'off'
}
},
{
- files: ['examples/typescript/*.ts'],
+ files: ['examples/typescript/**/*.ts'],
rules: {
'import/no-unresolved': 'off'
}
+ },
+ {
+ files: ['examples/**/*.js'],
+ rules: {
+ 'node/no-missing-require': 'off'
+ }
}
]
}
const Benchmark = require('benchmark')
const suite = new Benchmark.Suite()
-const FixedThreadPool = require('../lib/fixed')
-const DynamicThreadPool = require('../lib/dynamic')
+const { FixedThreadPool } = require('../lib/index')
+const { DynamicThreadPool } = require('../lib/index')
const size = 30
const tasks = 1
+const LIST_FORMATTER = new Intl.ListFormat('en-US', {
+ style: 'long',
+ type: 'conjunction'
+})
+
// pools
-const fixedPool = new FixedThreadPool(size, './yourWorker.js', {
+const fixedPool = new FixedThreadPool(size, './threadWorker.js', {
maxTasks: 10000
})
const dynamicPool = new DynamicThreadPool(
size / 2,
size * 3,
- './yourWorker.js',
+ './threadWorker.js',
{ maxTasks: 10000 }
)
const workerData = { proof: 'ok' }
return new Promise((resolve, reject) => {
let executions = 0
for (let i = 0; i <= tasks; i++) {
- fixedPool.execute(workerData).then(res => {
- executions++
- if (executions === tasks) {
- resolve('FINISH')
- }
- })
+ fixedPool
+ .execute(workerData)
+ .then(res => {
+ executions++
+ if (executions === tasks) {
+ return resolve('FINISH')
+ }
+ return null
+ })
+ .catch(err => {
+ console.error(err)
+ })
}
})
}
return new Promise((resolve, reject) => {
let executions = 0
for (let i = 0; i <= tasks; i++) {
- dynamicPool.execute(workerData).then(res => {
- executions++
- if (executions === tasks) {
- resolve('FINISH')
- }
- })
+ dynamicPool
+ .execute(workerData)
+ .then(res => {
+ executions++
+ if (executions === tasks) {
+ return resolve('FINISH')
+ }
+ return null
+ })
+ .catch(err => console.error(err))
}
})
}
})
// add listeners
.on('cycle', function (event) {
- console.log(String(event.target))
+ console.log(event.target.toString())
})
.on('complete', function () {
- this.filter('fastest').map('name')
- console.log('Fastest is ' + this.filter('fastest').map('name'))
+ console.log(
+ 'Fastest is ' +
+ LIST_FORMATTER.format(this.filter('fastest').map('name'))
+ )
+ // eslint-disable-next-line no-process-exit
+ process.exit()
})
// run async
.run({ async: true })
-const FixedThreadPool = require('../lib/fixed')
-const DynamicThreadPool = require('../lib/dynamic')
-const Pool = require('worker-threads-pool')
+const { FixedThreadPool } = require('../lib/index')
+const { DynamicThreadPool } = require('../lib/index')
+const WorkerThreadsPool = require('worker-threads-pool')
+const workerpool = require('workerpool')
const tasks = 1000
const size = 16
// pools
-const externalPool = new Pool({ max: size })
-const fixedPool = new FixedThreadPool(size, './yourWorker.js', {
+const workerThreadsPool = new WorkerThreadsPool({ max: size })
+const workerPool = workerpool.pool('./workerpoolWorker.js', {
+ minWorkers: size / 2,
+ maxWorkers: size * 3,
+ workerType: 'thread'
+})
+const fixedPool = new FixedThreadPool(size, './threadWorker.js', {
maxTasks: 10000
})
const dynamicPool = new DynamicThreadPool(
size / 2,
size * 3,
- './yourWorker.js',
+ './threadWorker.js',
{ maxTasks: 10000 }
)
let executions = 0
const time = Date.now()
for (let i = 0; i <= tasks; i++) {
- fixedPool.execute(workerData).then(res => {
- executions++
- if (executions === tasks) {
- console.log(
- `Fixed pool take ${Date.now() - time} to work on ${executions} tasks`
- )
- }
- })
+ fixedPool
+ .execute(workerData)
+ .then(res => {
+ executions++
+ if (executions === tasks) {
+ return console.log(
+ `Fixed pool take ${
+ Date.now() - time
+ }ms to work on ${executions} tasks`
+ )
+ }
+ return null
+ })
+ .catch(err => console.error(err))
}
}
let executions = 0
const time = Date.now()
for (let i = 0; i <= tasks; i++) {
- dynamicPool.execute(workerData).then(res => {
- executions++
- if (executions === tasks) {
- console.log(
- `Dynamic pool take ${
- Date.now() - time
- } to work on ${executions} tasks`
- )
- }
- })
+ dynamicPool
+ .execute(workerData)
+ .then(res => {
+ executions++
+ if (executions === tasks) {
+ return console.log(
+ `Dynamic pool take ${
+ Date.now() - time
+ }ms to work on ${executions} tasks`
+ )
+ }
+ return null
+ })
+ .catch(err => console.error(err))
}
}
-async function externalPoolTest () {
+async function workerThreadsPoolTest () {
let executions = 0
const time = Date.now()
for (let i = 0; i <= tasks; i++) {
new Promise((resolve, reject) => {
- externalPool.acquire(
- './externalWorker.js',
+ workerThreadsPool.acquire(
+ './workerThreadsWorker.js',
{ workerData: workerData },
(err, worker) => {
if (err) {
})
}
)
- }).then(res => {
- if (tasks === executions) {
- console.log(
- `External pool take ${
- Date.now() - time
- } to work on ${executions} tasks`
- )
- }
})
+ .then(res => {
+ if (tasks === executions) {
+ return console.log(
+ `worker threads pool take ${
+ Date.now() - time
+ }ms to work on ${executions} tasks`
+ )
+ }
+ return null
+ })
+ .catch(err => console.error(err))
+ }
+}
+
+async function workerpoolTest () {
+ let executions = 0
+ const time = Date.now()
+ for (let i = 0; i <= tasks; i++) {
+ workerPool
+ .exec('yourFunction', [workerData])
+ .then(res => {
+ executions++
+ return null
+ })
+ .catch(err => console.error(err))
+ .then(res => {
+ if (tasks === executions) {
+ return console.log(
+ `workerpool take ${
+ Date.now() - time
+ }ms to work on ${executions} tasks`
+ )
+ }
+ return null
+ })
+ .catch(err => console.error(err))
}
}
async function test () {
- fixedTest()
- dynamicTest()
- externalPoolTest()
+ await fixedTest()
+ await dynamicTest()
+ await workerThreadsPoolTest()
+ await workerpoolTest()
}
test()
'use strict'
-const { ThreadWorker } = require('../lib/workers')
+const { ThreadWorker } = require('../lib/index')
function yourFunction (data) {
for (let i = 0; i <= 1000; i++) {
--- /dev/null
+const workerpool = require('workerpool')
+
+function yourFunction (data) {
+ for (let i = 0; i <= 1000; i++) {
+ const o = {
+ a: i
+ }
+ JSON.stringify(o)
+ }
+ // console.log('This is the main thread ' + isMainThread)
+ return { ok: 1 }
+}
+
+workerpool.worker({
+ yourFunction: yourFunction
+})
const start = Date.now()
const iterations = 1000
for (let i = 0; i <= iterations; i++) {
- pool.execute({}).then(res => {
- resolved++
- if (resolved === iterations) {
- console.log('Time take is ' + (Date.now() - start))
- console.log('The pool was full for ' + maxReached + ' times')
- }
- })
+ pool
+ .execute({})
+ .then(res => {
+ resolved++
+ if (resolved === iterations) {
+ console.log('Time take is ' + (Date.now() - start))
+ return console.log('The pool was full for ' + maxReached + ' times')
+ }
+ return null
+ })
+ .catch(err => console.error(err))
}
onlineHandler: () => console.log('worker is online')
})
-pool.execute({ fname: 'fn0', input: 'hello' }).then(res => console.log(res))
+pool
+ .execute({ fname: 'fn0', input: 'hello' })
+ .then(res => console.log(res))
+ .catch(err => console.error(err))
pool
.execute({ fname: 'fn1', input: 'multifunction' })
.then(res => console.log(res))
+ .catch(err => console.error(err))
setTimeout(pool.destroy.bind(pool), 3000)
const start = Date.now()
const iterations = 1000
for (let i = 0; i <= iterations; i++) {
- pool.execute({}).then(res => {
- resolved++
- if (resolved === iterations) {
- console.log('Time take is ' + (Date.now() - start))
- }
- })
+ pool
+ .execute({})
+ .then(res => {
+ resolved++
+ if (resolved === iterations) {
+ return console.log('Time take is ' + (Date.now() - start))
+ }
+ return null
+ })
+ .catch(err => console.error(err))
}
"integrity": "sha512-K0Ptm/47OKfQRpNQ2J/oIN/3QYiK6FwW+eJbILhsdxh2WTLdl+30o8aGdTbm5JbffpFFAg/g+zi1E+jvJha5ng==",
"dev": true
},
+ "after-all": {
+ "version": "2.0.2",
+ "resolved": "https://registry.npmjs.org/after-all/-/after-all-2.0.2.tgz",
+ "integrity": "sha1-IDACmO1glLTIXJjnyK1NymKPn3M=",
+ "dev": true,
+ "requires": {
+ "once": "^1.3.0"
+ }
+ },
"aggregate-error": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/aggregate-error/-/aggregate-error-3.1.0.tgz",
"requires": {
"has-flag": "^4.0.0"
}
+ },
+ "workerpool": {
+ "version": "6.0.2",
+ "resolved": "https://registry.npmjs.org/workerpool/-/workerpool-6.0.2.tgz",
+ "integrity": "sha512-DSNyvOpFKrNusaaUwk+ej6cBj1bmhLcBfj80elGk+ZIo5JSkq+unB1dLKEOcNfJDZgjGICfhQ0Q5TbP0PvF4+Q==",
+ "dev": true
}
}
},
"integrity": "sha1-J1hIEIkUVqQXHI0CJkQa3pDLyus=",
"dev": true
},
+ "worker-threads-pool": {
+ "version": "2.0.0",
+ "resolved": "https://registry.npmjs.org/worker-threads-pool/-/worker-threads-pool-2.0.0.tgz",
+ "integrity": "sha512-5dtGbEucee6o5/kQgpyKIUoHGWf8488DP3ihZDJzDIVvH4V+NA6HdBl/I5ckI4yN1NwM68pdZDbrwac1M95mEA==",
+ "dev": true,
+ "requires": {
+ "after-all": "^2.0.2"
+ }
+ },
"workerpool": {
- "version": "6.0.2",
- "resolved": "https://registry.npmjs.org/workerpool/-/workerpool-6.0.2.tgz",
- "integrity": "sha512-DSNyvOpFKrNusaaUwk+ej6cBj1bmhLcBfj80elGk+ZIo5JSkq+unB1dLKEOcNfJDZgjGICfhQ0Q5TbP0PvF4+Q==",
+ "version": "6.1.0",
+ "resolved": "https://registry.npmjs.org/workerpool/-/workerpool-6.1.0.tgz",
+ "integrity": "sha512-toV7q9rWNYha963Pl/qyeZ6wG+3nnsyvolaNUS8+R5Wtw6qJPTxIlOP1ZSvcGhEJw+l3HMMmtiNo9Gl61G4GVg==",
"dev": true
},
"wrap-ansi": {
"scripts": {
"build": "npm run build:clean && tsc",
"build:clean": "rimraf lib",
+ "benchmark": "npm run build && node benchmarks/bench.js",
"test": "npm run build && nyc mocha --exit --timeout 20000 tests/**/*.test.js",
"test:debug": "mocha --inspect-brk --exit tests/**/*.test.js",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"benchmark": "^2.1.4",
"coveralls": "^3.1.0",
"eslint": "^7.19.0",
- "eslint-config-prettier": "^7.2.0",
"eslint-config-standard": "^16.0.2",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-node": "^11.1.0",
"prettier-plugin-organize-imports": "^1.1.1",
"prettierx": "^0.17.0",
"rimraf": "^3.0.2",
- "typescript": "^4.1.3"
+ "typescript": "^4.1.4",
+ "worker-threads-pool": "^2.0.0",
+ "workerpool": "^6.1.0"
},
"engines": {
"node": ">=12.11.0",
.then(res => {
this.parent?.postMessage({ data: res, id: value.id })
this.lastTask = Date.now()
+ return null
})
.catch(e => {
this.parent?.postMessage({ error: e, id: value.id })