@@ -4,26 +4,26 @@ import nodePath from "node:path";
44import { getPercentile } from "./utils" ;
55
66export type FetchBenchmark = {
7- iterationsMs : number [ ] ;
8- averageMs : number ;
9- p90Ms : number ;
7+ iterationsMs : number [ ] ;
8+ averageMs : number ;
9+ p90Ms : number ;
1010} ;
1111
1212export type BenchmarkingResults = {
13- name : string ;
14- path : string ;
15- fetchBenchmark : FetchBenchmark ;
13+ name : string ;
14+ path : string ;
15+ fetchBenchmark : FetchBenchmark ;
1616} [ ] ;
1717
1818type BenchmarkFetchOptions = {
19- numberOfIterations ?: number ;
20- maxRandomDelayMs ?: number ;
21- fetch : ( deploymentUrl : string ) => Promise < Response > ;
19+ numberOfIterations ?: number ;
20+ maxRandomDelayMs ?: number ;
21+ fetch : ( deploymentUrl : string ) => Promise < Response > ;
2222} ;
2323
2424const defaultOptions : Required < Omit < BenchmarkFetchOptions , "fetch" > > = {
25- numberOfIterations : 20 ,
26- maxRandomDelayMs : 15_000 ,
25+ numberOfIterations : 20 ,
26+ maxRandomDelayMs : 15_000 ,
2727} ;
2828
2929/**
@@ -38,17 +38,17 @@ const defaultOptions: Required<Omit<BenchmarkFetchOptions, "fetch">> = {
3838 * @returns the benchmarking results for the application
3939 */
4040export async function benchmarkApplicationResponseTime ( {
41- build,
42- deploy,
43- fetch,
41+ build,
42+ deploy,
43+ fetch,
4444} : {
45- build : ( ) => Promise < void > ;
46- deploy : ( ) => Promise < string > ;
47- fetch : ( deploymentUrl : string ) => Promise < Response > ;
45+ build : ( ) => Promise < void > ;
46+ deploy : ( ) => Promise < string > ;
47+ fetch : ( deploymentUrl : string ) => Promise < Response > ;
4848} ) : Promise < FetchBenchmark > {
49- await build ( ) ;
50- const deploymentUrl = await deploy ( ) ;
51- return benchmarkFetch ( deploymentUrl , { fetch } ) ;
49+ await build ( ) ;
50+ const deploymentUrl = await deploy ( ) ;
51+ return benchmarkFetch ( deploymentUrl , { fetch } ) ;
5252}
5353
5454/**
@@ -59,38 +59,38 @@ export async function benchmarkApplicationResponseTime({
5959 * @returns the computed average alongside all the single call times
6060 */
6161async function benchmarkFetch ( url : string , options : BenchmarkFetchOptions ) : Promise < FetchBenchmark > {
62- const benchmarkFetchCall = async ( ) => {
63- const preTimeMs = performance . now ( ) ;
64- const resp = await options . fetch ( url ) ;
65- const postTimeMs = performance . now ( ) ;
62+ const benchmarkFetchCall = async ( ) => {
63+ const preTimeMs = performance . now ( ) ;
64+ const resp = await options . fetch ( url ) ;
65+ const postTimeMs = performance . now ( ) ;
6666
67- if ( ! resp . ok ) {
68- throw new Error ( `Error: Failed to fetch from "${ url } "` ) ;
69- }
67+ if ( ! resp . ok ) {
68+ throw new Error ( `Error: Failed to fetch from "${ url } "` ) ;
69+ }
7070
71- return postTimeMs - preTimeMs ;
72- } ;
71+ return postTimeMs - preTimeMs ;
72+ } ;
7373
74- const resolvedOptions = { ...defaultOptions , ...options } ;
74+ const resolvedOptions = { ...defaultOptions , ...options } ;
7575
76- const iterationsMs = await Promise . all (
77- new Array ( resolvedOptions . numberOfIterations ) . fill ( null ) . map ( async ( ) => {
78- // let's add a random delay before we make the fetch
79- await nodeTimesPromises . setTimeout ( Math . round ( Math . random ( ) * resolvedOptions . maxRandomDelayMs ) ) ;
76+ const iterationsMs = await Promise . all (
77+ new Array ( resolvedOptions . numberOfIterations ) . fill ( null ) . map ( async ( ) => {
78+ // let's add a random delay before we make the fetch
79+ await nodeTimesPromises . setTimeout ( Math . round ( Math . random ( ) * resolvedOptions . maxRandomDelayMs ) ) ;
8080
81- return benchmarkFetchCall ( ) ;
82- } )
83- ) ;
81+ return benchmarkFetchCall ( ) ;
82+ } )
83+ ) ;
8484
85- const averageMs = iterationsMs . reduce ( ( time , sum ) => sum + time ) / iterationsMs . length ;
85+ const averageMs = iterationsMs . reduce ( ( time , sum ) => sum + time ) / iterationsMs . length ;
8686
87- const p90Ms = getPercentile ( iterationsMs , 90 ) ;
87+ const p90Ms = getPercentile ( iterationsMs , 90 ) ;
8888
89- return {
90- iterationsMs,
91- averageMs,
92- p90Ms,
93- } ;
89+ return {
90+ iterationsMs,
91+ averageMs,
92+ p90Ms,
93+ } ;
9494}
9595
9696/**
@@ -100,18 +100,18 @@ async function benchmarkFetch(url: string, options: BenchmarkFetchOptions): Prom
100100 * @returns the path to the created json file
101101 */
102102export async function saveResultsToDisk ( results : BenchmarkingResults ) : Promise < string > {
103- const date = new Date ( ) ;
103+ const date = new Date ( ) ;
104104
105- const fileName = `${ toSimpleDateString ( date ) } .json` ;
105+ const fileName = `${ toSimpleDateString ( date ) } .json` ;
106106
107- const outputFile = nodePath . resolve ( `./results/${ fileName } ` ) ;
107+ const outputFile = nodePath . resolve ( `./results/${ fileName } ` ) ;
108108
109- await nodeFsPromises . mkdir ( nodePath . dirname ( outputFile ) , { recursive : true } ) ;
109+ await nodeFsPromises . mkdir ( nodePath . dirname ( outputFile ) , { recursive : true } ) ;
110110
111- const resultStr = JSON . stringify ( results , null , 2 ) ;
112- await nodeFsPromises . writeFile ( outputFile , resultStr ) ;
111+ const resultStr = JSON . stringify ( results , null , 2 ) ;
112+ await nodeFsPromises . writeFile ( outputFile , resultStr ) ;
113113
114- return outputFile ;
114+ return outputFile ;
115115}
116116
117117/**
@@ -125,8 +125,8 @@ export async function saveResultsToDisk(results: BenchmarkingResults): Promise<s
125125 * @returns a string representing the date
126126 */
127127function toSimpleDateString ( date : Date ) : string {
128- const isoString = date . toISOString ( ) ;
129- const isoDate = isoString . split ( "." ) [ 0 ] ! ;
128+ const isoString = date . toISOString ( ) ;
129+ const isoDate = isoString . split ( "." ) [ 0 ] ! ;
130130
131- return isoDate . replace ( "T" , "_" ) . replaceAll ( ":" , "-" ) ;
131+ return isoDate . replace ( "T" , "_" ) . replaceAll ( ":" , "-" ) ;
132132}
0 commit comments