Skip to content

Commit

Permalink
feat: add method benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
Hebilicious committed Jul 30, 2023
1 parent a4e3655 commit 8b8474d
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 42 deletions.
24 changes: 18 additions & 6 deletions benchmark/direct.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
/* eslint-disable no-console */
import Benchmark from "benchmark"; // https://www.npmjs.com/package/benchmark'
import { printEnv, benchSets, printStats, router, logSection } from "./utils.mjs";
import {
printEnv,
benchSets,
printStats,
router,
logSection,
} from "./utils.mjs";

async function main () {
async function main() {
printEnv();

for (const bench of benchSets) {
Expand All @@ -11,13 +17,19 @@ async function main () {
const stats = {};
suite.add("lookup", () => {
for (const req of bench.requests) {
const match = router.lookup(req.path);
if (!match) { stats[match] = (stats[match] || 0) + 1; }
const match = router.lookup(req.path, { method: req.method });
if (!match) {
stats[match] = (stats[match] || 0) + 1;
}
stats[req.path] = (stats[req.path] || 0) + 1;
}
});
suite.on("cycle", (event) => { console.log(String(event.target)); });
const promise = new Promise(resolve => suite.on("complete", () => resolve()));
suite.on("cycle", (event) => {
console.log(String(event.target));
});
const promise = new Promise((resolve) =>
suite.on("complete", () => resolve())
);
suite.run({ async: true });
await promise;
printStats(stats);
Expand Down
36 changes: 24 additions & 12 deletions benchmark/http.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@

import autocannon from "autocannon"; // https://github.com/mcollina/autocannon
import { listen } from "listhen";
import { printEnv, benchSets, printStats, logSection, router } from "./utils.mjs";
import {
printEnv,
benchSets,
printStats,
logSection,
router,
} from "./utils.mjs";

async function main () {
async function main() {
printEnv();

for (const bench of benchSets) {
logSection(`Benchmark: ${bench.title}`);
const { listener, stats } = await createServer();
const instance = autocannon({
url: listener.url,
requests: bench.requests
requests: bench.requests,
});
autocannon.track(instance);
process.once("SIGINT", () => {
Expand All @@ -29,16 +35,22 @@ async function main () {
// eslint-disable-next-line unicorn/prefer-top-level-await
main().catch(console.error);

async function createServer () {
async function createServer() {
const stats = {};
const listener = await listen((req, res) => {
stats[req.url] = (stats[req.url] || 0) + 1;
const match = router.lookup(req.url);
if (!match) {
stats[match] = (stats[match] || 0) + 1;
}
res.end(JSON.stringify((match || { error: 404 })));
}, { showURL: false });
const listener = await listen(
(req, res) => {
stats[req.url] = (stats[req.url] || 0) + 1;
const match = router.lookup(
req.url,
req.url.endsWith("get") ? { method: "GET" } : undefined
);
if (!match) {
stats[match] = (stats[match] || 0) + 1;
}
res.end(JSON.stringify(match || { error: 404 }));
},
{ showURL: false }
);

return { listener, stats };
}
61 changes: 37 additions & 24 deletions benchmark/utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ import { readFileSync } from "node:fs";
import os from "node:os";
import { createRouter } from "radix3";

export const logSection = (title) => { console.log(`\n--- ${title} ---\n`); };
export const logSection = (title) => {
console.log(`\n--- ${title} ---\n`);
};

const pkgVersion = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")).version;
const pkgVersion = JSON.parse(
readFileSync(new URL("../package.json", import.meta.url), "utf8")
).version;

export function printEnv () {
export function printEnv() {
logSection("Test environment");
console.log("Node.js version:", process.versions.node);
console.log("Radix3 version:", pkgVersion);
Expand All @@ -17,36 +21,45 @@ export function printEnv () {
console.log("");
}

export function printStats (stats) {
console.log("Stats:\n" + Object.entries(stats).map(([path, hits]) => ` - ${path}: ${hits}`).join("\n"));
export function printStats(stats) {
console.log(
"Stats:\n" +
Object.entries(stats)
.map(([path, hits]) => ` - ${path}: ${hits}`)
.join("\n")
);
}

export const router = createRouter({
routes: Object.fromEntries([
"/hello",
"/cool",
"/hi",
"/helium",
"/coooool",
"/chrome",
"/choot",
"/choot/:choo",
"/ui/**",
"/ui/components/**"
].map(path => [path, { path }]))
routes: Object.fromEntries(
[
"/hello",
"/cool",
"/hi",
"/helium",
"/coooool",
"/chrome",
"/choot",
"/choot/:choo",
"/ui/**",
"/ui/components/**",
].map((path) => [path, { path }])
),
});

router.insert("/method-get", { path: "/method-get" }, { method: "GET" });

export const benchSets = [
{
title: "static route",
requests: [
{ path: "/choot" }
]
requests: [{ path: "/choot" }],
},
{
title: "dynamic route",
requests: [
{ path: "/choot/123" }
]
}
requests: [{ path: "/choot/123" }],
},
{
title: "method route",
requests: [{ path: "/method-get", method: "GET" }],
},
];

0 comments on commit 8b8474d

Please sign in to comment.