Commit | Line | Data |
---|---|---|
4a8cea37 | 1 | const { randomInt } = require('node:crypto') |
4a8cea37 JB |
2 | const { |
3 | existsSync, | |
4 | mkdirSync, | |
5 | readFileSync, | |
6 | rmSync, | |
3a502712 | 7 | writeFileSync, |
4a8cea37 | 8 | } = require('node:fs') |
d35e5717 | 9 | const { TaskFunctions } = require('./benchmarks-types.cjs') |
2d2e32c2 | 10 | |
041dc05b | 11 | const jsonIntegerSerialization = n => { |
cdace0e5 JB |
12 | for (let i = 0; i < n; i++) { |
13 | const o = { | |
3a502712 | 14 | a: i, |
cdace0e5 JB |
15 | } |
16 | JSON.stringify(o) | |
17 | } | |
30b963d4 | 18 | return { ok: 1 } |
cdace0e5 JB |
19 | } |
20 | ||
bdacc2d2 | 21 | /** |
3a502712 JB |
22 | * @param n - The number of fibonacci numbers to generate. |
23 | * @returns - The nth fibonacci number. | |
bdacc2d2 | 24 | */ |
041dc05b | 25 | const fibonacci = n => { |
66f0c14c JB |
26 | n = BigInt(n) |
27 | let current = 1n | |
28 | let previous = 0n | |
29 | while (--n) { | |
30 | const tmp = current | |
31 | current += previous | |
32 | previous = tmp | |
33 | } | |
d83e2353 JB |
34 | // cluster worker do not support BigInt |
35 | return current.toString() | |
bdacc2d2 JB |
36 | } |
37 | ||
7d82d90e | 38 | /** |
3a502712 JB |
39 | * @param n - The number to calculate the factorial of. |
40 | * @returns - The factorial of n. | |
7d82d90e | 41 | */ |
041dc05b | 42 | const factorial = n => { |
66f0c14c JB |
43 | if (n === 0 || n === 1) { |
44 | return 1n | |
45 | } else { | |
46 | n = BigInt(n) | |
47 | let factorial = 1n | |
48 | for (let i = 1n; i <= n; i++) { | |
49 | factorial *= i | |
50 | } | |
d83e2353 JB |
51 | // cluster worker do not support BigInt |
52 | return factorial.toString() | |
7d82d90e JB |
53 | } |
54 | } | |
55 | ||
bac873bd | 56 | const readWriteFiles = ( |
670734fc | 57 | n, |
4a8cea37 | 58 | baseDirectory = `/tmp/poolifier-benchmarks/${randomInt(281474976710655)}` |
bac873bd | 59 | ) => { |
4a8cea37 JB |
60 | if (existsSync(baseDirectory) === true) { |
61 | rmSync(baseDirectory, { recursive: true }) | |
cdace0e5 | 62 | } |
4a8cea37 | 63 | mkdirSync(baseDirectory, { recursive: true }) |
cdace0e5 JB |
64 | for (let i = 0; i < n; i++) { |
65 | const filePath = `${baseDirectory}/${i}` | |
4a8cea37 | 66 | writeFileSync(filePath, i.toString(), { |
cdace0e5 | 67 | encoding: 'utf8', |
3a502712 | 68 | flag: 'a', |
cdace0e5 | 69 | }) |
4a8cea37 | 70 | readFileSync(filePath, 'utf8') |
cdace0e5 | 71 | } |
4a8cea37 | 72 | rmSync(baseDirectory, { recursive: true }) |
30b963d4 | 73 | return { ok: 1 } |
cdace0e5 JB |
74 | } |
75 | ||
ab7bb4f8 | 76 | const executeTaskFunction = data => { |
2d2e32c2 | 77 | switch (data.function) { |
dbca3be9 | 78 | case TaskFunctions.jsonIntegerSerialization: |
d1a9aa41 | 79 | return jsonIntegerSerialization(data.taskSize || 1000) |
dbca3be9 | 80 | case TaskFunctions.fibonacci: |
d1a9aa41 | 81 | return fibonacci(data.taskSize || 1000) |
dbca3be9 | 82 | case TaskFunctions.factorial: |
d1a9aa41 | 83 | return factorial(data.taskSize || 1000) |
dbca3be9 | 84 | case TaskFunctions.readWriteFiles: |
cdace0e5 | 85 | return readWriteFiles(data.taskSize || 1000) |
2d2e32c2 | 86 | default: |
dbca3be9 | 87 | throw new Error('Unknown task function') |
2d2e32c2 JB |
88 | } |
89 | } | |
ab7bb4f8 JB |
90 | |
91 | module.exports = { | |
3a502712 | 92 | executeTaskFunction, |
ab7bb4f8 | 93 | } |