Skip to content

Commit

Permalink
migrate benchmarks to k6
Browse files Browse the repository at this point in the history
Signed-off-by: Sahil Yeole <[email protected]>
  • Loading branch information
beelchester committed Jul 3, 2024
1 parent 5592580 commit fd2e11d
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 24 deletions.
13 changes: 9 additions & 4 deletions .github/workflows/bench.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,19 @@ jobs:
token: ${{ secrets.GITHUB_TOKEN }}
ref: ${{ github.event_name == 'pull_request_target' && github.event.pull_request.head.sha || github.sha }}

- name: Build devcontainer and run benchmarks
- name: Build devcontainer
uses: devcontainers/[email protected]
with:
imageName: graphql-benchmarks
push: never
runCmd: |
bash ./setup.sh
bash ./run_benchmarks.sh

- name: Setup k6
uses: grafana/setup-k6-action@v1

- name: Run benchmarks
run: |
bash ./setup.sh
bash ./run_benchmarks.sh
- name: Print benchmark results
run: cat ./results.md
Expand Down
52 changes: 52 additions & 0 deletions k6/bench.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import http from 'k6/http';
import { check } from 'k6';

const whichBenchmark = Number(__ENV.BENCHMARK);

export const options = {
scenarios: {
posts: {
executor: 'constant-vus',
duration: whichBenchmark === 2 ? '30s' : '10s',
gracefulStop: '0s',
vus: 100,
}
},
cloud: {
name: __ENV.TEST_NAME + '-' + whichBenchmark,
},
};

const url = 'http://localhost:8000/graphql';
const params = {
headers: {
'Connection': 'keep-alive',
'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',
'Content-Type': 'application/json',
},
};

export default function() {
const payload = JSON.stringify({
operationName: null,
variables: {},
query: whichBenchmark === 2 ? '{posts{id,userId,title,user{id,name,email}}}' : '{posts{title}}',
});

const res = http.post(url, payload, params);
check(res, {
'status is 200': (r) => r.status === 200,
});
}

export function handleSummary(data) {
const med_request_count = data.metrics.http_reqs.values.count;
const avg_latency = data.metrics.http_req_duration.values.avg;
const trimmed_avg_latency = Math.round(avg_latency * 100) / 100;
const request_count_message = `Requests/sec: ${med_request_count}\n`;
const latency_message = `Latency: ${trimmed_avg_latency} ms\n`;

return {
stdout: request_count_message + latency_message,
};
}
5 changes: 5 additions & 0 deletions k6/bench.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
test_name=$1
benchmark=$2

# k6 run k6/bench.js --quiet --out cloud --env TEST_NAME=$test_name --env BENCHMARK=$benchmark
k6 run k6/bench.js --quiet --env TEST_NAME=$test_name --env BENCHMARK=$benchmark
1 change: 1 addition & 0 deletions k6/warmup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
k6 run k6/bench.js --quiet
18 changes: 10 additions & 8 deletions run_benchmarks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ sh nginx/run.sh
function runBenchmark() {
killServerOnPort 8000
sleep 5
local serviceScript="$1"
local service="$1"
local benchmarks=(1 2)
local serviceScript="graphql/${service}/run.sh"

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

for bench in "${benchmarks[@]}"; do
local benchmarkScript="wrk/bench${bench}.sh"

local benchmarkScript="tests/test.sh"
local warmupScript="tests/warmup.sh"

# Replace / with _
local sanitizedServiceScriptName=$(echo "$serviceScript" | tr '/' '_')

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

# Warmup run
bash "$benchmarkScript" > /dev/null
bash "$warmupScript" > /dev/null
sleep 1 # Give some time for apps to finish in-flight requests from warmup
bash "$benchmarkScript" > /dev/null
bash "$warmupScript" > /dev/null
sleep 1
bash "$benchmarkScript" > /dev/null
bash "$warmupScript" > /dev/null
sleep 1


# 3 benchmark runs
for resultFile in "${resultFiles[@]}"; do
echo "Running benchmark $bench for $serviceScript"
bash "$benchmarkScript" > "bench${bench}_${resultFile}"
bash "$benchmarkScript" "$service" "$bench" > "bench${bench}_${resultFile}"
if [ "$bench" == "1" ]; then
bench1Results+=("bench1_${resultFile}")
else
Expand All @@ -61,7 +63,7 @@ function runBenchmark() {
rm "results.md"

for service in "apollo_server" "caliban" "netflix_dgs" "gqlgen" "tailcall" "async_graphql"; do
runBenchmark "graphql/${service}/run.sh"
runBenchmark "$service"
if [ "$service" == "apollo_server" ]; then
cd graphql/apollo_server/
npm stop
Expand Down
1 change: 0 additions & 1 deletion wrk/bench1.sh

This file was deleted.

1 change: 0 additions & 1 deletion wrk/bench2.sh

This file was deleted.

5 changes: 0 additions & 5 deletions wrk/wrk1.lua

This file was deleted.

5 changes: 0 additions & 5 deletions wrk/wrk2.lua

This file was deleted.

0 comments on commit fd2e11d

Please sign in to comment.