From 66f0c14c082ad6013dd1ca8e2a45a61c8295bc23 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Thu, 28 Mar 2024 19:30:54 +0100 Subject: [PATCH] perf: switch benchmarks to factorial(50000) MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- benchmarks/benchmarks-utils.cjs | 25 +++++++++++----- benchmarks/internal/bench.mjs | 4 +-- .../express-cluster/package.json | 2 +- .../express-cluster/src/worker.ts | 18 ++++++++---- .../express-hybrid/package.json | 2 +- .../src/request-handler-worker.ts | 18 ++++++++---- .../express-worker_threads/package.json | 2 +- .../express-worker_threads/src/worker.ts | 18 ++++++++---- .../fastify-cluster/package.json | 2 +- .../fastify-cluster/src/worker.ts | 16 ++++++---- .../fastify-hybrid/package.json | 2 +- .../src/request-handler-worker.ts | 18 ++++++++---- .../fastify-worker_threads/package.json | 2 +- .../fastify-worker_threads/src/worker.ts | 18 ++++++++---- .../ws-cluster/requests.js | 2 +- .../ws-cluster/src/worker.ts | 29 ++++++++++++------- .../ws-hybrid/requests.js | 2 +- .../ws-hybrid/src/request-handler-worker.ts | 14 ++++++--- .../ws-hybrid/src/websocket-server-worker.ts | 11 ++++--- .../ws-worker_threads/requests.js | 2 +- .../ws-worker_threads/src/main.ts | 11 ++++--- .../ws-worker_threads/src/worker.ts | 14 ++++++--- tests/pools/cluster/dynamic.test.mjs | 2 +- tests/pools/cluster/fixed.test.mjs | 2 +- tests/pools/thread/dynamic.test.mjs | 2 +- tests/pools/thread/fixed.test.mjs | 2 +- tests/test-utils.cjs | 23 ++++++++++----- 27 files changed, 178 insertions(+), 85 deletions(-) diff --git a/benchmarks/benchmarks-utils.cjs b/benchmarks/benchmarks-utils.cjs index b4a0d9bd..c2de0d5e 100644 --- a/benchmarks/benchmarks-utils.cjs +++ b/benchmarks/benchmarks-utils.cjs @@ -197,25 +197,36 @@ const jsonIntegerSerialization = n => { } /** - * Intentionally inefficient implementation. * @param {number} n - The number of fibonacci numbers to generate. * @returns {number} - The nth fibonacci number. */ const fibonacci = n => { - if (n <= 1) return n - return fibonacci(n - 1) + fibonacci(n - 2) + n = BigInt(n) + let current = 1n + let previous = 0n + while (--n) { + const tmp = current + current += previous + previous = tmp + } + return current } /** - * Intentionally inefficient implementation. * @param {number} n - The number to calculate the factorial of. * @returns {number} - The factorial of n. */ const factorial = n => { - if (n === 0) { - return 1 + if (n === 0 || n === 1) { + return 1n + } else { + n = BigInt(n) + let factorial = 1n + for (let i = 1n; i <= n; i++) { + factorial *= i + } + return factorial } - return factorial(n - 1) * n } const readWriteFiles = ( diff --git a/benchmarks/internal/bench.mjs b/benchmarks/internal/bench.mjs index b59514d2..62eefe64 100644 --- a/benchmarks/internal/bench.mjs +++ b/benchmarks/internal/bench.mjs @@ -11,8 +11,8 @@ import { runPoolifierPoolBenchmark } from '../benchmarks-utils.cjs' const poolSize = availableParallelism() const taskExecutions = 1 const workerData = { - function: TaskFunctions.jsonIntegerSerialization, - taskSize: 1000 + function: TaskFunctions.factorial, + taskSize: 50000 } // FixedThreadPool diff --git a/examples/typescript/http-server-pool/express-cluster/package.json b/examples/typescript/http-server-pool/express-cluster/package.json index fe37e826..ddbb5d6c 100644 --- a/examples/typescript/http-server-pool/express-cluster/package.json +++ b/examples/typescript/http-server-pool/express-cluster/package.json @@ -15,7 +15,7 @@ "start:esm": "node --enable-source-maps dist/main.js", "test": "echo \"Error: no test specified\" && exit 1", "benchmark:echo": "autocannon -c 100 -d 30 -p 10 -m POST -H Content-Type=application/json -b '{\"key1\":\"value1\", \"key2\":\"value2\"}' http://localhost:8080/api/echo", - "benchmark:factorial": "autocannon -c 100 -d 30 -p 10 http://localhost:8080/api/factorial/30" + "benchmark:factorial": "autocannon -c 100 -d 30 -p 10 http://localhost:8080/api/factorial/50000" }, "keywords": [], "author": "", diff --git a/examples/typescript/http-server-pool/express-cluster/src/worker.ts b/examples/typescript/http-server-pool/express-cluster/src/worker.ts index e04900ab..da2e9ad7 100644 --- a/examples/typescript/http-server-pool/express-cluster/src/worker.ts +++ b/examples/typescript/http-server-pool/express-cluster/src/worker.ts @@ -9,11 +9,17 @@ import type { WorkerData, WorkerResponse } from './types.js' class ExpressWorker extends ClusterWorker { private static server: Server - private static readonly factorial = (n: number): number => { - if (n === 0) { - return 1 + private static readonly factorial = (n: number | bigint): bigint => { + if (n === 0 || n === 1) { + return 1n + } else { + n = BigInt(n) + let factorial = 1n + for (let i = 1n; i <= n; i++) { + factorial *= i + } + return factorial } - return ExpressWorker.factorial(n - 1) * n } private static readonly startExpress = ( @@ -32,7 +38,9 @@ class ExpressWorker extends ClusterWorker { application.get('/api/factorial/:number', (req: Request, res: Response) => { const { number } = req.params - res.send({ number: ExpressWorker.factorial(parseInt(number)) }).end() + res + .send({ number: ExpressWorker.factorial(parseInt(number)).toString() }) + .end() }) ExpressWorker.server = application.listen(port, () => { diff --git a/examples/typescript/http-server-pool/express-hybrid/package.json b/examples/typescript/http-server-pool/express-hybrid/package.json index a478e731..9b1df361 100644 --- a/examples/typescript/http-server-pool/express-hybrid/package.json +++ b/examples/typescript/http-server-pool/express-hybrid/package.json @@ -15,7 +15,7 @@ "start:esm": "node --enable-source-maps dist/main.js", "test": "echo \"Error: no test specified\" && exit 1", "benchmark:echo": "autocannon -c 100 -d 30 -p 10 -m POST -H Content-Type=application/json -b '{\"key1\":\"value1\", \"key2\":\"value2\"}' http://localhost:8080/api/echo", - "benchmark:factorial": "autocannon -c 100 -d 30 -p 10 http://localhost:8080/api/factorial/30" + "benchmark:factorial": "autocannon -c 100 -d 30 -p 10 http://localhost:8080/api/factorial/50000" }, "keywords": [], "author": "", diff --git a/examples/typescript/http-server-pool/express-hybrid/src/request-handler-worker.ts b/examples/typescript/http-server-pool/express-hybrid/src/request-handler-worker.ts index d769c762..b7ff8b16 100644 --- a/examples/typescript/http-server-pool/express-hybrid/src/request-handler-worker.ts +++ b/examples/typescript/http-server-pool/express-hybrid/src/request-handler-worker.ts @@ -10,11 +10,17 @@ class RequestHandlerWorker< Data extends ThreadWorkerData, Response extends ThreadWorkerResponse > extends ThreadWorker { - private static readonly factorial = (n: number): number => { - if (n === 0) { - return 1 + private static readonly factorial = (n: number | bigint): bigint => { + if (n === 0 || n === 1) { + return 1n + } else { + n = BigInt(n) + let factorial = 1n + for (let i = 1n; i <= n; i++) { + factorial *= i + } + return factorial } - return RequestHandlerWorker.factorial(n - 1) * n } public constructor () { @@ -25,7 +31,9 @@ class RequestHandlerWorker< factorial: (workerData?: Data) => { return { data: { - number: RequestHandlerWorker.factorial(workerData!.data.number!) + number: RequestHandlerWorker.factorial( + workerData!.data.number! + ).toString() } } as unknown as Response } diff --git a/examples/typescript/http-server-pool/express-worker_threads/package.json b/examples/typescript/http-server-pool/express-worker_threads/package.json index c51231f0..de54766a 100644 --- a/examples/typescript/http-server-pool/express-worker_threads/package.json +++ b/examples/typescript/http-server-pool/express-worker_threads/package.json @@ -15,7 +15,7 @@ "start": "node --enable-source-maps dist/main.js", "test": "echo \"Error: no test specified\" && exit 1", "benchmark:echo": "autocannon -c 100 -d 30 -p 10 -m POST -H Content-Type=application/json -b '{\"key1\":\"value1\", \"key2\":\"value2\"}' http://localhost:8080/api/echo", - "benchmark:factorial": "autocannon -c 100 -d 30 -p 10 http://localhost:8080/api/factorial/30" + "benchmark:factorial": "autocannon -c 100 -d 30 -p 10 http://localhost:8080/api/factorial/50000" }, "keywords": [], "author": "", diff --git a/examples/typescript/http-server-pool/express-worker_threads/src/worker.ts b/examples/typescript/http-server-pool/express-worker_threads/src/worker.ts index c7a64055..18e5c8a3 100644 --- a/examples/typescript/http-server-pool/express-worker_threads/src/worker.ts +++ b/examples/typescript/http-server-pool/express-worker_threads/src/worker.ts @@ -10,11 +10,17 @@ class RequestHandlerWorker< Data extends WorkerData, Response extends WorkerResponse > extends ThreadWorker { - private static readonly factorial: (n: number) => number = n => { - if (n === 0) { - return 1 + private static readonly factorial: (n: number | bigint) => bigint = n => { + if (n === 0 || n === 1) { + return 1n + } else { + n = BigInt(n) + let factorial = 1n + for (let i = 1n; i <= n; i++) { + factorial *= i + } + return factorial } - return RequestHandlerWorker.factorial(n - 1) * n } public constructor () { @@ -25,7 +31,9 @@ class RequestHandlerWorker< factorial: (workerData?: Data) => { return { body: { - number: RequestHandlerWorker.factorial(workerData!.body.number!) + number: RequestHandlerWorker.factorial( + workerData!.body.number! + ).toString() } } as unknown as Response } diff --git a/examples/typescript/http-server-pool/fastify-cluster/package.json b/examples/typescript/http-server-pool/fastify-cluster/package.json index f7192eef..240a054a 100644 --- a/examples/typescript/http-server-pool/fastify-cluster/package.json +++ b/examples/typescript/http-server-pool/fastify-cluster/package.json @@ -15,7 +15,7 @@ "start:esm": "node --enable-source-maps dist/main.js", "test": "echo \"Error: no test specified\" && exit 1", "benchmark:echo": "autocannon -c 100 -d 30 -p 10 -m POST -H Content-Type=application/json -b '{\"key1\":\"value1\", \"key2\":\"value2\"}' http://localhost:8080/api/echo", - "benchmark:factorial": "autocannon -c 100 -d 30 -p 10 http://localhost:8080/api/factorial/30" + "benchmark:factorial": "autocannon -c 100 -d 30 -p 10 http://localhost:8080/api/factorial/50000" }, "keywords": [], "author": "", diff --git a/examples/typescript/http-server-pool/fastify-cluster/src/worker.ts b/examples/typescript/http-server-pool/fastify-cluster/src/worker.ts index 8f4442ed..e491fa8f 100644 --- a/examples/typescript/http-server-pool/fastify-cluster/src/worker.ts +++ b/examples/typescript/http-server-pool/fastify-cluster/src/worker.ts @@ -8,11 +8,17 @@ import type { WorkerData, WorkerResponse } from './types.js' class FastifyWorker extends ClusterWorker { private static fastify: FastifyInstance - private static readonly factorial = (n: number): number => { - if (n === 0) { - return 1 + private static readonly factorial = (n: number | bigint): bigint => { + if (n === 0 || n === 1) { + return 1n + } else { + n = BigInt(n) + let factorial = 1n + for (let i = 1n; i <= n; i++) { + factorial *= i + } + return factorial } - return FastifyWorker.factorial(n - 1) * n } private static readonly startFastify = async ( @@ -32,7 +38,7 @@ class FastifyWorker extends ClusterWorker { Params: { number: number } }>('/api/factorial/:number', request => { const { number } = request.params - return { number: FastifyWorker.factorial(number) } + return { number: FastifyWorker.factorial(number).toString() } }) await FastifyWorker.fastify.listen({ port }) diff --git a/examples/typescript/http-server-pool/fastify-hybrid/package.json b/examples/typescript/http-server-pool/fastify-hybrid/package.json index 75f6b329..785887e4 100644 --- a/examples/typescript/http-server-pool/fastify-hybrid/package.json +++ b/examples/typescript/http-server-pool/fastify-hybrid/package.json @@ -15,7 +15,7 @@ "start:esm": "node --enable-source-maps dist/main.js", "test": "echo \"Error: no test specified\" && exit 1", "benchmark:echo": "autocannon -c 100 -d 30 -p 10 -m POST -H Content-Type=application/json -b '{\"key1\":\"value1\", \"key2\":\"value2\"}' http://localhost:8080/api/echo", - "benchmark:factorial": "autocannon -c 100 -d 30 -p 10 http://localhost:8080/api/factorial/30" + "benchmark:factorial": "autocannon -c 100 -d 30 -p 10 http://localhost:8080/api/factorial/50000" }, "keywords": [], "author": "", diff --git a/examples/typescript/http-server-pool/fastify-hybrid/src/request-handler-worker.ts b/examples/typescript/http-server-pool/fastify-hybrid/src/request-handler-worker.ts index d769c762..b7ff8b16 100644 --- a/examples/typescript/http-server-pool/fastify-hybrid/src/request-handler-worker.ts +++ b/examples/typescript/http-server-pool/fastify-hybrid/src/request-handler-worker.ts @@ -10,11 +10,17 @@ class RequestHandlerWorker< Data extends ThreadWorkerData, Response extends ThreadWorkerResponse > extends ThreadWorker { - private static readonly factorial = (n: number): number => { - if (n === 0) { - return 1 + private static readonly factorial = (n: number | bigint): bigint => { + if (n === 0 || n === 1) { + return 1n + } else { + n = BigInt(n) + let factorial = 1n + for (let i = 1n; i <= n; i++) { + factorial *= i + } + return factorial } - return RequestHandlerWorker.factorial(n - 1) * n } public constructor () { @@ -25,7 +31,9 @@ class RequestHandlerWorker< factorial: (workerData?: Data) => { return { data: { - number: RequestHandlerWorker.factorial(workerData!.data.number!) + number: RequestHandlerWorker.factorial( + workerData!.data.number! + ).toString() } } as unknown as Response } diff --git a/examples/typescript/http-server-pool/fastify-worker_threads/package.json b/examples/typescript/http-server-pool/fastify-worker_threads/package.json index a3c76a71..6f7c35a2 100644 --- a/examples/typescript/http-server-pool/fastify-worker_threads/package.json +++ b/examples/typescript/http-server-pool/fastify-worker_threads/package.json @@ -15,7 +15,7 @@ "start": "node --enable-source-maps dist/main.js", "test": "echo \"Error: no test specified\" && exit 1", "benchmark:echo": "autocannon -c 100 -d 30 -p 10 -m POST -H Content-Type=application/json -b '{\"key1\":\"value1\", \"key2\":\"value2\"}' http://localhost:8080/api/echo", - "benchmark:factorial": "autocannon -c 100 -d 30 -p 10 http://localhost:8080/api/factorial/30" + "benchmark:factorial": "autocannon -c 100 -d 30 -p 10 http://localhost:8080/api/factorial/50000" }, "keywords": [], "author": "", diff --git a/examples/typescript/http-server-pool/fastify-worker_threads/src/worker.ts b/examples/typescript/http-server-pool/fastify-worker_threads/src/worker.ts index c7a64055..18e5c8a3 100644 --- a/examples/typescript/http-server-pool/fastify-worker_threads/src/worker.ts +++ b/examples/typescript/http-server-pool/fastify-worker_threads/src/worker.ts @@ -10,11 +10,17 @@ class RequestHandlerWorker< Data extends WorkerData, Response extends WorkerResponse > extends ThreadWorker { - private static readonly factorial: (n: number) => number = n => { - if (n === 0) { - return 1 + private static readonly factorial: (n: number | bigint) => bigint = n => { + if (n === 0 || n === 1) { + return 1n + } else { + n = BigInt(n) + let factorial = 1n + for (let i = 1n; i <= n; i++) { + factorial *= i + } + return factorial } - return RequestHandlerWorker.factorial(n - 1) * n } public constructor () { @@ -25,7 +31,9 @@ class RequestHandlerWorker< factorial: (workerData?: Data) => { return { body: { - number: RequestHandlerWorker.factorial(workerData!.body.number!) + number: RequestHandlerWorker.factorial( + workerData!.body.number! + ).toString() } } as unknown as Response } diff --git a/examples/typescript/websocket-server-pool/ws-cluster/requests.js b/examples/typescript/websocket-server-pool/ws-cluster/requests.js index 1f302a6c..9beb2b78 100644 --- a/examples/typescript/websocket-server-pool/ws-cluster/requests.js +++ b/examples/typescript/websocket-server-pool/ws-cluster/requests.js @@ -12,7 +12,7 @@ ws.on('open', () => { ) } for (let i = 0; i < 60; i++) { - ws.send(JSON.stringify({ type: 'factorial', data: { number: 30 } })) + ws.send(JSON.stringify({ type: 'factorial', data: { number: 50000 } })) } }) diff --git a/examples/typescript/websocket-server-pool/ws-cluster/src/worker.ts b/examples/typescript/websocket-server-pool/ws-cluster/src/worker.ts index 9ec81eb5..232382f6 100644 --- a/examples/typescript/websocket-server-pool/ws-cluster/src/worker.ts +++ b/examples/typescript/websocket-server-pool/ws-cluster/src/worker.ts @@ -12,11 +12,17 @@ import { class WebSocketServerWorker extends ClusterWorker { private static wss: WebSocketServer - private static readonly factorial = (n: number): number => { - if (n === 0) { - return 1 + private static readonly factorial = (n: number | bigint): bigint => { + if (n === 0 || n === 1) { + return 1n + } else { + n = BigInt(n) + let factorial = 1n + for (let i = 1n; i <= n; i++) { + factorial *= i + } + return factorial } - return WebSocketServerWorker.factorial(n - 1) * n } private static readonly startWebSocketServer = ( @@ -48,12 +54,15 @@ class WebSocketServerWorker extends ClusterWorker { break case MessageType.factorial: ws.send( - JSON.stringify({ - type: MessageType.factorial, - data: { - number: WebSocketServerWorker.factorial(data.number!) - } - }) + JSON.stringify( + { + type: MessageType.factorial, + data: { + number: WebSocketServerWorker.factorial(data.number!) + } + }, + (_, v) => (typeof v === 'bigint' ? v.toString() : v) + ) ) break } diff --git a/examples/typescript/websocket-server-pool/ws-hybrid/requests.js b/examples/typescript/websocket-server-pool/ws-hybrid/requests.js index 1f302a6c..9beb2b78 100644 --- a/examples/typescript/websocket-server-pool/ws-hybrid/requests.js +++ b/examples/typescript/websocket-server-pool/ws-hybrid/requests.js @@ -12,7 +12,7 @@ ws.on('open', () => { ) } for (let i = 0; i < 60; i++) { - ws.send(JSON.stringify({ type: 'factorial', data: { number: 30 } })) + ws.send(JSON.stringify({ type: 'factorial', data: { number: 50000 } })) } }) diff --git a/examples/typescript/websocket-server-pool/ws-hybrid/src/request-handler-worker.ts b/examples/typescript/websocket-server-pool/ws-hybrid/src/request-handler-worker.ts index d769c762..004dd3db 100644 --- a/examples/typescript/websocket-server-pool/ws-hybrid/src/request-handler-worker.ts +++ b/examples/typescript/websocket-server-pool/ws-hybrid/src/request-handler-worker.ts @@ -10,11 +10,17 @@ class RequestHandlerWorker< Data extends ThreadWorkerData, Response extends ThreadWorkerResponse > extends ThreadWorker { - private static readonly factorial = (n: number): number => { - if (n === 0) { - return 1 + private static readonly factorial = (n: number | bigint): bigint => { + if (n === 0 || n === 1) { + return 1n + } else { + n = BigInt(n) + let factorial = 1n + for (let i = 1n; i <= n; i++) { + factorial *= i + } + return factorial } - return RequestHandlerWorker.factorial(n - 1) * n } public constructor () { diff --git a/examples/typescript/websocket-server-pool/ws-hybrid/src/websocket-server-worker.ts b/examples/typescript/websocket-server-pool/ws-hybrid/src/websocket-server-worker.ts index eabf72eb..e52f0c06 100644 --- a/examples/typescript/websocket-server-pool/ws-hybrid/src/websocket-server-worker.ts +++ b/examples/typescript/websocket-server-pool/ws-hybrid/src/websocket-server-worker.ts @@ -78,10 +78,13 @@ ClusterWorkerResponse .execute({ data }, 'factorial') .then(response => { ws.send( - JSON.stringify({ - type: MessageType.factorial, - data: response.data - }) + JSON.stringify( + { + type: MessageType.factorial, + data: response.data + }, + (_, v) => (typeof v === 'bigint' ? v.toString() : v) + ) ) return undefined }) diff --git a/examples/typescript/websocket-server-pool/ws-worker_threads/requests.js b/examples/typescript/websocket-server-pool/ws-worker_threads/requests.js index 1f302a6c..9beb2b78 100644 --- a/examples/typescript/websocket-server-pool/ws-worker_threads/requests.js +++ b/examples/typescript/websocket-server-pool/ws-worker_threads/requests.js @@ -12,7 +12,7 @@ ws.on('open', () => { ) } for (let i = 0; i < 60; i++) { - ws.send(JSON.stringify({ type: 'factorial', data: { number: 30 } })) + ws.send(JSON.stringify({ type: 'factorial', data: { number: 50000 } })) } }) diff --git a/examples/typescript/websocket-server-pool/ws-worker_threads/src/main.ts b/examples/typescript/websocket-server-pool/ws-worker_threads/src/main.ts index f3aea4e5..7a162499 100644 --- a/examples/typescript/websocket-server-pool/ws-worker_threads/src/main.ts +++ b/examples/typescript/websocket-server-pool/ws-worker_threads/src/main.ts @@ -41,10 +41,13 @@ wss.on('connection', ws => { .execute({ data }, 'factorial') .then(response => { ws.send( - JSON.stringify({ - type: MessageType.factorial, - data: response.data - }) + JSON.stringify( + { + type: MessageType.factorial, + data: response.data + }, + (_, v) => (typeof v === 'bigint' ? v.toString() : v) + ) ) return undefined }) diff --git a/examples/typescript/websocket-server-pool/ws-worker_threads/src/worker.ts b/examples/typescript/websocket-server-pool/ws-worker_threads/src/worker.ts index 5aa9e90a..e1b67193 100644 --- a/examples/typescript/websocket-server-pool/ws-worker_threads/src/worker.ts +++ b/examples/typescript/websocket-server-pool/ws-worker_threads/src/worker.ts @@ -10,11 +10,17 @@ class RequestHandlerWorker< Data extends WorkerData, Response extends WorkerResponse > extends ThreadWorker { - private static readonly factorial = (n: number): number => { - if (n === 0) { - return 1 + private static readonly factorial = (n: number | bigint): bigint => { + if (n === 0 || n === 1) { + return 1n + } else { + n = BigInt(n) + let factorial = 1n + for (let i = 1n; i <= n; i++) { + factorial *= i + } + return factorial } - return RequestHandlerWorker.factorial(n - 1) * n } public constructor () { diff --git a/tests/pools/cluster/dynamic.test.mjs b/tests/pools/cluster/dynamic.test.mjs index 09df7318..a60d95fa 100644 --- a/tests/pools/cluster/dynamic.test.mjs +++ b/tests/pools/cluster/dynamic.test.mjs @@ -24,7 +24,7 @@ describe('Dynamic cluster pool test suite', () => { let result = await pool.execute({ function: TaskFunctions.fibonacci }) - expect(result).toBe(75025) + expect(result).toBe(354224848179262000000) result = await pool.execute({ function: TaskFunctions.factorial }) diff --git a/tests/pools/cluster/fixed.test.mjs b/tests/pools/cluster/fixed.test.mjs index fa609cf3..794da1c0 100644 --- a/tests/pools/cluster/fixed.test.mjs +++ b/tests/pools/cluster/fixed.test.mjs @@ -68,7 +68,7 @@ describe('Fixed cluster pool test suite', () => { let result = await pool.execute({ function: TaskFunctions.fibonacci }) - expect(result).toBe(75025) + expect(result).toBe(354224848179262000000) result = await pool.execute({ function: TaskFunctions.factorial }) diff --git a/tests/pools/thread/dynamic.test.mjs b/tests/pools/thread/dynamic.test.mjs index 6f563f52..6cefa0aa 100644 --- a/tests/pools/thread/dynamic.test.mjs +++ b/tests/pools/thread/dynamic.test.mjs @@ -24,7 +24,7 @@ describe('Dynamic thread pool test suite', () => { let result = await pool.execute({ function: TaskFunctions.fibonacci }) - expect(result).toBe(75025) + expect(result).toBe(354224848179262000000) result = await pool.execute({ function: TaskFunctions.factorial }) diff --git a/tests/pools/thread/fixed.test.mjs b/tests/pools/thread/fixed.test.mjs index 3e10a92d..48f19e4d 100644 --- a/tests/pools/thread/fixed.test.mjs +++ b/tests/pools/thread/fixed.test.mjs @@ -68,7 +68,7 @@ describe('Fixed thread pool test suite', () => { let result = await pool.execute({ function: TaskFunctions.fibonacci }) - expect(result).toBe(75025) + expect(result).toBe(354224848179262000000) result = await pool.execute({ function: TaskFunctions.factorial }) diff --git a/tests/test-utils.cjs b/tests/test-utils.cjs index 735d406a..4448eaf6 100644 --- a/tests/test-utils.cjs +++ b/tests/test-utils.cjs @@ -66,25 +66,34 @@ const jsonIntegerSerialization = n => { } /** - * Intentionally inefficient implementation. * @param {number} n - The number of fibonacci numbers to generate. * @returns {number} - The nth fibonacci number. */ const fibonacci = n => { - if (n <= 1) return n - return fibonacci(n - 1) + fibonacci(n - 2) + let current = 1 + let previous = 0 + while (--n) { + const tmp = current + current += previous + previous = tmp + } + return current } /** - * Intentionally inefficient implementation. * @param {number} n - The number to calculate the factorial of. * @returns {number} - The factorial of n. */ const factorial = n => { - if (n === 0) { + if (n === 0 || n === 1) { return 1 + } else { + let factorial = 1 + for (let i = 1; i <= n; i++) { + factorial *= i + } + return factorial } - return factorial(n - 1) * n } const executeTaskFunction = data => { @@ -92,7 +101,7 @@ const executeTaskFunction = data => { case TaskFunctions.jsonIntegerSerialization: return jsonIntegerSerialization(data.n || 100) case TaskFunctions.fibonacci: - return fibonacci(data.n || 25) + return fibonacci(data.n || 100) case TaskFunctions.factorial: return factorial(data.n || 100) default: -- 2.34.1