Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ZribiHaythem authored Jun 24, 2024
0 parents commit 39fa461
Show file tree
Hide file tree
Showing 21 changed files with 4,874 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"presets": ["@babel/env", "@babel/typescript"],
"plugins": [
"@babel/proposal-class-properties",

"@babel/proposal-object-rest-spread"
]
}
28 changes: 28 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Test Scenario
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Use Node.js 14.x
uses: actions/setup-node@v1
with:
node-version: 14.x
- name: setup config
run: mv tests/config/configExample.ts tests/config/config.ts
- name: Setup test Env
run: |
yarn install
yarn build
docker-compose up -d
- name: run
# In GitHub Actions, if pseudo-tty is assigned in case of docker compose run,
# container execution will continue and step will not be completed, so disable it with -T option.
run: docker compose run -T k6 run --compatibility-mode=base - < ./dist/httpGet.js
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.idea
dist
tests/config/config.ts
node_modules
influxdb
!.gitkeep
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 tubone(Yu Otsubo)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
112 changes: 112 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# K6 with TypeScript on Docker

> Create a scenario for the k6 load testing tool using TypeScript, and run the scenario you created on K6 built on Docker.
## Quickstart

This repository is a public template and can be used as your own GitHub repository by following the link below.

[Create it with Template](https://github.com/tubone24/k6_template_with_typescript_on_docker/generate)

## background

K6 can be used to easily perform load testing, but there is not much information available on templates that implement the following, so we have created this one.

- K6 with TypeScript
- K6 run on Docker
- (K6 scenario with cookie auth)

## Setup

The configuration required for the K6 scenario is defined in tests/config/config.ts.

Copy and paste configExample.ts into the file.

## How to use

### Add Scenario

Describe the K6 test scenario in TypeScript and place it under `test/script`.

## Run

First, use the compose up command to start [InfluxDB](https://www.influxdata.com/) for storing k6 execution results and [Grafana](https://grafana.com/) for visualization.

Specify a test scenario file as the argument to `yarn start`. File extension is **not** required.

After the transpile is executed in TSC, it will be entered into the k6 container and the test will be executed.

```
yarn install
docker compose up -d
yarn start testFileNoExtension
```

### Result Example

```
yarn run v1.22.11
$ sh -c "webpack && docker compose run k6 run --compatibility-mode=base - < ./dist/${0}.js" httpGet
asset cookieAuthServerSession.js 5.3 KiB [emitted] (name: cookieAuthServerSession)
asset httpGet.js 2.63 KiB [emitted] (name: httpGet)
runtime modules 1.83 KiB 8 modules
orphan modules 451 bytes [orphan] 4 modules
built modules 2.52 KiB [built]
./tests/scripts/cookieAuthServerSession.ts + 4 modules 2.38 KiB [not cacheable] [built] [code generated]
./tests/scripts/httpGet.ts + 1 modules 141 bytes [not cacheable] [built] [code generated]
webpack 5.35.1 compiled successfully in 619 ms
failed to get console mode for stdin: The handle is invalid.
failed to get console mode for stdin: The handle is invalid.
failed to get console mode for stdin: The handle is invalid.
/\ |‾‾| /‾‾/ /‾‾/
/\ / \ | |/ / / /
/ \/ \ | ( / ‾‾\
/ \ | |\ \ | (‾) |
/ __________ \ |__| \__\ \_____/ .io
execution: local
script: -
output: InfluxDBv1 (http://influxdb:8086)
scenarios: (100.00%) 1 scenario, 1 max VUs, 10m30s max duration (incl. graceful stop):
* default: 1 iterations for each of 1 VUs (maxDuration: 10m0s, gracefulStop: 30s)
running (00m00.9s), 0/1 VUs, 1 complete and 0 interrupted iterations
default ✓ [ 100% ] 1 VUs 00m00.9s/10m0s 1/1 iters, 1 per VU
data_received..................: 21 kB 24 kB/s
data_sent......................: 519 B 574 B/s
http_req_blocked...............: avg=734.31ms min=734.31ms med=734.31ms max=734.31ms p(90)=734.31ms p(95)=734.31ms
http_req_connecting............: avg=161.49ms min=161.49ms med=161.49ms max=161.49ms p(90)=161.49ms p(95)=161.49ms
http_req_duration..............: avg=169.36ms min=169.36ms med=169.36ms max=169.36ms p(90)=169.36ms p(95)=169.36ms
{ expected_response:true }...: avg=169.36ms min=169.36ms med=169.36ms max=169.36ms p(90)=169.36ms p(95)=169.36ms
http_req_failed................: 0.00% ✓ 0 ✗ 1
http_req_receiving.............: avg=2.71ms min=2.71ms med=2.71ms max=2.71ms p(90)=2.71ms p(95)=2.71ms
http_req_sending...............: avg=49µs min=49µs med=49µs max=49µs p(90)=49µs p(95)=49µs
http_req_tls_handshaking.......: avg=381.41ms min=381.41ms med=381.41ms max=381.41ms p(90)=381.41ms p(95)=381.41ms
http_req_waiting...............: avg=166.6ms min=166.6ms med=166.6ms max=166.6ms p(90)=166.6ms p(95)=166.6ms
http_reqs......................: 1 1.105208/s
iteration_duration.............: avg=903.84ms min=903.84ms med=903.84ms max=903.84ms p(90)=903.84ms p(95)=903.84ms
iterations.....................: 1 1.105208/s
Done in 6.27s.
```

### Clean DB

If you want to clean up the execution result once, run the following command.

```
yarn clean:db
```

## Analysis

The results of the load test can be visualized with influxdb and grafana set up with docker-compose.

You can visualize vus and duration.

![img](https://i.imgur.com/sX5HtCG.png)
43 changes: 43 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
version: '3.8'

services:
influxdb:
image: influxdb:1.8
ports:
- "8086:8086"
environment:
- INFLUXDB_DB=k6
networks:
- k6net
- grafanaNet
volumes:
- ./influxdb:/var/lib/influxdb

k6:
image: grafana/k6:latest
networks:
- k6net
ports:
- "6565:6565"
environment:
- K6_OUT=influxdb=http://influxdb:8086/k6
volumes:
- ./dist:/scripts

grafana:
image: grafana/grafana:latest
ports:
- "3000:3000"
environment:
- GF_AUTH_ANONYMOUS_ORG_ROLE=Admin
- GF_AUTH_ANONYMOUS_ENABLED=true
- GF_AUTH_BASIC_ENABLED=false
volumes:
- ./grafana:/etc/grafana/provisioning/
networks:
- grafanaNet
networks:
k6net:
driver: bridge
grafanaNet:
driver: bridge
12 changes: 12 additions & 0 deletions grafana/dashboards/dashboard.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

apiVersion: 1

providers:
- name: 'myinfluxdb'
orgId: 1
folder: ''
type: file
disableDeletion: false
editable: true
options:
path: /etc/grafana/provisioning/dashboards
Loading

0 comments on commit 39fa461

Please sign in to comment.