-
Notifications
You must be signed in to change notification settings - Fork 14
refactor: implement epoch clock logic #1204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
ef3cea0
refactor: impl clock logic
fc19fcc
refactor: fix lint
3fe62f1
refactor: fix format
0e7a06e
refactor: fix lint
8e684b2
Merge branch 'main' into feat/utils/clock
69bee31
refactor: add mock restore
db25345
refactor: add mock clear
e33cc3b
refactor: adjust timeOrigin logic
26a851a
refactor: remove hasTimeorigin
f4848b8
refactor: fix unit-test in CI
173134e
refactor: fix unit-test ci 2
0b1f335
Update clock-epoch.unit.test.ts
BioPhoton 8c21478
Update process.setup-file.ts
BioPhoton f192614
refactor: add int-test
03a3886
Update clock-epoch.int.test.ts
BioPhoton 277d329
Update clock-epoch.int.test.ts
BioPhoton 952d8b7
Update clock-epoch.int.test.ts
BioPhoton 9487a3f
Update clock-epoch.unit.test.ts
BioPhoton aaac230
refactor: adjust unit test
7d02d56
refactor: adjust types
04a8655
refactor: fix timing range test issue in CI
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| import process from 'node:process'; | ||
| import { threadId } from 'node:worker_threads'; | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { defaultClock, epochClock } from './clock-epoch.js'; | ||
|
|
||
| describe('epochClock', () => { | ||
| it('should create epoch clock with defaults', () => { | ||
| const c = epochClock(); | ||
| expect(c).toStrictEqual( | ||
| expect.objectContaining({ | ||
| tid: threadId, | ||
| pid: process.pid, | ||
| timeOriginMs: performance.timeOrigin, | ||
| }), | ||
| ); | ||
| expect(c.fromEpochMs).toBeFunction(); | ||
| expect(c.fromEpochUs).toBeFunction(); | ||
| expect(c.fromPerfMs).toBeFunction(); | ||
| expect(c.fromEntryStartTimeMs).toBeFunction(); | ||
| expect(c.fromDateNowMs).toBeFunction(); | ||
| }); | ||
|
|
||
| it('should support performance clock by default for epochNowUs', () => { | ||
| const c = epochClock(); | ||
| expect(c.timeOriginMs).toBe(performance.timeOrigin); | ||
| const nowUs = c.epochNowUs(); | ||
| expect(nowUs).toBe(Math.round(nowUs)); | ||
| const expectedUs = Date.now() * 1000; | ||
|
|
||
| expect(nowUs).toBeWithin(expectedUs - 2000, expectedUs + 1000); | ||
| }); | ||
|
|
||
| it('should convert epoch milliseconds to microseconds correctly', () => { | ||
| const c = epochClock(); | ||
| const epochMs = Date.now(); | ||
|
|
||
| const result = c.fromEpochMs(epochMs); | ||
| expect(result).toBeInteger(); | ||
| expect(result).toBe(Math.round(epochMs * 1000)); | ||
| }); | ||
|
|
||
| it('should convert epoch microseconds to microseconds correctly', () => { | ||
| const c = epochClock(); | ||
| const epochUs = Date.now() * 1000; | ||
| const expectedUs = Math.round(epochUs); | ||
|
|
||
| const result = c.fromEpochUs(epochUs); | ||
| expect(result).toBe(expectedUs); | ||
| expect(result).toBe(Math.round(result)); | ||
| }); | ||
|
|
||
| it('should convert performance milliseconds to epoch microseconds correctly', () => { | ||
| const c = epochClock(); | ||
| const perfMs = performance.now(); | ||
| const expectedUs = Math.round((c.timeOriginMs + perfMs) * 1000); | ||
|
|
||
| const result = c.fromPerfMs(perfMs); | ||
| expect(result).toBe(expectedUs); | ||
| expect(result).toBeInteger(); | ||
| }); | ||
|
|
||
| it('should convert entry start time milliseconds to epoch microseconds correctly', () => { | ||
| const c = epochClock(); | ||
| const entryStartMs = performance.mark('fromPerfMs').startTime; | ||
| const expectedUs = Math.round((c.timeOriginMs + entryStartMs) * 1000); | ||
|
|
||
| const result = c.fromEntryStartTimeMs(entryStartMs); | ||
| expect(result).toBe(expectedUs); | ||
| expect(result).toBe(Math.round(result)); | ||
| }); | ||
|
|
||
| it('should convert Date.now milliseconds to epoch microseconds correctly', () => { | ||
| const c = epochClock(); | ||
| const dateNowMs = Date.now(); | ||
|
|
||
| const result = c.fromDateNowMs(dateNowMs); | ||
| expect(result).toBe(Math.round(dateNowMs * 1000)); | ||
| expect(result).toBe(Math.round(result)); | ||
| }); | ||
| }); | ||
|
|
||
| describe('defaultClock', () => { | ||
| it('should have valid defaultClock export', () => { | ||
| const c = defaultClock; | ||
| expect(c).toStrictEqual( | ||
| expect.objectContaining({ | ||
| tid: threadId, | ||
| pid: process.pid, | ||
| timeOriginMs: performance.timeOrigin, | ||
| }), | ||
| ); | ||
|
|
||
| expect(c.fromEpochMs).toBeFunction(); | ||
| expect(c.fromEpochUs).toBeFunction(); | ||
| expect(c.fromPerfMs).toBeFunction(); | ||
| expect(c.fromEntryStartTimeMs).toBeFunction(); | ||
| expect(c.fromDateNowMs).toBeFunction(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import process from 'node:process'; | ||
| import { threadId } from 'node:worker_threads'; | ||
|
|
||
| export type Microseconds = number; | ||
| export type Milliseconds = number; | ||
|
|
||
| const msToUs = (ms: number): Microseconds => Math.round(ms * 1000); | ||
| const usToUs = (us: number): Microseconds => Math.round(us); | ||
|
|
||
| /** | ||
| * Defines clock utilities for time conversions. | ||
| * Handles time origins in NodeJS and the Browser | ||
| * Provides process and thread IDs. | ||
| * @param init | ||
| */ | ||
| export type EpochClockOptions = { | ||
| pid?: number; | ||
| tid?: number; | ||
| }; | ||
|
|
||
| /** | ||
| * Creates epoch-based clock utility. | ||
| * Epoch time has been the time since January 1, 1970 (UNIX epoch). | ||
| * Date.now gives epoch time in milliseconds. | ||
| * performance.now() + performance.timeOrigin when available is used for higher precision. | ||
| */ | ||
| export function epochClock(init: EpochClockOptions = {}) { | ||
| const pid = init.pid ?? process.pid; | ||
| const tid = init.tid ?? threadId; | ||
|
|
||
| const timeOriginMs = performance.timeOrigin; | ||
|
|
||
| const epochNowUs = (): Microseconds => | ||
| msToUs(timeOriginMs + performance.now()); | ||
|
|
||
| const fromEpochUs = usToUs; | ||
|
|
||
| const fromEpochMs = msToUs; | ||
|
|
||
| const fromPerfMs = (perfMs: Milliseconds): Microseconds => | ||
| msToUs(timeOriginMs + perfMs); | ||
|
|
||
| const fromEntryStartTimeMs = fromPerfMs; | ||
| const fromDateNowMs = fromEpochMs; | ||
|
|
||
| return { | ||
| timeOriginMs, | ||
| pid, | ||
| tid, | ||
|
|
||
| epochNowUs, | ||
| msToUs, | ||
| usToUs, | ||
|
|
||
| fromEpochMs, | ||
| fromEpochUs, | ||
| fromPerfMs, | ||
| fromEntryStartTimeMs, | ||
| fromDateNowMs, | ||
| }; | ||
| } | ||
|
|
||
| export const defaultClock = epochClock(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { defaultClock, epochClock } from './clock-epoch.js'; | ||
|
|
||
| describe('epochClock', () => { | ||
| it('should create epoch clock with defaults', () => { | ||
| const c = epochClock(); | ||
| expect(c.timeOriginMs).toBe(500_000); | ||
| expect(c.tid).toBe(2); | ||
| expect(c.pid).toBe(10_001); | ||
| expect(c.fromEpochMs).toBeFunction(); | ||
| expect(c.fromEpochUs).toBeFunction(); | ||
| expect(c.fromPerfMs).toBeFunction(); | ||
| expect(c.fromEntryStartTimeMs).toBeFunction(); | ||
| expect(c.fromDateNowMs).toBeFunction(); | ||
| }); | ||
|
|
||
| it('should use pid options', () => { | ||
| expect(epochClock({ pid: 999 })).toStrictEqual( | ||
| expect.objectContaining({ | ||
| pid: 999, | ||
| }), | ||
| ); | ||
| }); | ||
|
|
||
| it('should use tid options', () => { | ||
| expect(epochClock({ tid: 888 })).toStrictEqual( | ||
| expect.objectContaining({ | ||
| tid: 888, | ||
| }), | ||
| ); | ||
| }); | ||
|
|
||
| it('should support performance clock by default for epochNowUs', () => { | ||
| const c = epochClock(); | ||
| expect(c.timeOriginMs).toBe(500_000); | ||
| expect(c.epochNowUs()).toBe(1_000_000_000); // timeOrigin + (Date.now() - timeOrigin) = Date.now() | ||
| }); | ||
|
|
||
| it.each([ | ||
| [1_000_000_000, 1_000_000_000], | ||
| [1_001_000_000, 1_001_000_000], | ||
| [999_000_000, 999_000_000], | ||
| ])('should convert epoch microseconds to microseconds', (us, result) => { | ||
| const c = epochClock(); | ||
| expect(c.fromEpochUs(us)).toBe(result); | ||
| }); | ||
|
|
||
| it.each([ | ||
| [1_000_000, 1_000_000_000], | ||
| [1_001_000.5, 1_001_000_500], | ||
| [999_000.4, 999_000_400], | ||
| ])('should convert epoch milliseconds to microseconds', (ms, result) => { | ||
| const c = epochClock(); | ||
| expect(c.fromEpochMs(ms)).toBe(result); | ||
| }); | ||
|
|
||
| it.each([ | ||
| [0, 500_000_000], | ||
| [1000, 501_000_000], | ||
| ])( | ||
| 'should convert performance milliseconds to microseconds', | ||
| (perfMs, expected) => { | ||
| expect(epochClock().fromPerfMs(perfMs)).toBe(expected); | ||
| }, | ||
| ); | ||
|
|
||
| it('should convert entry start time to microseconds', () => { | ||
| const c = epochClock(); | ||
| expect([ | ||
| c.fromEntryStartTimeMs(0), | ||
| c.fromEntryStartTimeMs(1000), | ||
| ]).toStrictEqual([c.fromPerfMs(0), c.fromPerfMs(1000)]); | ||
| }); | ||
|
|
||
| it('should convert Date.now() milliseconds to microseconds', () => { | ||
| const c = epochClock(); | ||
| expect([ | ||
| c.fromDateNowMs(1_000_000), | ||
| c.fromDateNowMs(2_000_000), | ||
| ]).toStrictEqual([1_000_000_000, 2_000_000_000]); | ||
| }); | ||
|
|
||
| it('should maintain conversion consistency', () => { | ||
| const c = epochClock(); | ||
|
|
||
| expect({ | ||
| fromEpochUs_2B: c.fromEpochUs(2_000_000_000), | ||
| fromEpochMs_2M: c.fromEpochMs(2_000_000), | ||
| fromEpochUs_1B: c.fromEpochUs(1_000_000_000), | ||
| fromEpochMs_1M: c.fromEpochMs(1_000_000), | ||
| }).toStrictEqual({ | ||
| fromEpochUs_2B: 2_000_000_000, | ||
| fromEpochMs_2M: 2_000_000_000, | ||
| fromEpochUs_1B: 1_000_000_000, | ||
| fromEpochMs_1M: 1_000_000_000, | ||
| }); | ||
| }); | ||
|
|
||
| it.each([ | ||
| [1_000_000_000.1, 1_000_000_000], | ||
| [1_000_000_000.4, 1_000_000_000], | ||
| [1_000_000_000.5, 1_000_000_001], | ||
| [1_000_000_000.9, 1_000_000_001], | ||
| ])('should round microseconds correctly', (value, result) => { | ||
| const c = epochClock(); | ||
| expect(c.fromEpochUs(value)).toBe(result); | ||
| }); | ||
| }); | ||
|
|
||
| describe('defaultClock', () => { | ||
| it('should have valid defaultClock export', () => { | ||
| expect({ | ||
| tid: typeof defaultClock.tid, | ||
| timeOriginMs: typeof defaultClock.timeOriginMs, | ||
| }).toStrictEqual({ | ||
| tid: 'number', | ||
| timeOriginMs: 'number', | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import { type MockInstance, afterEach, beforeEach, vi } from 'vitest'; | ||
|
|
||
| const MOCK_DATE_NOW_MS = 1_000_000; | ||
| const MOCK_TIME_ORIGIN = 500_000; | ||
|
|
||
| const dateNow = MOCK_DATE_NOW_MS; | ||
| const performanceTimeOrigin = MOCK_TIME_ORIGIN; | ||
|
|
||
| /* eslint-disable functional/no-let */ | ||
| let dateNowSpy: MockInstance<[], number> | undefined; | ||
| let performanceNowSpy: MockInstance<[], number> | undefined; | ||
| /* eslint-enable functional/no-let */ | ||
|
|
||
| beforeEach(() => { | ||
| dateNowSpy = vi.spyOn(Date, 'now').mockReturnValue(dateNow); | ||
| performanceNowSpy = vi | ||
| .spyOn(performance, 'now') | ||
| .mockReturnValue(dateNow - performanceTimeOrigin); | ||
|
|
||
| vi.stubGlobal('performance', { | ||
| ...performance, | ||
| timeOrigin: performanceTimeOrigin, | ||
| }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.unstubAllGlobals(); | ||
|
|
||
| if (dateNowSpy) { | ||
| dateNowSpy.mockRestore(); | ||
| dateNowSpy = undefined; | ||
| } | ||
| if (performanceNowSpy) { | ||
| performanceNowSpy.mockRestore(); | ||
| performanceNowSpy = undefined; | ||
| } | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import process from 'node:process'; | ||
| import { afterEach, beforeEach, vi } from 'vitest'; | ||
|
|
||
| export const MOCK_PID = 10_001; | ||
| export const MOCK_TID = 2; | ||
|
|
||
| vi.mock('node:worker_threads', () => ({ | ||
| get threadId() { | ||
| return MOCK_TID; | ||
| }, | ||
| })); | ||
|
|
||
| const processMock = vi.spyOn(process, 'pid', 'get'); | ||
|
|
||
| beforeEach(() => { | ||
| processMock.mockReturnValue(MOCK_PID); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| processMock.mockClear(); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.