From 7d299e35a76fb7515c133f01b8fdf2a6c14278a4 Mon Sep 17 00:00:00 2001 From: Sahil Yeole Date: Thu, 4 Jul 2024 15:38:24 +0530 Subject: [PATCH] setup k6 cloud to upload results Signed-off-by: Sahil Yeole --- .github/workflows/bench.yml | 11 +++++++++++ k6/bench.js | 7 ++++--- k6/bench.sh | 7 +++++-- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/.github/workflows/bench.yml b/.github/workflows/bench.yml index baf9de73..71da3fb0 100644 --- a/.github/workflows/bench.yml +++ b/.github/workflows/bench.yml @@ -18,6 +18,7 @@ jobs: if: github.event.head_commit.message != 'Update performance results in README.md' env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + K6_CLOUD_API_TOKEN: ${{ secrets.K6_CLOUD_API_TOKEN }} steps: - name: Checkout (GitHub) uses: actions/checkout@v4 @@ -34,6 +35,16 @@ jobs: - name: Setup k6 uses: grafana/setup-k6-action@v1 + - name: Setup k6 cloud environment + if: github.event_name == 'push' && github.ref == 'refs/heads/main' + run: | + if [ -z "$K6_CLOUD_API_TOKEN" ]; then + echo "K6_CLOUD_API_TOKEN is not set, results will not be uploaded to k6 cloud" + else + k6 login cloud --token $K6_CLOUD_API_TOKEN + echo "IS_K6_CLOUD_ENABLED=true" >> $GITHUB_ENV + fi + - name: Run benchmarks run: | bash ./setup.sh diff --git a/k6/bench.js b/k6/bench.js index 1d2da1a1..cc2b8986 100644 --- a/k6/bench.js +++ b/k6/bench.js @@ -2,18 +2,19 @@ import http from 'k6/http'; import { check } from 'k6'; const whichBenchmark = Number(__ENV.BENCHMARK); +const benchmarkName = whichBenchmark === 1 ? 'posts' : 'posts+users'; export const options = { scenarios: { posts: { executor: 'constant-vus', - duration: whichBenchmark === 2 ? '30s' : '10s', + duration: whichBenchmark === 1 ? '10s' : '30s', gracefulStop: '0s', vus: 100, } }, cloud: { - name: __ENV.TEST_NAME + '-' + whichBenchmark, + name: __ENV.TEST_NAME + '-' + benchmarkName, }, }; @@ -30,7 +31,7 @@ export default function() { const payload = JSON.stringify({ operationName: null, variables: {}, - query: whichBenchmark === 2 ? '{posts{id,userId,title,user{id,name,email}}}' : '{posts{title}}', + query: whichBenchmark === 1 ? '{posts{title}}' : '{posts{id,userId,title,user{id,name,email}}}', }); const res = http.post(url, payload, params); diff --git a/k6/bench.sh b/k6/bench.sh index e268b5a2..a0ed1bdb 100644 --- a/k6/bench.sh +++ b/k6/bench.sh @@ -1,5 +1,8 @@ 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 +if [ "$IS_K6_CLOUD_ENABLED" == "true" ]; then + k6 run k6/bench.js --quiet --out cloud --env TEST_NAME=$test_name --env BENCHMARK=$benchmark +else + k6 run k6/bench.js --quiet --env TEST_NAME=$test_name --env BENCHMARK=$benchmark +fi