Performance improvement with round robin algorithm
authoraardizio <alessandroardizio94@gmail.com>
Fri, 17 Jan 2020 19:03:07 +0000 (20:03 +0100)
committeraardizio <alessandroardizio94@gmail.com>
Fri, 17 Jan 2020 19:03:07 +0000 (20:03 +0100)
.gitignore [new file with mode: 0644]
fixed.js
proof.js

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..2ac23b4
--- /dev/null
@@ -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
index efae2cee150a3f36aa9a118ce614f0219b8ed368..9b47484e7137b6752450e407b0863b2d34e92152 100644 (file)
--- 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]
+    }
+  }
 }
 
 /**
index daf55240418fffabb96901d5c2eab79403ca830a..b62e5ba5b9cc15af8cbf05dd1bc1f7a0697b26f9 100644 (file)
--- 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))
     }
-  } )
+  })
 }