Skip to content

Commit

Permalink
Spawn server from main file as separate process
Browse files Browse the repository at this point in the history
  • Loading branch information
hansott committed Jan 9, 2025
1 parent 00150f0 commit 8d27ef5
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 79 deletions.
136 changes: 78 additions & 58 deletions benchmarks/operations/benchmark.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
const { spawnSync } = require("child_process");
const { spawnSync, spawn } = require("child_process");
const { setTimeout } = require("timers/promises");

const modules = [
{
module: "sqli",
name: "SQL query",
},
{
module: "ssrf",
name: "Outgoing HTTP request",
},
{
module: "path-traversal",
name: "File read",
Expand All @@ -25,70 +22,93 @@ const modules = [
module: "shelli",
name: "Shell command",
},
{
module: "ssrf",
name: "Outgoing HTTP request",
},
];

let markdownTable =
"| Benchmark | Avg. time w/o Zen | Avg. time w/ Zen | Delta |\n";
markdownTable +=
"|---------------------|-------------------|------------------|------------|\n";

for (const { module, name } of modules) {
console.log();
console.log(`Running module ${module}`);
const withoutZen = spawnSync(`node`, ["run.js", module, "false"], {
(async () => {
let server = spawn("node", ["spawn-server.js"], {
cwd: __dirname,
stdio: "inherit",
});

if (withoutZen.status !== 0) {
console.error(withoutZen.stderr.toString());
process.exit(1);
}
await setTimeout(1000);

let timingsWithoutZen;
try {
timingsWithoutZen = JSON.parse(withoutZen.stdout.toString());
} catch (e) {
console.error(withoutZen.stdout.toString());
process.exit(1);
}
let markdownTable =
"| Benchmark | Avg. time w/o Zen | Avg. time w/ Zen | Delta |\n";
markdownTable +=
"|---------------------|-------------------|------------------|------------|\n";

const withZen = spawnSync("node", ["run.js", module, "true"], {
cwd: __dirname,
});
for (const { module, name } of modules) {
console.log();
console.log(`Running module ${module}`);
const withoutZen = spawnSync(`node`, ["run.js", module, "false"], {
cwd: __dirname,
});

if (withZen.status !== 0) {
console.error(withZen.stderr.toString());
process.exit(1);
}
if (withoutZen.status !== 0) {
console.error(withoutZen.stderr.toString());
process.exit(1);
}

let timingsWithZen;
try {
timingsWithZen = JSON.parse(withZen.stdout.toString());
} catch (e) {
console.error(withZen.stdout.toString());
process.exit(1);
}
let timingsWithoutZen;
try {
timingsWithoutZen = JSON.parse(withoutZen.stdout.toString());
} catch (e) {
console.error(withoutZen.stdout.toString());
process.exit(1);
}

const averageWithoutZen =
timingsWithoutZen.reduce((a, b) => a + b, 0) / timingsWithoutZen.length;
const averageWithZen =
timingsWithZen.reduce((a, b) => a + b, 0) / timingsWithZen.length;
const delta = averageWithZen - averageWithoutZen;
let flags = [];
if (module === "ssrf") {
flags.push("--prof");
}
const withZen = spawnSync(
"node",
flags.concat(["run.js", module, "true"]),
{
cwd: __dirname,
}
);

const formatter = new Intl.NumberFormat("en-US", {
style: "decimal",
maximumFractionDigits: 4,
});
if (withZen.status !== 0) {
console.error(withZen.stderr.toString());
process.exit(1);
}

let timingsWithZen;
try {
timingsWithZen = JSON.parse(withZen.stdout.toString());
} catch (e) {
console.error(withZen.stdout.toString());
process.exit(1);
}

console.log(`Module: ${name}`);
console.log(
`Average time without Zen: ${formatter.format(averageWithoutZen)}ms`
);
console.log(`Average time with Zen: ${formatter.format(averageWithZen)}ms`);
console.log(`Delta: +${formatter.format(delta)}ms`);
const averageWithoutZen =
timingsWithoutZen.reduce((a, b) => a + b, 0) / timingsWithoutZen.length;
const averageWithZen =
timingsWithZen.reduce((a, b) => a + b, 0) / timingsWithZen.length;
const delta = averageWithZen - averageWithoutZen;

markdownTable += `| ${name} | ${formatter.format(averageWithoutZen)}ms | ${formatter.format(averageWithZen)}ms | +${formatter.format(delta)}ms |\n`;
}
const formatter = new Intl.NumberFormat("en-US", {
style: "decimal",
maximumFractionDigits: 4,
});

console.log(`Module: ${name}`);
console.log(
`Average time without Zen: ${formatter.format(averageWithoutZen)}ms`
);
console.log(`Average time with Zen: ${formatter.format(averageWithZen)}ms`);
console.log(`Delta: +${formatter.format(delta)}ms`);

markdownTable += `| ${name} | ${formatter.format(averageWithoutZen)}ms | ${formatter.format(averageWithZen)}ms | +${formatter.format(delta)}ms |\n`;
}

console.log();
console.log(markdownTable);

console.log();
console.log(markdownTable);
server.kill();
})();
14 changes: 14 additions & 0 deletions benchmarks/operations/spawn-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { createServer } = require("http");

const server = createServer((req, res) => {
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("Hello Node.js!");
});

server.listen(10411);

process.on("SIGTERM", () => {
server.close(() => {
process.exit(0);
});
});
22 changes: 1 addition & 21 deletions benchmarks/operations/ssrf.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,5 @@
const { createServer, get } = require("http");

const server = createServer((req, res) => {
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("Hello Node.js!");
});

module.exports = {
setup: async function setup() {
return new Promise((resolve) => {
server.listen(0, resolve);
});
},
step: async function step() {
return new Promise((resolve) => {
get(`http://localhost:${server.address().port}`, (res) => {
res.resume();
res.on("end", resolve);
});
});
},
teardown: async function teardown() {
server.close();
await fetch("http://localhost:10411");
},
};

0 comments on commit 8d27ef5

Please sign in to comment.