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.
Clone the generated repository on your local machine, move to the project root folder and install the dependencies defined in package.json
yarn
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
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.
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
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
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
- raise issue on stdout chars
- showcase browser recorder