Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/timer/executionTimer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,48 @@ describe('ExecutionTimer', () => {
const elapsedTime = timer.getElapsedTime(customId);
expect(elapsedTime).toMatch(/5 seconds/);
});

it('should return correct timer details', () => {
const customId = 'testExecution';

jest.spyOn(performance, 'now').mockReturnValueOnce(1000);
timer.start(customId);

jest.spyOn(performance, 'now').mockReturnValueOnce(5000);
timer.stop(customId);

const timeInfo = timer.getInfo(customId);

expect(timeInfo).toBeDefined();
expect(timeInfo.executionId).toBe(customId);
expect(timeInfo.startTime).toBeInstanceOf(Date);
expect(timeInfo.endTime).toBeInstanceOf(Date);
expect(timeInfo.duration).toBe(4000);
expect(timeInfo.elapsedTime).toMatch(/4 seconds/);
});

it('should return default values if timer was not started', () => {
const timeInfo = timer.getInfo('timerForDetails');

expect(timeInfo).toBeDefined();
expect(timeInfo.executionId).toBe('timerForDetails');
expect(timeInfo.startTime).toBeUndefined();
expect(timeInfo.endTime).toBeUndefined();
expect(timeInfo.duration).toBeUndefined();
expect(timeInfo.elapsedTime).toBeUndefined();
});

it('should handle fraction digits for duration', () => {
const customId = 'fractionTest';

jest.spyOn(performance, 'now').mockReturnValueOnce(1000);
timer.start(customId);

jest.spyOn(performance, 'now').mockReturnValueOnce(3500);
timer.stop(customId);

const timeInfo = timer.getInfo(customId, 2);

expect(timeInfo.duration).toBeCloseTo(2500, 2);
});
});
18 changes: 18 additions & 0 deletions src/timer/executionTimer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { TimerDetailsModel } from './models/timer.model';

/**
* A class for measuring the execution time of code blocks.
*/
Expand Down Expand Up @@ -115,4 +117,20 @@

return parts.join(' ');
}

/**
* Gets details of a specific execution timer.
* @param executionId - The timer ID. Defaults to 'default'.
* @param fractionDigits - Decimal places for milliseconds.
* @returns An object containing timer details.
*/
getInfo(executionId: string = 'default', fractionDigits?: number): TimerDetailsModel {

Check warning on line 127 in src/timer/executionTimer.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
return {
executionId,
startTime: this.getStartDate(executionId),
endTime: this.getEndDate(executionId),
duration: this.getDuration(executionId, fractionDigits),
elapsedTime: this.getElapsedTime(executionId, fractionDigits)
};
}
}
8 changes: 8 additions & 0 deletions src/timer/models/timer.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

export interface TimerDetailsModel{
executionId: string;
startTime: Date | undefined;
endTime: Date | undefined;
duration: number | undefined;
elapsedTime: string | undefined;
}
Loading