Skip to content

Commit 4a80e6d

Browse files
author
NarrowsProjects
committed
EXPERIMENT: add a benchmarker
1 parent 1d7c165 commit 4a80e6d

3 files changed

Lines changed: 129 additions & 0 deletions

File tree

.github/workflows/bookkeeping.yml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,62 @@ concurrency:
1515
cancel-in-progress: true
1616

1717
jobs:
18+
benchmark:
19+
name: envs benchmarks
20+
runs-on: ubuntu-latest
21+
timeout-minutes: 10
22+
23+
strategy:
24+
fail-fast: false
25+
26+
steps:
27+
- uses: actions/checkout@v6
28+
- name: Set up Docker
29+
uses: docker/setup-buildx-action@v4
30+
31+
- name: Create Coverage Directory
32+
run: mkdir -p ${{ github.workspace }}/coverage
33+
34+
- name: Set COMPOSE_PROJECT_NAME
35+
run: echo "COMPOSE_PROJECT_NAME=benchmark_app" >> $GITHUB_ENV
36+
37+
- name: Build and Start Test Database
38+
run: |
39+
docker compose \
40+
-f docker-compose.test-parallel-base.yml \
41+
-f docker-compose.test-parallel-ci.yml \
42+
up --detach test_db
43+
env:
44+
TEST_TYPE: envs
45+
46+
- name: Build Test Container
47+
run: |
48+
docker compose \
49+
-f docker-compose.test-parallel-base.yml \
50+
-f docker-compose.test-parallel-ci.yml \
51+
build test_app
52+
env:
53+
TEST_TYPE: envs
54+
55+
- name: Run Benchmarks
56+
run: |
57+
docker compose \
58+
-f docker-compose.test-parallel-base.yml \
59+
-f docker-compose.test-parallel-ci.yml \
60+
run test_app npm run benchmark
61+
env:
62+
TEST_TYPE: envs
63+
64+
- name: Clean Up
65+
if: always()
66+
run: |
67+
docker compose \
68+
-f docker-compose.test-parallel-base.yml \
69+
-f docker-compose.test-parallel-ci.yml \
70+
down
71+
env:
72+
TEST_TYPE: envs
73+
1874
parallel_tests:
1975
name: ${{ matrix.test_type }}
2076
runs-on: ubuntu-latest

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"start:dev": "nodemon --ignore 'lib/public/**/*.js' lib/main.js",
1616
"start:prod": "node lib/main.js",
1717
"test": "mocha --exit --timeout 0",
18+
"benchmark": "node ./opt/test.js",
1819
"test:subset": "nyc -- mocha --exit --timeout 0 test/scripts/test-${TEST_TYPE}.js && nyc report --report-dir=/usr/src/app/coverage/${TEST_TYPE} --reporter=json",
1920
"test:subset-local": "mocha --exit --timeout 0 --reporter test/scripts/parallel-local/custom-mocha-reporter.js test/scripts/test-${TEST_TYPE}.js",
2021
"docker-run": "docker compose -f docker-compose.yml -f docker-compose.dev.yml up --build",

scripts/test.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import http from 'http';
2+
3+
4+
const ITERATIONS = 100;
5+
const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MCwidXNlcm5hbWUiOiJhbm9ueW1vdXMiLCJuYW1lIjoiQW5vbnltb3VzIiwiYWNjZXNzIjoiYWRtaW4iLCJpYXQiOjE3ODIzOTk4MjgsImV4cCI6MTc4MzAwNDYyOCwiaXNzIjoibzItdWkifQ.7WPQ0YSB60_XZUV62jtzMTmxV08Zd5eeVolsb56XFRA';
6+
7+
8+
function request(url) {
9+
return new Promise((resolve) => {
10+
const start = Date.now();
11+
12+
13+
http.get(url, (res) => {
14+
res.resume();
15+
res.on("end", () => {
16+
resolve({ responseTime: Date.now() - start, statusCode: res.statusCode });
17+
});
18+
})
19+
});
20+
}
21+
22+
23+
const latencyData = {};
24+
25+
26+
async function runTest(key, endpoint) {
27+
const results = [];
28+
29+
30+
for (let i = 1; i <= ITERATIONS; i++) {
31+
const result = await request(`${endpoint}&token=${token}`);
32+
results.push(result);
33+
}
34+
35+
36+
37+
38+
const times = results.map((r) => r.responseTime);
39+
const sorted = [...times].sort((a, b) => a - b);
40+
const avg = times.reduce((a, b) => a + b, 0) / times.length;
41+
const min = sorted[0];
42+
const max = sorted[sorted.length - 1];
43+
44+
45+
latencyData[key] = { endpoint, min, max, avg: avg.toFixed(2) };
46+
}
47+
48+
49+
50+
51+
function printResults() {
52+
console.log("====== Latency Benchmark Results ======");
53+
54+
55+
for (let [key, { endpoint, min, max, avg }] of Object.entries(latencyData)) {
56+
console.log(`Page: ${key}`);
57+
console.log(`Endpoint: GET ${endpoint}`);
58+
console.log(`Iterations: ${ITERATIONS}`);
59+
console.log(`Min: ${min} ms`);
60+
console.log(`Max: ${max} ms`);
61+
console.log(`Avg: ${avg} ms`);
62+
console.log("---------------------------------------");
63+
}
64+
65+
66+
console.log("=======================================");
67+
}
68+
69+
70+
Promise.all([
71+
await runTest('Envs', 'http://localhost:4000/api/environments?page[offset]=0&page[limit]=10'),
72+
]).then(printResults);

0 commit comments

Comments
 (0)