Skip to content

Commit a3e839d

Browse files
author
John Doe
committed
refactor: adjust clock helper
1 parent 313a489 commit a3e839d

File tree

2 files changed

+12
-18
lines changed

2 files changed

+12
-18
lines changed

packages/utils/src/lib/clock-epoch.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ export function epochClock(init: EpochClockOptions = {}) {
4141
msToUs(timeOriginMs + perfMs);
4242

4343
const fromEntryStartTimeMs = fromPerfMs;
44+
const fromEntry = (entry: PerformanceEntry, asEnd = false) => {
45+
return defaultClock.fromPerfMs(
46+
entry.startTime +
47+
(entry.entryType === 'measure' && asEnd ? entry.duration : 0),
48+
);
49+
};
4450
const fromDateNowMs = fromEpochMs;
4551

4652
return {
@@ -55,6 +61,7 @@ export function epochClock(init: EpochClockOptions = {}) {
5561
fromEpochMs,
5662
fromEpochUs,
5763
fromPerfMs,
64+
fromEntry,
5865
fromEntryStartTimeMs,
5966
fromDateNowMs,
6067
};

packages/utils/src/lib/trace-file-utils.ts

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import os from 'node:os';
2-
import {
3-
type PerformanceEntry,
4-
type PerformanceMark,
5-
type PerformanceMeasure,
6-
performance,
7-
} from 'node:perf_hooks';
2+
import { type PerformanceMark, type PerformanceMeasure } from 'node:perf_hooks';
83
import { threadId } from 'node:worker_threads';
94
import { defaultClock } from './clock-epoch.js';
105
import type {
@@ -20,15 +15,6 @@ import type {
2015
TraceEventContainer,
2116
} from './trace-file.type.js';
2217

23-
export const entryToTraceTimestamp = (
24-
entry: PerformanceEntry,
25-
asEnd = false,
26-
): number =>
27-
defaultClock.fromPerfMs(
28-
entry.startTime +
29-
(entry.entryType === 'measure' && asEnd ? entry.duration : 0),
30-
);
31-
3218
// eslint-disable-next-line functional/no-let
3319
let id2Count = 0;
3420
export const nextId2 = () => ({ local: `0x${++id2Count}` });
@@ -147,6 +133,7 @@ export const getSpan = (opt: {
147133
// spans: ======== |======|
148134
// marks: | |
149135
const pad = opt.tsMarkerPadding ?? 1;
136+
// b|e need to share the same id2
150137
const id2 = opt.id2 ?? nextId2();
151138

152139
return [
@@ -170,7 +157,7 @@ export const markToInstantEvent = (
170157
getInstantEvent({
171158
...opt,
172159
name: opt?.name ?? entry.name,
173-
ts: defaultClock.fromEntryStartTimeMs(entry.startTime),
160+
ts: defaultClock.fromEntry(entry),
174161
args: entry.detail ? { detail: entry.detail } : undefined,
175162
});
176163

@@ -181,8 +168,8 @@ export const measureToSpanEvents = (
181168
getSpan({
182169
...opt,
183170
name: opt?.name ?? entry.name,
184-
tsB: entryToTraceTimestamp(entry),
185-
tsE: entryToTraceTimestamp(entry, true),
171+
tsB: defaultClock.fromEntry(entry),
172+
tsE: defaultClock.fromEntry(entry, true),
186173
args: entry.detail ? { data: { detail: entry.detail } } : undefined,
187174
});
188175

0 commit comments

Comments
 (0)