@@ -28,7 +28,7 @@ import { mockProfiler } from '../../../test'
2828import type { BrowserProfilerTrace } from '../../types'
2929import { mockedTrace } from './test-utils/mockedTrace'
3030import { createRumProfiler } from './profiler'
31- import type { ProfilerTrace } from './types'
31+ import type { ProfilerTrace , RUMProfilerConfiguration } from './types'
3232import type { ProfilingContextManager } from './profilingContext'
3333import { startProfilingContext } from './profilingContext'
3434import type { ProfileEventPayload } from './transport/assembly'
@@ -56,7 +56,7 @@ describe('profiler', () => {
5656
5757 let lifeCycle = new LifeCycle ( )
5858
59- function setupProfiler ( currentView ?: ViewHistoryEntry ) {
59+ function setupProfiler ( currentView ?: ViewHistoryEntry , profilerConfigOverrides ?: Partial < RUMProfilerConfiguration > ) {
6060 const sessionManager = createRumSessionManagerMock ( ) . setId ( 'session-id-1' )
6161 lifeCycle = new LifeCycle ( )
6262 const hooks = createHooks ( )
@@ -122,8 +122,8 @@ describe('profiler', () => {
122122 {
123123 sampleIntervalMs : 10 ,
124124 collectIntervalMs : 60000 , // 1min
125- minNumberOfSamples : 0 ,
126125 minProfileDurationMs : 0 ,
126+ ...profilerConfigOverrides ,
127127 }
128128 )
129129 return {
@@ -933,6 +933,66 @@ describe('profiler', () => {
933933 expect ( findTrackedSessionSpy ) . toHaveBeenCalledWith ( expectedStartTime )
934934 } )
935935
936+ describe ( 'discard logic' , ( ) => {
937+ it ( 'should discard profile when duration is below threshold and there are no long tasks' , async ( ) => {
938+ const clock = mockClock ( )
939+ const { profiler } = setupProfiler ( undefined , { minProfileDurationMs : 5000 } )
940+
941+ profiler . start ( )
942+ expect ( profiler . isRunning ( ) ) . toBe ( true )
943+
944+ clock . tick ( 100 )
945+ profiler . stop ( )
946+ expect ( profiler . isStopped ( ) ) . toBe ( true )
947+
948+ await waitNextMicrotask ( )
949+ await waitNextMicrotask ( )
950+
951+ expect ( interceptor . requests . length ) . toBe ( 0 )
952+ } )
953+
954+ it ( 'should send profile when below duration threshold if a long task is present' , async ( ) => {
955+ const clock = mockClock ( )
956+ const { profiler, addLongTask } = setupProfiler ( undefined , { minProfileDurationMs : 5000 } )
957+
958+ profiler . start ( )
959+ expect ( profiler . isRunning ( ) ) . toBe ( true )
960+
961+ addLongTask ( {
962+ id : 'long-task-id' ,
963+ startClocks : clocksNow ( ) ,
964+ duration : 50 as Duration ,
965+ entryType : RumPerformanceEntryType . LONG_ANIMATION_FRAME ,
966+ } )
967+ clock . tick ( 100 )
968+
969+ profiler . stop ( )
970+ expect ( profiler . isStopped ( ) ) . toBe ( true )
971+
972+ await waitNextMicrotask ( )
973+ await waitNextMicrotask ( )
974+
975+ expect ( interceptor . requests . length ) . toBe ( 1 )
976+ } )
977+
978+ it ( 'should send profile when duration threshold is met' , async ( ) => {
979+ const clock = mockClock ( )
980+ const { profiler } = setupProfiler ( undefined , { minProfileDurationMs : 100 } )
981+
982+ profiler . start ( )
983+ expect ( profiler . isRunning ( ) ) . toBe ( true )
984+
985+ clock . tick ( 200 )
986+ profiler . stop ( )
987+ expect ( profiler . isStopped ( ) ) . toBe ( true )
988+
989+ await waitNextMicrotask ( )
990+ await waitNextMicrotask ( )
991+
992+ expect ( interceptor . requests . length ) . toBe ( 1 )
993+ } )
994+ } )
995+
936996 it ( 'should restart profiling when session expires while paused and then renews' , async ( ) => {
937997 const { profiler, profilingContextManager } = setupProfiler ( )
938998
0 commit comments