Skip to content

shavo007/k6-demo

Repository files navigation

TypeScript with k6

banner

https://blog.shanelee.name/2021/12/15/performance-testing-with-k6/

This repository showcases k6 with typescript and generating boilerplate scripts from your OAS (Open API Spec). k6 recently outlined in thoughtworks tech radar tools quadrant.

.github/workflows/push.yml GitHub top language

Prerequisites

Installation

Install dependencies

Clone the generated repository on your local machine, move to the project root folder and install the dependencies defined in package.json

yarn

Usage/Examples

Running the test

To run a test written in TypeScript, we first have to transpile the TypeScript code into JavaScript and bundle the project

yarn bundle

This command creates the final test files to the ./dist folder.

Once that is done, we can run our script the same way we usually do, for instance:

k6 run dist/test1.js

OAS integration

Showcase gen k6 script from OAS (Open API Spec)

Generate k6s scripts via open api generator and the oas file. Can run the CLI via the docker image.

docker pull openapitools/openapi-generator-cli

docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli generate \
    -i /local/oas3.yaml \
    -g k6 \
    -o /local/k6-oas3/ \
    --skip-validate-spec

This generates script.js and is a great start to help support defining your perf test cases.

NB It is boilerplate so will need to be refined after for re-use

This auto-generation of the load test script will help streamline the API testing process, keeping on par with the latest changes to their APIs and specifications.

Test running against the API

docker run -d -p8090:8081 shanelee007/greetings-api:latest #run greetings API
yarn bundle
k6 run -e ENV=local dist/greetings.js
yarn html #generate a html report

OAuth2 integration

Showcase gen types from open api generator and k6 scripts for bpay API

  • Generate typescript client sdk (for types)
  • Generate k6 scripts from OAS
# gen client sdk from OAS using generator typescript
docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli generate \
    -i /local/bpay/oas3.yaml \
    -g typescript \
    -o /local/bpay/bpay-client/ \
    --additional-properties=supportsES6=true,platform=node \
    --skip-validate-spec

# OR generate types from OAS via openapi-typescript
npx openapi-typescript bpay/oas3.yaml --output bpay/types/schema.ts
npx openapi-typescript bpay/oas3.yaml --output src/bpay/schema.ts

# gen k6 scripts
docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli generate \
    -i /local/bpay/oas3.yaml \
    -g k6 \
    -o /local/bpay/k6/ \
    --skip-validate-spec

#run load test against bpay api
yarn bundle
CLIENT_ID=REDACTED CLIENT_SECRET=REDACTED k6 run dist/bpay.js

Load testing with influxdb and grafana

docker compose up -d #run the services in the background
docker-compose run -v \
    $PWD/dist:/scripts \
    k6 run -e ENV=docker  /scripts/greetings.js

Access grafana: open http://localhost:3000 and verify connection to influxdb datasource. Access the pre-defined dashboard and set interval to the past 5 mins. You should see similar outline to below

Grafana dashboard

Resources

TODO

  • raise issue on stdout chars
  • showcase browser recorder