From: aardizio Date: Fri, 17 Jan 2020 19:03:07 +0000 (+0100) Subject: Performance improvement with round robin algorithm X-Git-Tag: v0.0.1~47 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=3e460d6d082e6b398aa9cf58694ea5ef83d6571c;p=poolifier.git Performance improvement with round robin algorithm --- diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..2ac23b4d --- /dev/null +++ b/.gitignore @@ -0,0 +1,45 @@ +# 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 diff --git a/fixed.js b/fixed.js index efae2cee..9b47484e 100644 --- a/fixed.js +++ b/fixed.js @@ -3,8 +3,6 @@ const { 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 @@ -24,6 +22,7 @@ class FixedThreadPool { 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) @@ -40,8 +39,7 @@ class FixedThreadPool { 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) @@ -61,12 +59,19 @@ class FixedThreadPool { }) }) } -} -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] + } + } } /** diff --git a/proof.js b/proof.js index daf55240..b62e5ba5 100644 --- a/proof.js +++ b/proof.js @@ -1,9 +1,11 @@ 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 @@ -11,8 +13,8 @@ for (let i = 0; i <= iterations; 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)) } - } ) + }) }