Skip to content

Commit 9fce5e5

Browse files
committed
refactor: wip
1 parent 6a83e93 commit 9fce5e5

File tree

11 files changed

+46
-17
lines changed

11 files changed

+46
-17
lines changed

packages/utils/eslint.config.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ export default tseslint.config(
1313
},
1414
},
1515
{
16-
files: ['packages/utils/src/lib/**/wal*.ts'],
16+
files: [
17+
'packages/utils/src/lib/**/wal*.ts',
18+
'packages/utils/src/lib/**/wal*.test.ts',
19+
'packages/utils/src/lib/profiler/*.test.ts',
20+
],
1721
rules: {
1822
'n/no-sync': 'off',
1923
},

packages/utils/mocks/multiprocess-profiling/profiler-worker-child.mjs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ import {
55
performDummyWork,
66
} from './utils.js';
77

8-
(async () => {
9-
await createBufferedEvents();
8+
await createBufferedEvents();
109

11-
const profiler = new NodejsProfiler(getProfilerConfig());
10+
const profiler = new NodejsProfiler(getProfilerConfig());
1211

13-
await performDummyWork(profiler);
12+
await performDummyWork(profiler);
1413

15-
profiler.close();
16-
})();
14+
profiler.close();

packages/utils/mocks/multiprocess-profiling/profiler-worker.mjs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ const [numProcesses] = process.argv.slice(2);
88

99
if (!numProcesses) {
1010
console.error('Usage: node profiler-worker.mjs <numProcesses>');
11+
// eslint-disable-next-line unicorn/no-process-exit,n/no-process-exit
1112
process.exit(1);
1213
}
1314

1415
const numProcs = Number.parseInt(numProcesses, 10);
15-
if (isNaN(numProcs) || numProcs < 1) {
16+
if (Number.isNaN(numProcs) || numProcs < 1) {
1617
console.error('numProcesses must be a positive integer');
18+
// eslint-disable-next-line unicorn/no-process-exit,n/no-process-exit
1719
process.exit(1);
1820
}
1921

@@ -52,6 +54,7 @@ try {
5254
});
5355

5456
profiler.close();
57+
// eslint-disable-next-line no-console
5558
console.log(JSON.stringify(profiler.stats, null, 2));
5659
} catch (error) {
5760
// Ensure profiler is closed and stats are output even on error
@@ -60,11 +63,14 @@ try {
6063
}
6164
// Output stats if profiler was initialized, otherwise exit with error
6265
if (profiler) {
66+
// eslint-disable-next-line no-console
6367
console.log(JSON.stringify(profiler.stats, null, 2));
6468
// Exit successfully since we've output the stats that the test needs
69+
// eslint-disable-next-line unicorn/no-process-exit,n/no-process-exit
6570
process.exit(0);
6671
} else {
6772
console.error('Failed to initialize profiler:', error);
73+
// eslint-disable-next-line unicorn/no-process-exit,n/no-process-exit
6874
process.exit(1);
6975
}
7076
}

packages/utils/mocks/multiprocess-profiling/utils.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import { NodejsProfiler } from '../../src/lib/profiler/profiler-node.js';
2-
import type { NodejsProfilerOptions } from '../../src/lib/profiler/profiler-node.js';
1+
import {
2+
NodejsProfiler,
3+
type NodejsProfilerOptions,
4+
} from '../../src/lib/profiler/profiler-node.js';
35
import { entryToTraceEvents } from '../../src/lib/profiler/trace-file-utils.js';
46
import type { TraceEvent } from '../../src/lib/profiler/trace-file.type.js';
57
import { traceEventWalFormat } from '../../src/lib/profiler/wal-json-trace.js';
@@ -70,6 +72,7 @@ export async function performDummyWork(
7072
// Random number of intervals (1-3) - reduced from 2-5
7173
const numIntervals = Math.floor(Math.random() * 3) + 1;
7274

75+
// eslint-disable-next-line functional/no-loop-statements
7376
for (let interval = 0; interval < numIntervals; interval++) {
7477
// Random interval delay (25-100ms)
7578
const intervalDelay = Math.floor(Math.random() * 75) + 25;
@@ -78,6 +81,7 @@ export async function performDummyWork(
7881
// Random number of work packages per interval (1-3)
7982
const numWorkPackages = Math.floor(Math.random() * 3) + 1;
8083

84+
// eslint-disable-next-line functional/no-loop-statements
8185
for (let pkg = 0; pkg < numWorkPackages; pkg++) {
8286
// Random work size (100-2,500,000 elements)
8387
const workSize = Math.floor(Math.random() * 2_500_000);

packages/utils/mocks/omit-trace-json.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,11 @@ export async function loadAndOmitTraceJson(
125125
traceEvents: normalizedEvents,
126126
};
127127
if (container.displayTimeUnit) {
128+
// eslint-disable-next-line functional/immutable-data
128129
result.displayTimeUnit = container.displayTimeUnit;
129130
}
130131
if (container.metadata) {
132+
// eslint-disable-next-line functional/immutable-data
131133
result.metadata = normalizeMetadata(container.metadata);
132134
}
133135
// Validate that the result can be serialized to valid JSON
@@ -251,9 +253,11 @@ function normalizeAndFormatEventsArray(
251253
const uniqueLocalIds = new Set<string>();
252254
const timestamps: number[] = [];
253255

256+
// eslint-disable-next-line functional/no-loop-statements
254257
for (const event of decodedEvents) {
255258
if (event.pid != null) uniquePids.add(event.pid);
256259
if (event.tid != null) uniqueTids.add(event.tid);
260+
// eslint-disable-next-line functional/immutable-data
257261
timestamps.push(event.ts);
258262
if (event.id2?.local && typeof event.id2.local === 'string') {
259263
uniqueLocalIds.add(event.id2.local);
@@ -294,6 +298,7 @@ function normalizeAndFormatEventsArray(
294298

295299
// Handle args normalization
296300
if (event.args?.data && typeof event.args.data === 'object') {
301+
// eslint-disable-next-line functional/immutable-data
297302
normalized.args = {
298303
...event.args,
299304
data: {
@@ -316,6 +321,7 @@ function normalizeAndFormatEventsArray(
316321
};
317322
} else if (event.args) {
318323
// Preserve args if it exists and has other properties
324+
// eslint-disable-next-line functional/immutable-data
319325
normalized.args = event.args;
320326
}
321327
// If args is undefined or doesn't exist, don't include it
@@ -405,6 +411,7 @@ export async function loadNormalizedTraceJsonl(
405411
* @param container - Trace event container to validate
406412
*/
407413
export function expectTraceDecodable(container: TraceEventContainer): void {
414+
// eslint-disable-next-line functional/no-loop-statements
408415
for (const event of container.traceEvents) {
409416
if (event.cat === 'blink.user_timing') {
410417
expect(() => decodeEvent(event)).not.toThrow();

packages/utils/src/lib/profiler/profiler-node.int.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ describe('NodeJS Profiler Integration', () => {
6767
debug: options.debug ?? false,
6868
measureName: options.measureName,
6969
});
70+
// eslint-disable-next-line functional/immutable-data
7071
activeProfilers.push(profiler);
7172
return profiler;
7273
}
@@ -190,11 +191,13 @@ describe('NodeJS Profiler Integration', () => {
190191
});
191192

192193
afterEach(() => {
194+
// eslint-disable-next-line functional/no-loop-statements
193195
for (const profiler of activeProfilers) {
194196
if (profiler.stats.profilerState !== 'closed') {
195197
profiler.close();
196198
}
197199
}
200+
// eslint-disable-next-line functional/immutable-data
198201
activeProfilers.length = 0;
199202

200203
vi.stubEnv(PROFILER_ENABLED_ENV_VAR, undefined!);
@@ -420,8 +423,9 @@ describe('NodeJS Profiler Integration', () => {
420423
const processDuration = performance.now() - processStartTime;
421424

422425
if (!stdout.trim()) {
426+
const stderrMessage = stderr ? ` stderr: ${stderr}` : '';
423427
throw new Error(
424-
`Worker process produced no stdout output.${stderr ? ` stderr: ${stderr}` : ''}`,
428+
`Worker process produced no stdout output.${stderrMessage}`,
425429
);
426430
}
427431

@@ -465,6 +469,7 @@ describe('NodeJS Profiler Integration', () => {
465469
const totalDuration = performance.now() - startTime;
466470

467471
// Log timing information for debugging
472+
// eslint-disable-next-line no-console
468473
console.log(
469474
`[Timing] Process execution: ${processDuration.toFixed(2)}ms, Validation: ${validationDuration.toFixed(2)}ms, Total: ${totalDuration.toFixed(2)}ms`,
470475
);

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ describe('NodejsProfiler', () => {
378378
expect(shardPath).toContainPath('tmp/profiles/custom-filename');
379379
expect(shardPath).toMatch(/trace\.\d{8}-\d{6}-\d{3}(?:\.\d+){3}\.jsonl$/);
380380
// finalFilePath uses measureName as the identifier
381-
expect(profiler.stats.finalFilePath).toBe(
381+
expect(profiler.stats.finalFilePath).toMatchPath(
382382
`${PROFILER_PERSIST_OUT_DIR}/custom-filename/trace.custom-filename.json`,
383383
);
384384
});
@@ -441,7 +441,9 @@ describe('NodejsProfiler', () => {
441441
isCoordinator: true, // When no coordinator env var is set, this profiler becomes coordinator
442442
isFinalized: false,
443443
isCleaned: false,
444-
finalFilePath: `${PROFILER_PERSIST_OUT_DIR}/stats-getter/trace.stats-getter.json`,
444+
finalFilePath: expect.pathToMatch(
445+
`${PROFILER_PERSIST_OUT_DIR}/stats-getter/trace.stats-getter.json`,
446+
),
445447
shardFileCount: 0,
446448
shardFiles: [],
447449
shardOpen: false,

packages/utils/src/lib/profiler/wal-json-trace.unit.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ describe('traceEventCodec', () => {
207207

208208
it('should handle multiple round-trips correctly', () => {
209209
let current = instantEvent;
210+
// eslint-disable-next-line functional/no-loop-statements
210211
for (let i = 0; i < 3; i++) {
211212
const encoded = traceEventCodec.encode(current);
212213
const decoded = traceEventCodec.decode(encoded);

packages/utils/src/lib/wal-sharded.int.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ describe('ShardedWal Integration', () => {
8282
groupId: 'merge-shards',
8383
});
8484

85+
// eslint-disable-next-line functional/no-loop-statements
8586
for (let i = 1; i <= 5; i++) {
8687
const shard = shardedWal.shard();
8788
shard.open();

packages/utils/src/lib/wal-sharded.unit.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe('ShardedWal', () => {
2626
vol.reset();
2727
vol.fromJSON({}, MEMFS_VOLUME);
2828
// Clear coordinator env var for fresh state
29-
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
29+
// eslint-disable-next-line functional/immutable-data, @typescript-eslint/no-dynamic-delete
3030
delete process.env[PROFILER_SHARDER_ID_ENV_VAR];
3131
});
3232

@@ -228,7 +228,7 @@ describe('ShardedWal', () => {
228228
});
229229

230230
// Ensure no coordinator is set
231-
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
231+
// eslint-disable-next-line functional/immutable-data, @typescript-eslint/no-dynamic-delete
232232
delete process.env[PROFILER_SHARDER_ID_ENV_VAR];
233233

234234
const sw = getShardedWal({
@@ -249,7 +249,7 @@ describe('ShardedWal', () => {
249249
});
250250

251251
// Ensure no coordinator is set
252-
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
252+
// eslint-disable-next-line functional/immutable-data, @typescript-eslint/no-dynamic-delete
253253
delete process.env[PROFILER_SHARDER_ID_ENV_VAR];
254254

255255
const sw = getShardedWal({
@@ -443,7 +443,7 @@ describe('ShardedWal', () => {
443443
});
444444

445445
// Not coordinator - cleanupIfCoordinator should be no-op
446-
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
446+
// eslint-disable-next-line functional/immutable-data, @typescript-eslint/no-dynamic-delete
447447
delete process.env[PROFILER_SHARDER_ID_ENV_VAR];
448448
sw.cleanupIfCoordinator();
449449
expect(vol.toJSON()).not.toStrictEqual({});

0 commit comments

Comments
 (0)