Skip to content

Commit 959dba5

Browse files
committed
refactor: split profiler base/node
1 parent cee8ccd commit 959dba5

File tree

3 files changed

+4
-1438
lines changed

3 files changed

+4
-1438
lines changed

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

Lines changed: 1 addition & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import { MockTraceEventFileSink } from '../../../mocks/sink.mock.js';
2-
import type { PerformanceEntryEncoder } from '../performance-observer.js';
31
import type { ActionTrackEntryPayload } from '../user-timing-extensibility-api.type.js';
4-
import { NodejsProfiler, Profiler } from './profiler.js';
2+
import { Profiler } from './profiler.js';
53

64
describe('Profiler Integration', () => {
75
let profiler: Profiler<Record<string, ActionTrackEntryPayload>>;
@@ -297,169 +295,3 @@ describe('Profiler Integration', () => {
297295
expect(performance.getEntriesByType('measure')).toHaveLength(0);
298296
});
299297
});
300-
301-
describe('NodeJS Profiler Integration', () => {
302-
const simpleEncoder: PerformanceEntryEncoder<string> = entry => {
303-
if (entry.entryType === 'measure') {
304-
return [`${entry.name}:${entry.duration.toFixed(2)}ms`];
305-
}
306-
return [];
307-
};
308-
309-
let mockSink: MockTraceEventFileSink;
310-
let nodejsProfiler: NodejsProfiler<string>;
311-
312-
beforeEach(() => {
313-
mockSink = new MockTraceEventFileSink();
314-
315-
nodejsProfiler = new NodejsProfiler({
316-
prefix: 'test',
317-
track: 'test-track',
318-
sink: mockSink,
319-
encodePerfEntry: simpleEncoder,
320-
enabled: true,
321-
});
322-
});
323-
324-
it('should initialize with sink opened when enabled', () => {
325-
expect(mockSink.isClosed()).toBeFalse();
326-
expect(nodejsProfiler.isEnabled()).toBeTrue();
327-
expect(mockSink.open).toHaveBeenCalledOnce();
328-
});
329-
330-
it('should create performance entries and write to sink', () => {
331-
expect(nodejsProfiler.measure('test-operation', () => 'success')).toBe(
332-
'success',
333-
);
334-
});
335-
336-
it('should handle async operations', async () => {
337-
await expect(
338-
nodejsProfiler.measureAsync('async-test', async () => {
339-
await new Promise(resolve => setTimeout(resolve, 1));
340-
return 'async-result';
341-
}),
342-
).resolves.toBe('async-result');
343-
});
344-
345-
it('should disable profiling and close sink', () => {
346-
nodejsProfiler.setEnabled(false);
347-
expect(nodejsProfiler.isEnabled()).toBeFalse();
348-
expect(mockSink.isClosed()).toBeTrue();
349-
expect(mockSink.close).toHaveBeenCalledOnce();
350-
351-
expect(nodejsProfiler.measure('disabled-test', () => 'success')).toBe(
352-
'success',
353-
);
354-
355-
expect(mockSink.getWrittenItems()).toHaveLength(0);
356-
});
357-
358-
it('should re-enable profiling correctly', () => {
359-
nodejsProfiler.setEnabled(false);
360-
nodejsProfiler.setEnabled(true);
361-
362-
expect(nodejsProfiler.isEnabled()).toBeTrue();
363-
expect(mockSink.isClosed()).toBeFalse();
364-
expect(mockSink.open).toHaveBeenCalledTimes(2);
365-
366-
expect(nodejsProfiler.measure('re-enabled-test', () => 42)).toBe(42);
367-
});
368-
369-
it('should support custom tracks', () => {
370-
const profilerWithTracks = new NodejsProfiler({
371-
prefix: 'api-server',
372-
track: 'HTTP',
373-
tracks: {
374-
db: { track: 'Database', color: 'secondary' },
375-
cache: { track: 'Cache', color: 'primary' },
376-
},
377-
sink: mockSink,
378-
encodePerfEntry: simpleEncoder,
379-
});
380-
381-
expect(
382-
profilerWithTracks.measure('user-lookup', () => 'user123', {
383-
track: 'cache',
384-
}),
385-
).toBe('user123');
386-
});
387-
388-
it('should capture buffered entries when buffered option is enabled', () => {
389-
const bufferedProfiler = new NodejsProfiler({
390-
prefix: 'buffered-test',
391-
track: 'Test',
392-
sink: mockSink,
393-
encodePerfEntry: simpleEncoder,
394-
captureBufferedEntries: true,
395-
enabled: true,
396-
});
397-
398-
const bufferedStats = bufferedProfiler.stats;
399-
expect(bufferedStats.state).toBe('running');
400-
expect(bufferedStats.walOpen).toBeTrue();
401-
expect(bufferedStats.isSubscribed).toBeTrue();
402-
expect(bufferedStats.queued).toBe(0);
403-
expect(bufferedStats.dropped).toBe(0);
404-
expect(bufferedStats.written).toBe(0);
405-
406-
bufferedProfiler.setEnabled(false);
407-
});
408-
409-
it('should return correct getStats with dropped and written counts', () => {
410-
const statsProfiler = new NodejsProfiler({
411-
prefix: 'stats-test',
412-
track: 'Stats',
413-
sink: mockSink,
414-
encodePerfEntry: simpleEncoder,
415-
maxQueueSize: 2,
416-
flushThreshold: 2,
417-
enabled: true,
418-
});
419-
420-
expect(statsProfiler.measure('test-op', () => 'result')).toBe('result');
421-
422-
const stats = statsProfiler.stats;
423-
expect(stats.state).toBe('running');
424-
expect(stats.walOpen).toBeTrue();
425-
expect(stats.isSubscribed).toBeTrue();
426-
expect(typeof stats.queued).toBe('number');
427-
expect(typeof stats.dropped).toBe('number');
428-
expect(typeof stats.written).toBe('number');
429-
430-
statsProfiler.setEnabled(false);
431-
});
432-
433-
it('should provide comprehensive queue statistics via getStats', () => {
434-
const profiler = new NodejsProfiler({
435-
prefix: 'stats-profiler',
436-
track: 'Stats',
437-
sink: mockSink,
438-
encodePerfEntry: simpleEncoder,
439-
maxQueueSize: 3,
440-
flushThreshold: 2,
441-
enabled: true,
442-
});
443-
444-
const initialStats = profiler.stats;
445-
expect(initialStats.state).toBe('running');
446-
expect(initialStats.walOpen).toBeTrue();
447-
expect(initialStats.isSubscribed).toBeTrue();
448-
expect(initialStats.queued).toBe(0);
449-
expect(initialStats.dropped).toBe(0);
450-
expect(initialStats.written).toBe(0);
451-
452-
profiler.measure('operation-1', () => 'result1');
453-
profiler.measure('operation-2', () => 'result2');
454-
profiler.flush();
455-
expect(profiler.stats.written).toBe(0);
456-
457-
profiler.setEnabled(false);
458-
459-
const finalStats = profiler.stats;
460-
expect(finalStats.state).toBe('idle');
461-
expect(finalStats.walOpen).toBeFalse();
462-
expect(finalStats.isSubscribed).toBeFalse();
463-
expect(finalStats.queued).toBe(0);
464-
});
465-
});

0 commit comments

Comments
 (0)