Threadjs benchmarks (#230)
[poolifier.git] / benchmarks / versus-external-pools / threadjs.js
diff --git a/benchmarks/versus-external-pools/threadjs.js b/benchmarks/versus-external-pools/threadjs.js
new file mode 100644 (file)
index 0000000..a70692b
--- /dev/null
@@ -0,0 +1,32 @@
+// IMPORT LIBRARIES
+const { spawn, Thread, Worker } = require('threads')
+// FINISH IMPORT LIBRARIES
+const size = process.env.POOL_SIZE
+const iterations = process.env.NUM_ITERATIONS
+const data = {
+  test: 'MYBENCH',
+  taskType: process.env['TASK_TYPE']
+}
+
+// Threads.js is not really a pool so we need to write few additional code
+const workers = []
+async function poolify () {
+  for (let i = 0; i < size ; i++ ){
+    const worker = await spawn(new Worker("./workers/threadjs/function-to-bench-worker.js"))
+    workers.push(worker)
+  }
+}
+
+
+async function run () {
+  await poolify()
+  const promises = []
+  for (let i = 0; i < iterations; i++) {
+    const worker = workers[(i % size)]
+    promises.push(worker.exposedFunction(data))
+  }
+  await Promise.all(promises)
+  process.exit()
+}
+
+run()