Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/grafana/openapi-to-k6
A CLI tool to convert an OpenAPI schema to a TypeScript client to use with k6.
https://github.com/grafana/openapi-to-k6
api-development api-testing code-generation devops k6 load-testing openapi performance-testing test-automation testing-tools
Last synced: about 1 month ago
JSON representation
A CLI tool to convert an OpenAPI schema to a TypeScript client to use with k6.
- Host: GitHub
- URL: https://github.com/grafana/openapi-to-k6
- Owner: grafana
- License: agpl-3.0
- Created: 2024-10-07T08:32:10.000Z (3 months ago)
- Default Branch: main
- Last Pushed: 2024-11-07T21:59:42.000Z (about 1 month ago)
- Last Synced: 2024-11-07T22:30:01.376Z (about 1 month ago)
- Topics: api-development, api-testing, code-generation, devops, k6, load-testing, openapi, performance-testing, test-automation, testing-tools
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@grafana/openapi-to-k6
- Size: 354 KB
- Stars: 8
- Watchers: 5
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
⚠️
This tool is currently in the experimental stage. Expect bugs, incomplete features, and breaking changes as development progresses. Use at your own risk, and please report any issues or feedback to help us improve.
## Overview
The _openapi-to-k6_ repository is a tool designed to ease the process of writing k6 scripts.
It generates a TypeScript client from OpenAPI specification which can be imported in your k6 script to
easily call your endpoints and have auto completion in your IDE.This allows developers to easily create performance tests for their APIs based on their existing
OpenAPI documentation.Along with the client, it also generates a sample k6 script as an example of how to use the client.
The generated client exports a class with methods for each endpoint in the OpenAPI specification. You can create
a instance of the class and use the methods to call the endpoints.To take a look at a few examples of how the generated client looks and sample script looks, check out the [examples](./examples) directory.
## Getting started
1. Install the tool globally via
```shell
npm install -g @grafana/openapi-to-k6
```2. To start using the tool either give path to your OpenAPI schema file or provide a URL to your Open
API schema and the output path where you want to generate the client files.```shell
openapi-to-k6
```This will the generate a TypeScript client and a sample k6 script in the corresponding directory.
You can also supply the optional flag `--include-sample-script` to also generate a sample k6 script
along with the client.💡 _Note_: The tool supports both JSON and YAML format for OpenAPI schema.
### Options
Following are some of the configuration options supported by the tool.
1. `--mode` or `-m`: Specify the mode to use for generating the client. Following are available options:
1. `single`: This is the default mode used is nothing is specified. It generated the TypeScript client as a single file with all the types and implementation in a single file.
2. `split`: This mode splits the types and implementation into separate files.
3. `tags`: This modes splits your OpenAPI schema based on the tags and generates a separate client for each tag. If a route has no tag set, it will be available in `default.ts` file.To check how the output looks for each mode, check out the [examples](./examples) directory.
2. `--disable-analytics`: Disable anonymous usage analytics reporting which helping making the tool better. You can also set an environment variable `DISABLE_ANALYTICS=true` to disable the analytics.
3. `--include-sample-script`: Generate a sample k6 script.
4. `--verbose` or `-v` : Enable verbose logging to see more detailed logging output.
5. `--help` or `-h` : Show help message.## Developing locally
1. Clone the repository
```shell
git clone https://github.com/grafana/openapi-to-k6
```2. Install dependencies
```shell
npm install
```3. Run the sdk generator from source
```shell
npm run dev
```This will generate the SDK files in the corresponding directory.
4. Import them in you k6 script and run the script using the following command
```shell
k6 run --compatibility-mode=experimental_enhanced .ts
```Note: `--compatibility-mode` is needed to use a typescript file as K6 script. To know more about it, click [here](https://grafana.com/docs/k6/latest/using-k6/javascript-typescript-compatibility-mode/).
## Running E2E tests
We have some end-to-end tests to ensure the generated SDK works as expected. To run these tests, you can use the following command:
1. Navigate to the test directory
```shell
cd tests/e2e/
```2. Use Mockoon CLI to start the mock server which will create a mock server for the endpoints defined in the OpenAPI specification.
This will run the mock server in a docker container in background.```shell
docker run -v ./schema.json:/tmp/schema.json -p 3000:3000 mockoon/cli:latest -d /tmp/schema.json
```3. Assuming you have already followed previous steps and have the environment set up, you can generate the SDK by using
```shell
npm run dev -- ./schema.json ./sdk.ts
```4. Run the K6 script
```shell
k6 run --compatibility-mode=experimental_enhanced ./K6Script.ts
```## Packaging
1. Run the command `npm run build` to package the project together for distribution.
2. Install the compiled package locally by using `npm install .` or `npm install -g .`.
3. Use the CLI `k6-sdkgen `Special mention for the the open-source library [Orval](https://orval.dev/) which is used for the generation of the TypeScript client.