@@ -4,7 +4,7 @@ import nodePath from "node:path";
44import { getPercentile } from "./utils" ;
55
66export type FetchBenchmark = {
7- callDurationsMs : number [ ] ;
7+ iterationsMs : number [ ] ;
88 averageMs : number ;
99 p90Ms : number ;
1010} ;
@@ -15,6 +15,17 @@ export type BenchmarkingResults = {
1515 fetchBenchmark : FetchBenchmark ;
1616} [ ] ;
1717
18+ type BenchmarkFetchOptions = {
19+ numberOfIterations ?: number ;
20+ maxRandomDelayMs ?: number ;
21+ fetch : ( deploymentUrl : string ) => Promise < Response > ;
22+ } ;
23+
24+ const defaultOptions : Required < Omit < BenchmarkFetchOptions , "fetch" > > = {
25+ numberOfIterations : 20 ,
26+ maxRandomDelayMs : 15_000 ,
27+ } ;
28+
1829/**
1930 * Benchmarks the response time of an application end-to-end by:
2031 * - building the application
@@ -40,17 +51,6 @@ export async function benchmarkApplicationResponseTime({
4051 return benchmarkFetch ( deploymentUrl , { fetch } ) ;
4152}
4253
43- type BenchmarkFetchOptions = {
44- numberOfCalls ?: number ;
45- maxRandomDelayMs ?: number ;
46- fetch : ( deploymentUrl : string ) => Promise < Response > ;
47- } ;
48-
49- const defaultOptions : Required < Omit < BenchmarkFetchOptions , "fetch" > > = {
50- numberOfCalls : 20 ,
51- maxRandomDelayMs : 15_000 ,
52- } ;
53-
5454/**
5555 * Benchmarks a fetch operation by running it multiple times and computing the average time (in milliseconds) such fetch operation takes.
5656 *
@@ -71,23 +71,23 @@ async function benchmarkFetch(url: string, options: BenchmarkFetchOptions): Prom
7171 return postTimeMs - preTimeMs ;
7272 } ;
7373
74- const callDurationsMs = await Promise . all (
75- new Array ( options ?. numberOfCalls ?? defaultOptions . numberOfCalls ) . fill ( null ) . map ( async ( ) => {
74+ const resolvedOptions = { ...defaultOptions , ...options } ;
75+
76+ const iterationsMs = await Promise . all (
77+ new Array ( resolvedOptions . numberOfIterations ) . fill ( null ) . map ( async ( ) => {
7678 // let's add a random delay before we make the fetch
77- await nodeTimesPromises . setTimeout (
78- Math . round ( Math . random ( ) * ( options ?. maxRandomDelayMs ?? defaultOptions . maxRandomDelayMs ) )
79- ) ;
79+ await nodeTimesPromises . setTimeout ( Math . round ( Math . random ( ) * resolvedOptions . maxRandomDelayMs ) ) ;
8080
8181 return benchmarkFetchCall ( ) ;
8282 } )
8383 ) ;
8484
85- const averageMs = callDurationsMs . reduce ( ( time , sum ) => sum + time ) / callDurationsMs . length ;
85+ const averageMs = iterationsMs . reduce ( ( time , sum ) => sum + time ) / iterationsMs . length ;
8686
87- const p90Ms = getPercentile ( callDurationsMs , 90 ) ;
87+ const p90Ms = getPercentile ( iterationsMs , 90 ) ;
8888
8989 return {
90- callDurationsMs ,
90+ iterationsMs ,
9191 averageMs,
9292 p90Ms,
9393 } ;
0 commit comments