Skip to content

Commit a885d86

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

4 files changed

Lines changed: 135 additions & 0 deletions

File tree

.github/workflows/bookkeeping.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,51 @@ 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: Set COMPOSE_PROJECT_NAME
32+
run: echo "COMPOSE_PROJECT_NAME=benchmark_app" >> $GITHUB_ENV
33+
34+
- name: Build and Start Test Database
35+
run: |
36+
docker compose \
37+
-f docker-compose.test-parallel-base.yml \
38+
-f docker-compose.benchmark-ci.yml \
39+
up --detach test_db
40+
41+
- name: Build Benchmark Container
42+
run: |
43+
docker compose \
44+
-f docker-compose.test-parallel-base.yml \
45+
-f docker-compose.benchmark-ci.yml \
46+
build test_app
47+
48+
- name: Run Benchmarks
49+
run: |
50+
docker compose \
51+
-f docker-compose.test-parallel-base.yml \
52+
-f docker-compose.benchmark-ci.yml \
53+
run test_app
54+
55+
- name: Clean Up
56+
if: always()
57+
run: |
58+
docker compose \
59+
-f docker-compose.test-parallel-base.yml \
60+
-f docker-compose.benchmark-ci.yml \
61+
down
62+
1863
parallel_tests:
1964
name: ${{ matrix.test_type }}
2065
runs-on: ubuntu-latest

Dockerfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ FROM developmentdependencies AS test
4848
# Run start script as specified in package.json
4949
CMD [ "/opt/wait-for-it.sh", "-t", "0", "database:3306", "--", "npm", "run", "test" ]
5050

51+
# ---- Benchmark ----
52+
FROM developmentdependencies AS benchmark
53+
54+
CMD [ "sh", "-c", "/opt/wait-for-it.sh -t 0 test_db:3306 -- node /opt/test.js" ]
55+
5156
#
5257
# ---- Test parallel for CI ----
5358
FROM developmentdependencies AS test_parallel_ci

docker-compose.benchmark-ci.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
services:
2+
test_app:
3+
build:
4+
target: benchmark
5+
volumes:
6+
- type: bind
7+
read_only: true
8+
source: ./scripts
9+
target: /opt
10+
- type: bind
11+
source: ./docker/storage
12+
target: /var/storage
13+
read_only: false

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)