|
| 1 | +import { bench, summary, compact, run } from "mitata"; |
| 2 | +import { requestWithURL } from "../../src/utils/request.ts"; |
| 3 | + |
| 4 | +const req = new Request("http://localhost:3000/base/path?q=1", { |
| 5 | + method: "POST", |
| 6 | + headers: { "content-type": "application/json", "x-custom": "value" }, |
| 7 | + body: JSON.stringify({ hello: "world" }), |
| 8 | +}); |
| 9 | + |
| 10 | +// Pre-warmed proxy (cache populated) |
| 11 | +const warmed = requestWithURL(req, "http://localhost:3000/path?q=1"); |
| 12 | +warmed.method; |
| 13 | +warmed.headers; |
| 14 | + |
| 15 | +compact(() => { |
| 16 | + summary(() => { |
| 17 | + bench("new Request(url, req)", () => { |
| 18 | + const url = new URL(req.url); |
| 19 | + url.pathname = url.pathname.slice("/base".length) || "/"; |
| 20 | + return new Request(url, req); |
| 21 | + }); |
| 22 | + |
| 23 | + bench("requestWithURL(req, url)", () => { |
| 24 | + return requestWithURL(req, "http://localhost:3000/path?q=1"); |
| 25 | + }); |
| 26 | + }); |
| 27 | + |
| 28 | + summary(() => { |
| 29 | + bench("req.url", () => req.url); |
| 30 | + bench("proxied.url (cached)", () => warmed.url); |
| 31 | + }); |
| 32 | + |
| 33 | + summary(() => { |
| 34 | + bench("req.method", () => req.method); |
| 35 | + bench("proxied.method (cold)", () => { |
| 36 | + const p = requestWithURL(req, "http://localhost:3000/path?q=1"); |
| 37 | + return p.method; |
| 38 | + }); |
| 39 | + bench("proxied.method (cached)", () => warmed.method); |
| 40 | + }); |
| 41 | + |
| 42 | + summary(() => { |
| 43 | + bench("req.headers.get()", () => req.headers.get("content-type")); |
| 44 | + bench("proxied.headers.get() (cold)", () => { |
| 45 | + const p = requestWithURL(req, "http://localhost:3000/path?q=1"); |
| 46 | + return p.headers.get("content-type"); |
| 47 | + }); |
| 48 | + bench("proxied.headers.get() (cached)", () => |
| 49 | + warmed.headers.get("content-type"), |
| 50 | + ); |
| 51 | + }); |
| 52 | +}); |
| 53 | + |
| 54 | +await run({ throw: true }); |
0 commit comments