1 const { randomInt } = require('node:crypto')
9 const { TaskFunctions } = require('./benchmarks-types.cjs')
11 const jsonIntegerSerialization = n => {
12 for (let i = 0; i < n; i++) {
22 * @param {number} n - The number of fibonacci numbers to generate.
23 * @returns {number} - The nth fibonacci number.
25 const fibonacci = n => {
34 // cluster worker do not support BigInt
35 return current.toString()
39 * @param {number} n - The number to calculate the factorial of.
40 * @returns {number} - The factorial of n.
42 const factorial = n => {
43 if (n === 0 || n === 1) {
48 for (let i = 1n; i <= n; i++) {
51 // cluster worker do not support BigInt
52 return factorial.toString()
56 const readWriteFiles = (
58 baseDirectory = `/tmp/poolifier-benchmarks/${randomInt(281474976710655)}`
60 if (existsSync(baseDirectory) === true) {
61 rmSync(baseDirectory, { recursive: true })
63 mkdirSync(baseDirectory, { recursive: true })
64 for (let i = 0; i < n; i++) {
65 const filePath = `${baseDirectory}/${i}`
66 writeFileSync(filePath, i.toString(), {
70 readFileSync(filePath, 'utf8')
72 rmSync(baseDirectory, { recursive: true })
76 const executeTaskFunction = data => {
77 switch (data.function) {
78 case TaskFunctions.jsonIntegerSerialization:
79 return jsonIntegerSerialization(data.taskSize || 1000)
80 case TaskFunctions.fibonacci:
81 return fibonacci(data.taskSize || 1000)
82 case TaskFunctions.factorial:
83 return factorial(data.taskSize || 1000)
84 case TaskFunctions.readWriteFiles:
85 return readWriteFiles(data.taskSize || 1000)
87 throw new Error('Unknown task function')