Skip to content

Commit

Permalink
ci: add benchmarking (#21) [ci_release]
Browse files Browse the repository at this point in the history
* ci: add benchmark

* build: make _http_common external

* ci: refactor benchmarks

Co-authored-by: Nytelife26 <xtylerjrx@gmail.com>
  • Loading branch information
tbnritzdoge and Nytelife26 committed Apr 13, 2021
1 parent 339fc23 commit 19128e1
Show file tree
Hide file tree
Showing 11 changed files with 34,770 additions and 13,790 deletions.
4 changes: 4 additions & 0 deletions .benchmarks/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
petitio - base x 6,657 ops/sec ±3.41% (276 runs sampled)

petitio - large body x 373 ops/sec ±3.41% (268 runs sampled)

62 changes: 62 additions & 0 deletions .github/workflows/ci-benchmark.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: "CI: Benchmark"
on: [push, pull_request]
jobs:
benchmark:
name: Benchmark
if: "!(contains(github.event.head_commit.message, '[skip_ci]'))"
runs-on: ubuntu-latest
defaults:
run:
shell: bash
strategy:
matrix:
node: [14]
steps:
- name: "[INIT] Checkout repository"
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: "[INIT] Setup Node.js ${{ matrix.node }}"
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node }}
- name: "[INIT] Restore dependency cache"
id: cache-restore
uses: actions/cache@v2
with:
path: node_modules
key: ${{ runner.os }}-node${{ matrix.node }}-${{ hashFiles('**/package-lock.json') }}
- name: "[INIT] Install dependencies"
if: ${{ !steps.cache-restore.outputs.cache-hit }}
run: npm ci
- name: "[INIT] Build"
run: npm run build
- name: "[INIT] Download previous benchmark data"
uses: actions/cache@v1
with:
path: ./cache
key: ${{ runner.os }}-benchmark
- name: "[EXEC] Benchmark"
run: npm run --silent bench | tee .benchmarks/output.txt
- name: "[POST] Store benchmark results"
uses: rhysd/github-action-benchmark@v1
with:
name: Petitio
tool: "benchmarkjs"
output-file-path: .benchmarks/output.txt
external-data-json-path: ./cache/benchmark-data.json
alert-threshold: "200%"
github-token: ${{ secrets.GITHUB_TOKEN }}
alert-comment-cc-users: "@tbnritzdoge @nytelife26"
comment-on-alert: true
fail-on-alert: true
- name: "[POST] Push benchmark results"
if: github.event_name != 'pull_request'
run: |
git config user.name 'Kludge Cyber Systems (CI)'
git config user.email contact@kludge-cs.co.uk
git add .benchmarks
git commit --no-verify -m 'chore: update benchmarks' || true
git push
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1 change: 1 addition & 0 deletions .github/workflows/ci-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ on:
jobs:
build:
name: Build
if: "!(contains(github.event.head_commit.message, '[skip_ci]'))"
runs-on: ubuntu-latest
strategy:
matrix:
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/ci-lint-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ on: [push, pull_request]
jobs:
eslint:
name: Lint
if: "!(contains(github.event.head_commit.message, '[skip_ci]'))"
runs-on: ubuntu-latest
defaults:
run:
Expand Down Expand Up @@ -44,6 +45,7 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
test-cover:
name: Test & Upload Coverage
if: "!(contains(github.event.head_commit.message, '[skip_ci]'))"
runs-on: ubuntu-latest
defaults:
run:
Expand Down Expand Up @@ -77,6 +79,7 @@ jobs:
parallel: true
cover:
name: Finalize Coverage Report
if: "!(contains(github.event.head_commit.message, '[skip_ci]'))"
needs: test-cover
runs-on: ubuntu-latest
steps:
Expand Down
27 changes: 27 additions & 0 deletions benchmarks/client.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { CycleLogger } from "@kludge-cs/bench-utils";
// @ts-expect-error: BenchmarkJS does not support ESM exports
import bench from "benchmark";
const { Suite } = bench;
// @ts-expect-error: TSC is not aware when petitio is built
import petitio from "../dist/index.mjs";
import undici from "undici";
const { Client } = undici;

const client = new Client("http://localhost:8080");
const defer = {"defer": true, "minSamples": 200};

new Suite()
.add("petitio - base", async (deferred) => {
await petitio("http://localhost:8080/ok")
.client(client, true)
.send();
deferred.resolve();
}, defer)
.add("petitio - large body", async (deferred) => {
await petitio("http://localhost:8080/large")
.client(client, true)
.send();
deferred.resolve();
}, defer)
.on("cycle", (event) => console.log(event.target.toString()))
.run({async: true});
9 changes: 9 additions & 0 deletions benchmarks/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { spawn } from "child_process";
const server = spawn("node", ["benchmarks/server.mjs"]);
const client = spawn("node", ["benchmarks/client.mjs"]);

client.stdout.on("data", (data) => console.log(data.toString()));
client.on("error", (err) => console.error(err));
server.on("error", (err) => console.error(err));

client.on("exit", () => server.kill("SIGKILL"));
27,236 changes: 27,236 additions & 0 deletions benchmarks/large.txt

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions benchmarks/server.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as http from "http";
import { dirname } from "path";
import { fileURLToPath } from "url";
import { readFileSync } from "fs";

// @ts-expect-error: the file is running as a module, import.meta is allowed
const largeData = readFileSync(`${dirname(fileURLToPath(import.meta.url))}/large.txt`).toString();

function requestHandler(req, res) {
switch (req.url) {
case "/ok": {
res.writeHead(200, {"Content-Type": "text/plain"});
res.end("ok");
return;
}
case "/large": {
res.writeHead(200, {"Content-Type": "text/plain"});
res.end(largeData);
return;
}
default: {
res.end("No benchmark specified.");
}
}
}

http
.createServer(requestHandler)
.listen(8080, () => console.log("Awaiting requests."));
1 change: 1 addition & 0 deletions esbuild.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const esbuild = require("esbuild");
esbuild.build({
bundle: true,
entryPoints: ["./src/index.ts"],
external: ["_http_common"],
format: "cjs",
minify: true,
outfile: "./dist/index.js",
Expand Down
Loading

0 comments on commit 19128e1

Please sign in to comment.