From 3da34e268426b92510c7f9b730a2fa297dca6fbf Mon Sep 17 00:00:00 2001 From: "Max P." Date: Tue, 27 May 2025 14:48:05 +0200 Subject: [PATCH] test(bench): add parallel benchmarks for HTTP kernel - Introduce parallel benchmarks for simple and complex HTTP requests - Improve performance testing with higher concurrency (10,000 requests) - Comment out single-request benchmarks for future reference --- src/__bench__/HttpKernel.bench.ts | 87 +++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 src/__bench__/HttpKernel.bench.ts diff --git a/src/__bench__/HttpKernel.bench.ts b/src/__bench__/HttpKernel.bench.ts new file mode 100644 index 0000000..d08a4bf --- /dev/null +++ b/src/__bench__/HttpKernel.bench.ts @@ -0,0 +1,87 @@ +import type { IRouteDefinition } from '../Interfaces/mod.ts'; +import { HttpKernel } from '../mod.ts'; + +const CONCURRENT_REQUESTS = 10000; + +// Deno.bench('Simple request', async (b) => { +// const kernel = new HttpKernel(); + +// const def: IRouteDefinition = { method: 'GET', path: '/hello' }; +// kernel.route(def).handle((_ctx) => { +// return Promise.resolve(new Response('OK', { status: 200 })); +// }); +// b.start(); +// await kernel.handle( +// new Request('http://localhost/hello', { method: 'GET' }), +// ); +// b.end(); +// }); + +Deno.bench('Simple request (parallel)', async (b) => { + const kernel = new HttpKernel(); + + const def: IRouteDefinition = { method: 'GET', path: '/hello' }; + kernel.route(def).handle((_ctx) => { + return Promise.resolve(new Response('OK', { status: 200 })); + }); + + const requests = Array.from( + { length: CONCURRENT_REQUESTS }, + () => + kernel.handle( + new Request('http://localhost/hello', { method: 'GET' }), + ), + ); + + b.start(); + await Promise.all(requests); + b.end(); +}); + +// Deno.bench('Complex request', async (b) => { +// const kernel = new HttpKernel(); + +// kernel.route({ method: 'GET', path: '/test' }) +// .middleware(async (_ctx, next) => { +// return await next(); +// }) +// .middleware(async (_ctx, next) => { +// return await next(); +// }) +// .handle((_ctx) => { +// return Promise.resolve(new Response('done')); +// }); + +// b.start(); +// await kernel.handle( +// new Request('http://localhost/test', { method: 'GET' }), +// ); +// b.end(); +// }); + +Deno.bench('Complex request (parallel)', async (b) => { + const kernel = new HttpKernel(); + + kernel.route({ method: 'GET', path: '/test' }) + .middleware(async (_ctx, next) => { + return await next(); + }) + .middleware(async (_ctx, next) => { + return await next(); + }) + .handle((_ctx) => { + return Promise.resolve(new Response('done')); + }); + + const requests = Array.from( + { length: CONCURRENT_REQUESTS }, + () => + kernel.handle( + new Request('http://localhost/test', { method: 'GET' }), + ), + ); + + b.start(); + await Promise.all(requests); + b.end(); +});