Skip to content

Commit

Permalink
chore: extract test options for test purposes
Browse files Browse the repository at this point in the history
  • Loading branch information
niieani committed Nov 25, 2024
1 parent ea7e568 commit f5042a9
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 33 deletions.
9 changes: 5 additions & 4 deletions src/v3/ActiveTrace.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
/* eslint-disable @typescript-eslint/consistent-indexed-object-style */
/* eslint-disable max-classes-per-file */
import {
DEFAULT_DEBOUNCE_DURATION,
DEFAULT_INTERACTIVE_TIMEOUT_DURATION,
DEFAULT_TIMEOUT_DURATION,
} from './constants'
import { ensureTimestamp } from './ensureTimestamp'
import {
type CPUIdleLongTaskProcessor,
Expand Down Expand Up @@ -89,10 +94,6 @@ type StatesBase<ScopeT extends ScopeBase> = Record<
type TraceStateMachineSideEffectHandlers<ScopeT extends ScopeBase> =
TraceStateMachine<ScopeT>['sideEffectFns']

const DEFAULT_DEBOUNCE_DURATION = 500
export const DEFAULT_TIMEOUT_DURATION = 45_000
const DEFAULT_INTERACTIVE_TIMEOUT_DURATION = 10_000

type EntryType<ScopeT extends ScopeBase> = PerformanceEntryLike & {
entry: SpanAndAnnotation<ScopeT>
}
Expand Down
3 changes: 3 additions & 0 deletions src/v3/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const DEFAULT_DEBOUNCE_DURATION = 500
export const DEFAULT_TIMEOUT_DURATION = 45_000
export const DEFAULT_INTERACTIVE_TIMEOUT_DURATION = 10_000
1 change: 1 addition & 0 deletions src/v3/ensureMatcherFn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export function ensureMatcherFn<
: fromDefinition(matcherFnOrDefinition)
) as MatcherInputT extends string ? MatcherInputT : SpanMatcherFn<ScopeT>
}

export function convertMatchersToFns<ScopeT extends ScopeBase>(
matchers: SpanMatch<ScopeT>[] | undefined,
): ArrayWithAtLeastOneElement<SpanMatcherFn<ScopeT>> | undefined {
Expand Down
13 changes: 7 additions & 6 deletions src/v3/firstCPUIdle.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const QUIET_WINDOW_DURATION = 2_000 // 2 seconds
const CLUSTER_PADDING = 1_000 // 1 second
const HEAVY_CLUSTER_THRESHOLD = 250 // 250ms
const DEFAULT_QUIET_WINDOW_DURATION = 2_000 // Google used 2 seconds
const DEFAULT_CLUSTER_PADDING = 1_000 // Google used 1 second
const DEFAULT_HEAVY_CLUSTER_THRESHOLD = 250 // Google used 250ms

export interface PerformanceEntryLike {
entryType: string
Expand All @@ -25,8 +25,8 @@ const isLongTask = (entry: PerformanceEntryLike) =>
export function createCPUIdleProcessor<T extends number | PerformanceEntryLike>(
fmpOrEntry: T,
{
clusterPadding = CLUSTER_PADDING,
heavyClusterThreshold = HEAVY_CLUSTER_THRESHOLD,
clusterPadding = DEFAULT_CLUSTER_PADDING,
heavyClusterThreshold = DEFAULT_HEAVY_CLUSTER_THRESHOLD,
getQuietWindowDuration,
}: CPUIdleProcessorOptions = {},
): CPUIdleLongTaskProcessor<T> {
Expand All @@ -51,7 +51,8 @@ export function createCPUIdleProcessor<T extends number | PerformanceEntryLike>(
const entryEndTime = entry.startTime + entry.duration
const isEntryLongTask = isLongTask(entry)
const quietWindowDuration =
getQuietWindowDuration?.(entryEndTime, fmp) ?? QUIET_WINDOW_DURATION
getQuietWindowDuration?.(entryEndTime, fmp) ??
DEFAULT_QUIET_WINDOW_DURATION

// If this is the first long task
if (endTimeStampOfLastLongTask === null) {
Expand Down
8 changes: 4 additions & 4 deletions src/v3/getDynamicQuietWindowDuration.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const INITIAL_QUIET_WINDOW_SIZE = 5_000 // 5 seconds at FMP
const X_SECONDS = 15 // 15 seconds after FMP
const QUITE_WINDOW_SIZE_AT_X = 3_000 // 3 seconds at some specified point in time x
const MINIMUM_QUIET_WINDOW_SIZE = 1_000 // 1 second as t -> infinity
const INITIAL_QUIET_WINDOW_SIZE = 5_000 // Google's value: 5 seconds at FMP
const X_SECONDS = 15 // Google's value: 15 seconds after FMP
const QUITE_WINDOW_SIZE_AT_X = 3_000 // Google's value: 3 seconds at some specified point in time x
const MINIMUM_QUIET_WINDOW_SIZE = 1_000 // Google's value: 1 second as t -> infinity

/**
* Create a function to calculate the dynamic quiet window duration based on configurable decay points.
Expand Down
65 changes: 47 additions & 18 deletions src/v3/traceManager.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import './test/asciiTimelineSerializer'
import { DEFAULT_CAPTURE_INTERACTIVE_TIME } from '../main'
import { DEFAULT_TIMEOUT_DURATION } from './ActiveTrace'
import { DEFAULT_TIMEOUT_DURATION } from './constants'
import { ensureTimestamp } from './ensureTimestamp'
import { createQuietWindowDurationCalculator } from './getDynamicQuietWindowDuration'
import * as matchSpan from './matchSpan'
Expand Down Expand Up @@ -36,6 +36,16 @@ function processCheckSpan(
})
}

const GOOGLES_QUIET_WINDOW_DURATION = 2_000
const GOOGLES_CLUSTER_PADDING = 1_000
const GOOGLES_HEAVY_CLUSTER_THRESHOLD = 250

const cpuIdleProcessorOptions = {
getQuietWindowDuration: () => GOOGLES_QUIET_WINDOW_DURATION,
clusterPadding: GOOGLES_CLUSTER_PADDING,
heavyClusterThreshold: GOOGLES_HEAVY_CLUSTER_THRESHOLD,
}

describe('TraceManager', () => {
let reportFn: jest.Mock
let generateId: jest.Mock
Expand Down Expand Up @@ -591,7 +601,7 @@ describe('TraceManager', () => {
type: 'operation',
requiredScopeKeys: [],
requiredToEnd: [{ name: 'end' }],
captureInteractive: true,
captureInteractive: cpuIdleProcessorOptions,
}
const tracer = traceManager.createTracer(traceDefinition)
const startConfig: StartTraceConfig<ScopeBase> = {
Expand Down Expand Up @@ -635,7 +645,10 @@ describe('TraceManager', () => {
type: 'operation',
requiredScopeKeys: [],
requiredToEnd: [{ name: 'end' }],
captureInteractive: { timeout: interactiveTimeout },
captureInteractive: {
...cpuIdleProcessorOptions,
timeout: interactiveTimeout,
},
}
const tracer = traceManager.createTracer(traceDefinition)
const startConfig: StartTraceConfig<ScopeBase> = {
Expand Down Expand Up @@ -680,7 +693,7 @@ describe('TraceManager', () => {
requiredScopeKeys: [],
requiredToEnd: [matchSpan.withName('end')],
interruptOn: [matchSpan.withName('interrupt')],
captureInteractive: true,
captureInteractive: cpuIdleProcessorOptions,
}
const tracer = traceManager.createTracer(traceDefinition)
const startConfig: StartTraceConfig<ScopeBase> = {
Expand Down Expand Up @@ -727,7 +740,7 @@ describe('TraceManager', () => {
requiredScopeKeys: [],
requiredToEnd: [{ name: 'end' }],
debounceOn: [{ name: 'debounce' }],
captureInteractive: true,
captureInteractive: cpuIdleProcessorOptions,
debounceDuration: 300,
}
const tracer = traceManager.createTracer(traceDefinition)
Expand Down Expand Up @@ -773,7 +786,10 @@ describe('TraceManager', () => {
requiredScopeKeys: [],
requiredToEnd: [{ name: 'end' }],
debounceOn: [{ name: 'debounce' }],
captureInteractive: { timeout: CUSTOM_CAPTURE_INTERACTIVE_TIMEOUT },
captureInteractive: {
...cpuIdleProcessorOptions,
timeout: CUSTOM_CAPTURE_INTERACTIVE_TIMEOUT,
},
debounceDuration: 100,
}
const tracer = traceManager.createTracer(traceDefinition)
Expand Down Expand Up @@ -824,7 +840,11 @@ describe('TraceManager', () => {
type: 'operation',
requiredScopeKeys: [],
requiredToEnd: [{ name: 'end' }],
captureInteractive: { timeout: 100, getQuietWindowDuration },
captureInteractive: {
...cpuIdleProcessorOptions,
timeout: 100,
getQuietWindowDuration,
},
}
const tracer = traceManager.createTracer(traceDefinition)
const startConfig: StartTraceConfig<ScopeBase> = {
Expand Down Expand Up @@ -875,6 +895,7 @@ describe('TraceManager', () => {
requiredScopeKeys: [],
requiredToEnd: [{ name: 'end' }],
captureInteractive: {
...cpuIdleProcessorOptions,
timeout: 1_000,
getQuietWindowDuration: createQuietWindowDurationCalculator(),
},
Expand Down Expand Up @@ -922,7 +943,7 @@ describe('TraceManager', () => {
type: 'operation',
requiredScopeKeys: [],
requiredToEnd: [{ name: 'end' }],
captureInteractive: true,
captureInteractive: cpuIdleProcessorOptions,
}
const tracer = traceManager.createTracer(traceDefinition)
const startConfig: StartTraceConfig<ScopeBase> = {
Expand Down Expand Up @@ -966,7 +987,7 @@ describe('TraceManager', () => {
type: 'operation',
requiredScopeKeys: [],
requiredToEnd: [{ name: 'end' }],
captureInteractive: true,
captureInteractive: cpuIdleProcessorOptions,
}
const tracer = traceManager.createTracer(traceDefinition)
const startConfig: StartTraceConfig<ScopeBase> = {
Expand Down Expand Up @@ -1010,7 +1031,7 @@ describe('TraceManager', () => {
type: 'operation',
requiredScopeKeys: [],
requiredToEnd: [{ name: 'end' }],
captureInteractive: true,
captureInteractive: cpuIdleProcessorOptions,
}
const tracer = traceManager.createTracer(traceDefinition)
const startConfig: StartTraceConfig<ScopeBase> = {
Expand Down Expand Up @@ -1056,7 +1077,7 @@ describe('TraceManager', () => {
type: 'operation',
requiredScopeKeys: [],
requiredToEnd: [{ name: 'end' }],
captureInteractive: true,
captureInteractive: cpuIdleProcessorOptions,
}
const tracer = traceManager.createTracer(traceDefinition)
const startConfig: StartTraceConfig<ScopeBase> = {
Expand All @@ -1068,7 +1089,7 @@ describe('TraceManager', () => {
// prettier-ignore
const { entries } = getEventsFromTimeline`
Events: ${Render('start', 0)}-----${Render('end', 0)}-----${LongTask(200)}-----${LongTask(200)}-----${LongTask(200)}-----${Check}
Time: ${0} ${200} ${300} ${900} ${1500} ${3800}
Time: ${0} ${200} ${300} ${900} ${1_500} ${3_800}
`

processEntries(entries, traceManager)
Expand Down Expand Up @@ -1103,7 +1124,7 @@ describe('TraceManager', () => {
type: 'operation',
requiredScopeKeys: [],
requiredToEnd: [{ name: 'end' }],
captureInteractive: true,
captureInteractive: cpuIdleProcessorOptions,
}
const tracer = traceManager.createTracer(traceDefinition)
const startConfig: StartTraceConfig<ScopeBase> = {
Expand Down Expand Up @@ -1147,7 +1168,7 @@ describe('TraceManager', () => {
type: 'operation',
requiredScopeKeys: [],
requiredToEnd: [{ name: 'end' }],
captureInteractive: true,
captureInteractive: cpuIdleProcessorOptions,
}
const tracer = traceManager.createTracer(traceDefinition)
const startConfig: StartTraceConfig<ScopeBase> = {
Expand All @@ -1160,7 +1181,7 @@ describe('TraceManager', () => {
const { entries } = getEventsFromTimeline`
Events: ${Render('start', 0)}-----${Render('end', 0)}-----${LongTask(200)}-----${LongTask(100)}-----${LongTask(200)}-----${LongTask(200)}-----${Check}
Time: ${0} ${200} ${300} ${600} ${1_700} ${2_900} ${4_650}
}
}
`

processEntries(entries, traceManager)
Expand Down Expand Up @@ -1196,7 +1217,7 @@ describe('TraceManager', () => {
type: 'operation',
requiredScopeKeys: [],
requiredToEnd: [{ name: 'end' }],
captureInteractive: true,
captureInteractive: cpuIdleProcessorOptions,
}
const tracer = traceManager.createTracer(traceDefinition)
const startConfig: StartTraceConfig<ScopeBase> = {
Expand Down Expand Up @@ -1244,7 +1265,7 @@ describe('TraceManager', () => {
type: 'operation',
requiredScopeKeys: [],
requiredToEnd: [{ name: 'end' }],
captureInteractive: true,
captureInteractive: cpuIdleProcessorOptions,
}
const tracer = traceManager.createTracer(traceDefinition)
const startConfig: StartTraceConfig<ScopeBase> = {
Expand Down Expand Up @@ -1294,7 +1315,7 @@ describe('TraceManager', () => {
type: 'operation',
requiredScopeKeys: [],
requiredToEnd: [{ name: 'end' }],
captureInteractive: true,
captureInteractive: cpuIdleProcessorOptions,
}
const tracer = traceManager.createTracer(traceDefinition)
const startConfig: StartTraceConfig<ScopeBase> = {
Expand Down Expand Up @@ -1384,6 +1405,10 @@ describe('TraceManager', () => {
"ticketId": "74",
},
"startTillInteractive": 1504.4000000059605,
"startTime": {
"epoch": 1732230167488.4,
"now": 298438.60000000894,
},
"status": "ok",
"type": "operation",
}
Expand Down Expand Up @@ -1443,6 +1468,10 @@ describe('TraceManager', () => {
"ticketId": "74",
},
"startTillInteractive": 1302.3999999910593,
"startTime": {
"epoch": 1732236012113.3,
"now": 34982.5,
},
"status": "ok",
"type": "operation",
}
Expand Down
2 changes: 1 addition & 1 deletion src/v3/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export interface CaptureInteractiveConfig extends CPUIdleProcessorOptions {
/**
* How long to wait for CPU Idle before giving up.
*/
timeout: number
timeout?: number
}

/**
Expand Down

0 comments on commit f5042a9

Please sign in to comment.