From e332ff91d86c646439bdca01f85af2ea4ff6bf84 Mon Sep 17 00:00:00 2001 From: tim-fabian Date: Mon, 8 Jul 2024 02:42:49 +0200 Subject: [PATCH] cicd fix --- src/__tests__/mocks/add-worker-file.mock.ts | 31 +++++++++++++++++++ .../thread-job.service.heavy.test.ts | 9 +++--- 2 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 src/__tests__/mocks/add-worker-file.mock.ts diff --git a/src/__tests__/mocks/add-worker-file.mock.ts b/src/__tests__/mocks/add-worker-file.mock.ts new file mode 100644 index 0000000..c31ec26 --- /dev/null +++ b/src/__tests__/mocks/add-worker-file.mock.ts @@ -0,0 +1,31 @@ +/* eslint-disable jsdoc/require-jsdoc */ +import { parentPort, workerData as nodeWorkerData } from 'node:worker_threads'; + +import { BaseWorkerData } from '../../models'; +import { reportCompletion } from '../../services'; + +type AddWorkerData = BaseWorkerData & { + size: number +}; + +const workerData: AddWorkerData | undefined = nodeWorkerData as AddWorkerData | undefined; + +if (!workerData) { + //@ts-ignore-next-line + return; +} + +function generateLargeArray(size: number): number[] { + return Array.from({ length: size }, () => Math.random()); +} + +function calculateSum(arr: number[]): number { + return arr.reduce((acc, num) => acc + num, 0); +} + +const array: number[] = generateLargeArray(workerData.size); +const res: number = calculateSum(array); + +parentPort?.postMessage(`got the final result: ${res}`); + +reportCompletion(); \ No newline at end of file diff --git a/src/__tests__/thread-job.service.heavy.test.ts b/src/__tests__/thread-job.service.heavy.test.ts index c31dd3a..d3e4eba 100644 --- a/src/__tests__/thread-job.service.heavy.test.ts +++ b/src/__tests__/thread-job.service.heavy.test.ts @@ -13,6 +13,7 @@ import { ThreadJobService } from '../services'; const emptyTsWorkerFilePath: string = path.join(__dirname, 'mocks', 'empty-ts-worker-file.mock.js'); const fibonacciTsWorkerFilePath: string = path.join(__dirname, 'mocks', 'fibonacci-ts-worker-file.mock.js'); +const addWorkerFilePath: string = path.join(__dirname, 'mocks', 'add-worker-file.mock.ts'); const osThreads: number = availableParallelism(); @@ -72,7 +73,7 @@ describe('ThreadJobService heavy tasks', () => { }).timeout(40000); it('should have major improved performance', async () => { - const startValue: number = 46; + const size: number = 1e8; const numberOfJobs: number = 4; // run with single core threadJobService = new ThreadJobService(repository, 1, 0); @@ -83,7 +84,7 @@ describe('ThreadJobService heavy tasks', () => { for (let i: number = 0; i < numberOfJobs; i++) { jobs.push( threadJobService.runThreadJob({ - workerData: { filePath: fibonacciTsWorkerFilePath, startValue: startValue }, + workerData: { filePath: addWorkerFilePath, size: size }, onMessage: m => console.log(m), onComplete: () => { console.log('completed the job', i, `in ${performance.measure('total-' + i, 'startSingleThread').duration.toFixed(2)} ms`); @@ -109,7 +110,7 @@ describe('ThreadJobService heavy tasks', () => { for (let i: number = 0; i < numberOfJobs; i++) { multiThreadJobs.push( threadJobService.runThreadJob({ - workerData: { filePath: fibonacciTsWorkerFilePath, startValue: startValue }, + workerData: { filePath: addWorkerFilePath, size: size }, onMessage: m => console.log(m), onComplete: () => { console.log( @@ -130,7 +131,7 @@ describe('ThreadJobService heavy tasks', () => { const singleThreadJobDuration: number = getAverageJobDuration(await Promise.all(jobs)); const multiThreadJobDuration: number = getAverageJobDuration(await Promise.all(multiThreadJobs)); - expect(multiThreadJobDuration * 0.85).to.be.below(singleThreadJobDuration); + expect(multiThreadJobDuration * 0.8).to.be.below(singleThreadJobDuration); const totalWithMultiThread: number = performance.measure('totalMultiThread', 'start', 'end').duration; expect(totalWithMultiThread * (numberOfJobs - 1)).to.be.below(totalWithSingleThread);