Skip to content

Commit

Permalink
apply minor PR suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
dario-piotrowicz committed Nov 5, 2024
1 parent b2da6a6 commit 6394009
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
40 changes: 20 additions & 20 deletions benchmarking/src/benchmarking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import nodePath from "node:path";
import { getPercentile } from "./utils";

export type FetchBenchmark = {
callDurationsMs: number[];
iterationsMs: number[];
averageMs: number;
p90Ms: number;
};
Expand All @@ -15,6 +15,17 @@ export type BenchmarkingResults = {
fetchBenchmark: FetchBenchmark;
}[];

type BenchmarkFetchOptions = {
numberOfIterations?: number;
maxRandomDelayMs?: number;
fetch: (deploymentUrl: string) => Promise<Response>;
};

const defaultOptions: Required<Omit<BenchmarkFetchOptions, "fetch">> = {
numberOfIterations: 20,
maxRandomDelayMs: 15_000,
};

/**
* Benchmarks the response time of an application end-to-end by:
* - building the application
Expand All @@ -40,17 +51,6 @@ export async function benchmarkApplicationResponseTime({
return benchmarkFetch(deploymentUrl, { fetch });
}

type BenchmarkFetchOptions = {
numberOfCalls?: number;
maxRandomDelayMs?: number;
fetch: (deploymentUrl: string) => Promise<Response>;
};

const defaultOptions: Required<Omit<BenchmarkFetchOptions, "fetch">> = {
numberOfCalls: 20,
maxRandomDelayMs: 15_000,
};

/**
* Benchmarks a fetch operation by running it multiple times and computing the average time (in milliseconds) such fetch operation takes.
*
Expand All @@ -71,23 +71,23 @@ async function benchmarkFetch(url: string, options: BenchmarkFetchOptions): Prom
return postTimeMs - preTimeMs;
};

const callDurationsMs = await Promise.all(
new Array(options?.numberOfCalls ?? defaultOptions.numberOfCalls).fill(null).map(async () => {
const resolvedOptions = { ...defaultOptions, ...options };

const iterationsMs = await Promise.all(
new Array(resolvedOptions.numberOfIterations).fill(null).map(async () => {
// let's add a random delay before we make the fetch
await nodeTimesPromises.setTimeout(
Math.round(Math.random() * (options?.maxRandomDelayMs ?? defaultOptions.maxRandomDelayMs))
);
await nodeTimesPromises.setTimeout(Math.round(Math.random() * resolvedOptions.maxRandomDelayMs));

return benchmarkFetchCall();
})
);

const averageMs = callDurationsMs.reduce((time, sum) => sum + time) / callDurationsMs.length;
const averageMs = iterationsMs.reduce((time, sum) => sum + time) / iterationsMs.length;

const p90Ms = getPercentile(callDurationsMs, 90);
const p90Ms = getPercentile(iterationsMs, 90);

return {
callDurationsMs,
iterationsMs,
averageMs,
p90Ms,
};
Expand Down
8 changes: 5 additions & 3 deletions benchmarking/src/cloudflare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ export async function buildApp(dir: string): Promise<void> {

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

if (!("scripts" in packageJsonContent) || !("build:worker" in packageJsonContent.scripts)) {
throw new Error(`Error: package.json for app at "${dir}" does not include a "build:worker" script`);
const buildScript = "build:worker";

if (!packageJsonContent.scripts?.[buildScript]) {
throw new Error(`Error: package.json for app at "${dir}" does not include a "${buildScript}" script`);
}

const command = "pnpm build:worker";
const command = `pnpm ${buildScript}`;

await promiseExec(command, { cwd: dir });
}
Expand Down

0 comments on commit 6394009

Please sign in to comment.