Skip to content

Commit

Permalink
cicd fix
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-fabian committed Jul 8, 2024
1 parent c5def66 commit e332ff9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
31 changes: 31 additions & 0 deletions src/__tests__/mocks/add-worker-file.mock.ts
Original file line number Diff line number Diff line change
@@ -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();
9 changes: 5 additions & 4 deletions src/__tests__/thread-job.service.heavy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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);
Expand All @@ -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`);
Expand All @@ -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(
Expand All @@ -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);
Expand Down

0 comments on commit e332ff9

Please sign in to comment.