Skip to content
This repository was archived by the owner on Mar 17, 2025. It is now read-only.

Commit fd2e11d

Browse files
committed
migrate benchmarks to k6
Signed-off-by: Sahil Yeole <[email protected]>
1 parent 5592580 commit fd2e11d

File tree

9 files changed

+77
-24
lines changed

9 files changed

+77
-24
lines changed

.github/workflows/bench.yml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,19 @@ jobs:
2525
token: ${{ secrets.GITHUB_TOKEN }}
2626
ref: ${{ github.event_name == 'pull_request_target' && github.event.pull_request.head.sha || github.sha }}
2727

28-
- name: Build devcontainer and run benchmarks
28+
- name: Build devcontainer
2929
uses: devcontainers/[email protected]
3030
with:
3131
imageName: graphql-benchmarks
3232
push: never
33-
runCmd: |
34-
bash ./setup.sh
35-
bash ./run_benchmarks.sh
33+
34+
- name: Setup k6
35+
uses: grafana/setup-k6-action@v1
36+
37+
- name: Run benchmarks
38+
run: |
39+
bash ./setup.sh
40+
bash ./run_benchmarks.sh
3641
3742
- name: Print benchmark results
3843
run: cat ./results.md

k6/bench.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import http from 'k6/http';
2+
import { check } from 'k6';
3+
4+
const whichBenchmark = Number(__ENV.BENCHMARK);
5+
6+
export const options = {
7+
scenarios: {
8+
posts: {
9+
executor: 'constant-vus',
10+
duration: whichBenchmark === 2 ? '30s' : '10s',
11+
gracefulStop: '0s',
12+
vus: 100,
13+
}
14+
},
15+
cloud: {
16+
name: __ENV.TEST_NAME + '-' + whichBenchmark,
17+
},
18+
};
19+
20+
const url = 'http://localhost:8000/graphql';
21+
const params = {
22+
headers: {
23+
'Connection': 'keep-alive',
24+
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36',
25+
'Content-Type': 'application/json',
26+
},
27+
};
28+
29+
export default function() {
30+
const payload = JSON.stringify({
31+
operationName: null,
32+
variables: {},
33+
query: whichBenchmark === 2 ? '{posts{id,userId,title,user{id,name,email}}}' : '{posts{title}}',
34+
});
35+
36+
const res = http.post(url, payload, params);
37+
check(res, {
38+
'status is 200': (r) => r.status === 200,
39+
});
40+
}
41+
42+
export function handleSummary(data) {
43+
const med_request_count = data.metrics.http_reqs.values.count;
44+
const avg_latency = data.metrics.http_req_duration.values.avg;
45+
const trimmed_avg_latency = Math.round(avg_latency * 100) / 100;
46+
const request_count_message = `Requests/sec: ${med_request_count}\n`;
47+
const latency_message = `Latency: ${trimmed_avg_latency} ms\n`;
48+
49+
return {
50+
stdout: request_count_message + latency_message,
51+
};
52+
}

k6/bench.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
test_name=$1
2+
benchmark=$2
3+
4+
# k6 run k6/bench.js --quiet --out cloud --env TEST_NAME=$test_name --env BENCHMARK=$benchmark
5+
k6 run k6/bench.js --quiet --env TEST_NAME=$test_name --env BENCHMARK=$benchmark

k6/warmup.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
k6 run k6/bench.js --quiet

run_benchmarks.sh

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,17 @@ sh nginx/run.sh
2020
function runBenchmark() {
2121
killServerOnPort 8000
2222
sleep 5
23-
local serviceScript="$1"
23+
local service="$1"
2424
local benchmarks=(1 2)
25+
local serviceScript="graphql/${service}/run.sh"
2526

2627
bash "$serviceScript" & # Run in daemon mode
2728
sleep 15 # Give some time for the service to start up
2829

2930
for bench in "${benchmarks[@]}"; do
30-
local benchmarkScript="wrk/bench${bench}.sh"
31-
31+
local benchmarkScript="tests/test.sh"
32+
local warmupScript="tests/warmup.sh"
33+
3234
# Replace / with _
3335
local sanitizedServiceScriptName=$(echo "$serviceScript" | tr '/' '_')
3436

@@ -37,18 +39,18 @@ function runBenchmark() {
3739
bash "test_query${bench}.sh"
3840

3941
# Warmup run
40-
bash "$benchmarkScript" > /dev/null
42+
bash "$warmupScript" > /dev/null
4143
sleep 1 # Give some time for apps to finish in-flight requests from warmup
42-
bash "$benchmarkScript" > /dev/null
44+
bash "$warmupScript" > /dev/null
4345
sleep 1
44-
bash "$benchmarkScript" > /dev/null
46+
bash "$warmupScript" > /dev/null
4547
sleep 1
4648

4749

4850
# 3 benchmark runs
4951
for resultFile in "${resultFiles[@]}"; do
5052
echo "Running benchmark $bench for $serviceScript"
51-
bash "$benchmarkScript" > "bench${bench}_${resultFile}"
53+
bash "$benchmarkScript" "$service" "$bench" > "bench${bench}_${resultFile}"
5254
if [ "$bench" == "1" ]; then
5355
bench1Results+=("bench1_${resultFile}")
5456
else
@@ -61,7 +63,7 @@ function runBenchmark() {
6163
rm "results.md"
6264

6365
for service in "apollo_server" "caliban" "netflix_dgs" "gqlgen" "tailcall" "async_graphql"; do
64-
runBenchmark "graphql/${service}/run.sh"
66+
runBenchmark "$service"
6567
if [ "$service" == "apollo_server" ]; then
6668
cd graphql/apollo_server/
6769
npm stop

wrk/bench1.sh

Lines changed: 0 additions & 1 deletion
This file was deleted.

wrk/bench2.sh

Lines changed: 0 additions & 1 deletion
This file was deleted.

wrk/wrk1.lua

Lines changed: 0 additions & 5 deletions
This file was deleted.

wrk/wrk2.lua

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)