Skip to content

Commit bed8e70

Browse files
committed
fix(perf): preserve benchmark socket reuse
1 parent cb312e3 commit bed8e70

2 files changed

Lines changed: 50 additions & 2 deletions

File tree

‎scripts/bench-gateway-concurrency.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,13 @@ async function requestHttp(params: {
249249
}
250250
settled = true;
251251
clearTimeout(timer);
252-
req.destroy();
253252
run();
254253
};
255-
const fail = (error: Error) => settle(() => reject(error));
254+
const fail = (error: Error) =>
255+
settle(() => {
256+
req.destroy();
257+
reject(error);
258+
});
256259
const req = request(
257260
{
258261
headers: { accept: params.accept },

‎test/scripts/bench-gateway-concurrency.test.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,51 @@ describe("gateway concurrency benchmark script", () => {
210210
}
211211
});
212212

213+
it("reuses one connection for sequential successful HTTP samples", async () => {
214+
let connectionCount = 0;
215+
const server = createHttpServer((request, response) => {
216+
response.setHeader(
217+
"content-type",
218+
request.url === "/readyz" ? "application/json" : "text/html",
219+
);
220+
response.end(request.url === "/readyz" ? '{"status":"ok"}' : "<html></html>");
221+
});
222+
server.on("connection", () => {
223+
connectionCount += 1;
224+
});
225+
await new Promise<void>((resolve, reject) => {
226+
server.once("error", reject);
227+
server.listen(0, "127.0.0.1", resolve);
228+
});
229+
try {
230+
const address = server.address();
231+
if (!address || typeof address === "string") {
232+
throw new Error("expected HTTP test server address");
233+
}
234+
const deadlineAt = performance.now() + 5_000;
235+
236+
await testing.requestHttp({
237+
accept: "application/json",
238+
deadlineAt,
239+
path: "/readyz",
240+
port: address.port,
241+
});
242+
await testing.requestHttp({
243+
accept: "text/html",
244+
deadlineAt,
245+
path: "/",
246+
port: address.port,
247+
});
248+
249+
expect(connectionCount).toBe(1);
250+
} finally {
251+
server.closeAllConnections();
252+
await new Promise<void>((resolve, reject) => {
253+
server.close((error) => (error ? reject(error) : resolve()));
254+
});
255+
}
256+
});
257+
213258
it("loads through native Node TypeScript stripping", () => {
214259
const result = spawnSync(process.execPath, ["scripts/bench-gateway-concurrency.ts", "--help"], {
215260
cwd: process.cwd(),

0 commit comments

Comments
 (0)