Benchmarks: properly parse env variables
[poolifier.git] / benchmarks / versus-external-pools / functions / function-to-bench.js
index 0c9738e62282d1486ea57141332761a0fd9705e9..2f75d143330a07adb22ff9b7bcbc8a9862ca1d55 100644 (file)
@@ -1,14 +1,35 @@
+const fs = require('fs')
+const {
+  WorkerFunctions,
+  executeWorkerFunction
+  // eslint-disable-next-line n/no-unpublished-require
+} = require('../../benchmarks-utils')
+
+const TaskTypes = {
+  CPU_INTENSIVE: 'CPU_INTENSIVE',
+  IO_INTENSIVE: 'IO_INTENSIVE'
+}
+
 module.exports = function (data) {
-  if ( data.taskType === 'CPU_INTENSIVE' ) {
-    // CPU Intensive task
-    for (let i = 0; i <= 5000; i++) {
-      const o = {
-        a: i
+  data = data || {}
+  data.taskType = data.taskType || TaskTypes.CPU_INTENSIVE
+  data.taskSize = data.taskSize || 5000
+  const benchmarksFilePath = '/tmp/poolifier-benchmarks'
+  switch (data.taskType) {
+    case TaskTypes.CPU_INTENSIVE:
+      // CPU intensive task
+      data.function = data.function || WorkerFunctions.jsonIntegerSerialization
+      executeWorkerFunction(data)
+      return { ok: 1 }
+    case TaskTypes.IO_INTENSIVE:
+      // IO intensive task
+      for (let i = 0; i < data.taskSize; i++) {
+        fs.writeFileSync(benchmarksFilePath, i.toString(), 'utf8')
+        fs.readFileSync(benchmarksFilePath, 'utf8')
+        fs.unlinkSync(benchmarksFilePath)
       }
-      JSON.stringify(o)
-    }
-    return { ok: 1 }
-  } else {
-    throw new Error('Please specify the task type')
+      return { ok: 1 }
+    default:
+      throw new Error(`Unknown task type: ${data.taskType}`)
   }
 }