Skip to content

Commit 64457f3

Browse files
committed
refactor: wip
1 parent 2cec424 commit 64457f3

File tree

2 files changed

+27
-41
lines changed

2 files changed

+27
-41
lines changed

packages/utils/src/lib/profiler/profiler.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,12 @@ type ProfilerMeasureOptions<T extends Record<string, ActionTrackEntryPayload>> =
3535
*
3636
* @template T - Record type defining available track names and their configurations
3737
*/
38-
export type ProfilerOptions<T extends Record<string, ActionTrackEntryPayload>> =
39-
ProfilerMeasureOptions<T>;
38+
export type ProfilerOptions<
39+
T extends Record<string, ActionTrackEntryPayload> = Record<
40+
string,
41+
ActionTrackEntryPayload
42+
>,
43+
> = ProfilerMeasureOptions<T>;
4044

4145
/**
4246
* Performance profiler that creates structured timing measurements with DevTools visualization.
@@ -127,7 +131,7 @@ export class Profiler<T extends Record<string, ActionTrackEntryPayload>> {
127131
* ]
128132
* });
129133
*/
130-
marker(name: string, opt?: EntryMeta & { color: DevToolsColor }) {
134+
marker(name: string, opt?: EntryMeta & { color?: DevToolsColor }) {
131135
if (!this.#enabled) {
132136
return;
133137
}

packages/utils/src/lib/profiler/profiler.unit.test.ts

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
import { performance } from 'node:perf_hooks';
22
import { beforeEach, describe, expect, it, vi } from 'vitest';
33
import type { ActionTrackEntryPayload } from '../user-timing-extensibility-api.type.js';
4-
import { Profiler } from './profiler.js';
4+
import { Profiler, type ProfilerOptions } from './profiler.js';
55

66
describe('Profiler', () => {
7+
const getProfiler = (overrides?: Partial<ProfilerOptions>) =>
8+
new Profiler({
9+
prefix: 'cp',
10+
track: 'test-track',
11+
...overrides,
12+
});
13+
714
let profiler: Profiler<Record<string, ActionTrackEntryPayload>>;
815

916
beforeEach(() => {
1017
performance.clearMarks();
1118
performance.clearMeasures();
19+
// eslint-disable-next-line functional/immutable-data
1220
delete process.env.CP_PROFILING;
1321

14-
profiler = new Profiler({
15-
prefix: 'cp',
16-
track: 'test-track',
17-
tracks: {},
18-
});
22+
profiler = getProfiler();
1923
});
2024

2125
it('constructor should initialize with default enabled state from env', () => {
2226
vi.stubEnv('CP_PROFILING', 'true');
23-
const profilerWithEnv = new Profiler({
24-
prefix: 'cp',
25-
track: 'test-track',
26-
tracks: {},
27-
});
27+
const profilerWithEnv = getProfiler();
2828

2929
expect(profilerWithEnv.isEnabled()).toBe(true);
3030
});
@@ -34,20 +34,14 @@ describe('Profiler', () => {
3434
const profilerWithOverride = new Profiler({
3535
prefix: 'cp',
3636
track: 'test-track',
37-
tracks: {},
3837
enabled: true,
3938
});
4039

4140
expect(profilerWithOverride.isEnabled()).toBe(true);
4241
});
4342

4443
it('constructor should use defaults for measure', () => {
45-
const customProfiler = new Profiler({
46-
prefix: 'custom',
47-
track: 'custom-track',
48-
trackGroup: 'custom-group',
49-
color: 'secondary',
50-
});
44+
const customProfiler = getProfiler({ color: 'secondary' });
5145

5246
customProfiler.setEnabled(true);
5347

@@ -61,23 +55,21 @@ describe('Profiler', () => {
6155
expect(marks).toStrictEqual(
6256
expect.arrayContaining([
6357
expect.objectContaining({
64-
name: 'custom:test-operation:start',
58+
name: 'cp:test-operation:start',
6559
detail: {
6660
devtools: expect.objectContaining({
6761
dataType: 'track-entry',
68-
track: 'custom-track',
69-
trackGroup: 'custom-group',
62+
track: 'test-track',
7063
color: 'secondary',
7164
}),
7265
},
7366
}),
7467
expect.objectContaining({
75-
name: 'custom:test-operation:end',
68+
name: 'cp:test-operation:end',
7669
detail: {
7770
devtools: expect.objectContaining({
7871
dataType: 'track-entry',
79-
track: 'custom-track',
80-
trackGroup: 'custom-group',
72+
track: 'test-track',
8173
color: 'secondary',
8274
}),
8375
},
@@ -86,12 +78,11 @@ describe('Profiler', () => {
8678
);
8779
expect(measures).toStrictEqual([
8880
expect.objectContaining({
89-
name: 'custom:test-operation',
81+
name: 'cp:test-operation',
9082
detail: {
9183
devtools: expect.objectContaining({
9284
dataType: 'track-entry',
93-
track: 'custom-track',
94-
trackGroup: 'custom-group',
85+
track: 'test-track',
9586
color: 'secondary',
9687
}),
9788
},
@@ -186,12 +177,7 @@ describe('Profiler', () => {
186177
it('marker should execute without error when enabled with default color', () => {
187178
performance.clearMarks();
188179

189-
const profilerWithColor = new Profiler({
190-
prefix: 'cp',
191-
track: 'test-track',
192-
color: 'primary',
193-
tracks: {},
194-
});
180+
const profilerWithColor = getProfiler({ color: 'primary' });
195181
profilerWithColor.setEnabled(true);
196182

197183
expect(() => {
@@ -216,11 +202,7 @@ describe('Profiler', () => {
216202
});
217203

218204
it('marker should execute without error when enabled with no default color', () => {
219-
const profilerNoColor = new Profiler({
220-
prefix: 'cp',
221-
track: 'test-track',
222-
tracks: {},
223-
});
205+
const profilerNoColor = getProfiler();
224206
profilerNoColor.setEnabled(true);
225207

226208
expect(() => {

0 commit comments

Comments
 (0)