Skip to content

Commit 6394009

Browse files
apply minor PR suggestions
1 parent b2da6a6 commit 6394009

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

benchmarking/src/benchmarking.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import nodePath from "node:path";
44
import { getPercentile } from "./utils";
55

66
export 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
};

benchmarking/src/cloudflare.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,13 @@ export async function buildApp(dir: string): Promise<void> {
5353

5454
const packageJsonContent = JSON.parse(await nodeFsPromises.readFile(packageJsonPath, "utf8"));
5555

56-
if (!("scripts" in packageJsonContent) || !("build:worker" in packageJsonContent.scripts)) {
57-
throw new Error(`Error: package.json for app at "${dir}" does not include a "build:worker" script`);
56+
const buildScript = "build:worker";
57+
58+
if (!packageJsonContent.scripts?.[buildScript]) {
59+
throw new Error(`Error: package.json for app at "${dir}" does not include a "${buildScript}" script`);
5860
}
5961

60-
const command = "pnpm build:worker";
62+
const command = `pnpm ${buildScript}`;
6163

6264
await promiseExec(command, { cwd: dir });
6365
}

0 commit comments

Comments
 (0)