| 1 | const crypto = require('crypto') |
| 2 | const fs = require('fs') |
| 3 | const { |
| 4 | PoolTypes, |
| 5 | WorkerFunctions, |
| 6 | WorkerTypes |
| 7 | } = require('./benchmarks-types') |
| 8 | const { |
| 9 | DynamicClusterPool, |
| 10 | DynamicThreadPool, |
| 11 | FixedClusterPool, |
| 12 | FixedThreadPool |
| 13 | } = require('../lib') |
| 14 | |
| 15 | async function runTest (pool, { taskExecutions, workerData }) { |
| 16 | return new Promise((resolve, reject) => { |
| 17 | let executions = 0 |
| 18 | for (let i = 1; i <= taskExecutions; i++) { |
| 19 | pool |
| 20 | .execute(workerData) |
| 21 | .then(() => { |
| 22 | ++executions |
| 23 | if (executions === taskExecutions) { |
| 24 | return resolve({ ok: 1 }) |
| 25 | } |
| 26 | return null |
| 27 | }) |
| 28 | .catch(err => { |
| 29 | console.error(err) |
| 30 | return reject(err) |
| 31 | }) |
| 32 | } |
| 33 | }) |
| 34 | } |
| 35 | |
| 36 | function generateRandomInteger (max = Number.MAX_SAFE_INTEGER, min = 0) { |
| 37 | if (max < min || max < 0 || min < 0) { |
| 38 | throw new RangeError('Invalid interval') |
| 39 | } |
| 40 | max = Math.floor(max) |
| 41 | if (min != null && min !== 0) { |
| 42 | min = Math.ceil(min) |
| 43 | return Math.floor(Math.random() * (max - min + 1)) + min |
| 44 | } |
| 45 | return Math.floor(Math.random() * (max + 1)) |
| 46 | } |
| 47 | |
| 48 | function jsonIntegerSerialization (n) { |
| 49 | for (let i = 0; i < n; i++) { |
| 50 | const o = { |
| 51 | a: i |
| 52 | } |
| 53 | JSON.stringify(o) |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Intentionally inefficient implementation. |
| 59 | * @param {number} n - The number of fibonacci numbers to generate. |
| 60 | * @returns {number} - The nth fibonacci number. |
| 61 | */ |
| 62 | function fibonacci (n) { |
| 63 | if (n <= 1) return 1 |
| 64 | return fibonacci(n - 1) + fibonacci(n - 2) |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Intentionally inefficient implementation. |
| 69 | * @param {number} n - The number to calculate the factorial of. |
| 70 | * @returns {number} - The factorial of n. |
| 71 | */ |
| 72 | function factorial (n) { |
| 73 | if (n === 0) { |
| 74 | return 1 |
| 75 | } |
| 76 | return factorial(n - 1) * n |
| 77 | } |
| 78 | |
| 79 | function readWriteFiles ( |
| 80 | n, |
| 81 | baseDirectory = `/tmp/poolifier-benchmarks/${crypto.randomInt( |
| 82 | 281474976710655 |
| 83 | )}` |
| 84 | ) { |
| 85 | if (fs.existsSync(baseDirectory) === true) { |
| 86 | fs.rmSync(baseDirectory, { recursive: true }) |
| 87 | } |
| 88 | fs.mkdirSync(baseDirectory, { recursive: true }) |
| 89 | for (let i = 0; i < n; i++) { |
| 90 | const filePath = `${baseDirectory}/${i}` |
| 91 | fs.writeFileSync(filePath, i.toString(), { |
| 92 | encoding: 'utf8', |
| 93 | flag: 'a' |
| 94 | }) |
| 95 | fs.readFileSync(filePath, 'utf8') |
| 96 | } |
| 97 | fs.rmSync(baseDirectory, { recursive: true }) |
| 98 | } |
| 99 | |
| 100 | function executeWorkerFunction (data) { |
| 101 | switch (data.function) { |
| 102 | case WorkerFunctions.jsonIntegerSerialization: |
| 103 | return jsonIntegerSerialization(data.taskSize || 1000) |
| 104 | case WorkerFunctions.fibonacci: |
| 105 | return fibonacci(data.taskSize || 1000) |
| 106 | case WorkerFunctions.factorial: |
| 107 | return factorial(data.taskSize || 1000) |
| 108 | case WorkerFunctions.readWriteFiles: |
| 109 | return readWriteFiles(data.taskSize || 1000) |
| 110 | default: |
| 111 | throw new Error('Unknown worker function') |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | function buildPool (workerType, poolType, poolSize, poolOptions) { |
| 116 | switch (poolType) { |
| 117 | case PoolTypes.FIXED: |
| 118 | switch (workerType) { |
| 119 | case WorkerTypes.THREAD: |
| 120 | return new FixedThreadPool( |
| 121 | poolSize, |
| 122 | './benchmarks/internal/thread-worker.js', |
| 123 | poolOptions |
| 124 | ) |
| 125 | case WorkerTypes.CLUSTER: |
| 126 | return new FixedClusterPool( |
| 127 | poolSize, |
| 128 | './benchmarks/internal/cluster-worker.js', |
| 129 | poolOptions |
| 130 | ) |
| 131 | } |
| 132 | break |
| 133 | case PoolTypes.DYNAMIC: |
| 134 | switch (workerType) { |
| 135 | case WorkerTypes.THREAD: |
| 136 | return new DynamicThreadPool( |
| 137 | poolSize / 2, |
| 138 | poolSize * 3, |
| 139 | './benchmarks/internal/thread-worker.js', |
| 140 | poolOptions |
| 141 | ) |
| 142 | case WorkerTypes.CLUSTER: |
| 143 | return new DynamicClusterPool( |
| 144 | poolSize / 2, |
| 145 | poolSize * 3, |
| 146 | './benchmarks/internal/cluster-worker.js', |
| 147 | poolOptions |
| 148 | ) |
| 149 | } |
| 150 | break |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | module.exports = { |
| 155 | WorkerFunctions, |
| 156 | buildPool, |
| 157 | executeWorkerFunction, |
| 158 | generateRandomInteger, |
| 159 | readWriteFiles, |
| 160 | runTest |
| 161 | } |