--- /dev/null
+# Logs
+logs
+*.log
+npm-debug.log*
+
+# Runtime data
+pids
+*.pid
+*.seed
+
+# Directory for instrumented libs generated by jscoverage/JSCover
+lib-cov
+
+# Coverage directory used by tools like istanbul
+coverage
+
+# nyc test coverage
+.nyc_output
+
+# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
+.grunt
+
+# node-waf configuration
+.lock-wscript
+
+# Compiled binary addons (http://nodejs.org/api/addons.html)
+build/Release
+
+# Dependency directories
+node_modules
+jspm_packages
+
+# Optional npm cache directory
+.npm
+
+# Optional REPL history
+.node_repl_history
+
+# Project specifics
+.vscode/
+
+# Mac
+.DS_Store/
+
+.history/
\ No newline at end of file
Worker, isMainThread, MessageChannel
} = require('worker_threads')
-console.log('Fixed required')
-
// FixedThreadPool , TrampolineThreadPool
/**
* A thread pool with a static number of threads , is possible to execute tasks in sync or async mode as you prefer
this.numThreads = numThreads
this.workers = []
this.task = task
+ this.nextWorker = 0
for (let i = 1; i <= numThreads; i++) {
const worker = new Worker(__filename)
this.workers.push(worker)
execute (task, data) {
// TODO select a worker
// configure worker to handle message with the specified task
- const idx = chooseWorker(0, this.numThreads - 1)
- const worker = this.workers[idx]
+ const worker = this._chooseWorker()
const id = generateID()
const res = this._execute(worker, task, id)
worker.emitter.emit(id, data)
})
})
}
-}
-function chooseWorker (min, max) {
- min = Math.ceil(min)
- max = Math.floor(max)
- return Math.floor(Math.random() * (max - min + 1)) + min
+ _chooseWorker () {
+ console.log(this.workers.length -1)
+ console.log(this.nextWorker)
+ if ((this.workers.length - 1) === this.nextWorker) {
+ console.log('we are here')
+ this.nextWorker = 0
+ return this.workers[this.nextWorker]
+ } else {
+ this.nextWorker++
+ return this.workers[this.nextWorker]
+ }
+ }
}
/**
const FixedThreadPool = require('./fixed')
let resolved = 0
-const pool = new FixedThreadPool(100)
-let start = Date.now()
-const iterations = 5000
+const pool = new FixedThreadPool(10)
+
+const start = Date.now()
+const iterations = 100000
+
for (let i = 0; i <= iterations; i++) {
const o = {
a: i
pool.execute(JSON.stringify, o).then(res => {
console.log(res)
resolved++
- if(resolved === iterations) {
- console.log('Time take is ' + (Date.now() - start))
+ if (resolved === iterations) {
+ console.log('Time take is ' + (Date.now() - start))
}
- } )
+ })
}