From f4baaf5985962409b1667cca452eb85b03c96961 Mon Sep 17 00:00:00 2001 From: Anna Mukharram Date: Tue, 12 Sep 2023 19:21:15 +0400 Subject: [PATCH] fix: k6 with ts, add description in readme, removed unnecessary scripts --- README.md | 30 ++++++++++++++++++++++++++++-- benchmarks/1-vu.script.js | 25 ------------------------- benchmarks/1-vu.script.ts | 19 +++++++++++++++++++ benchmarks/415.script.js | 29 ----------------------------- benchmarks/415.script.ts | 23 +++++++++++++++++++++++ benchmarks/batch.script.js | 25 ------------------------- benchmarks/multiple-vu.script.js | 22 ---------------------- package.json | 3 ++- tsconfig.json | 2 +- yarn.lock | 5 +++++ 10 files changed, 78 insertions(+), 105 deletions(-) delete mode 100644 benchmarks/1-vu.script.js create mode 100644 benchmarks/1-vu.script.ts delete mode 100644 benchmarks/415.script.js create mode 100644 benchmarks/415.script.ts delete mode 100644 benchmarks/batch.script.js delete mode 100644 benchmarks/multiple-vu.script.js diff --git a/README.md b/README.md index 757035a6..a3012d87 100644 --- a/README.md +++ b/README.md @@ -900,8 +900,34 @@ Pay attention that API by default running job for fetching and updating Validato ## Benchmarks -To see record output in file and send metrics to prometheus run command -`K6_PROMETHEUS_RW_SERVER_URL=http://localhost:9090/api/v1/write k6 run --out json=benchmarks/output.json -o experimental-prometheus-rw --tag testid= benchmarks/200.script.js` +This part of document outlines running benchmarks and collecting Prometheus metrics using K6. + +At first install [k6](https://k6.io/docs/get-started/installation/). + +Run `yarn build`. +Run KAPI `yarn start:dev`. + +Running Benchmarks: Execute this command to run benchmarks and save results in benchmarks/output.json: + +```bash +k6 run -e PORT= --out json=benchmarks/output.json --tag testid= dist/benchmarks/.script.js +``` + +Replace with your unique test identifier. + +Collecting Prometheus Metrics: For collecting benchmarks' Prometheus metrics, set the Prometheus server URL: + +```bash +export K6_PROMETHEUS_RW_SERVER_URL=http:///api/v1/write +``` + +Replace with your Prometheus server's address. Then, execute benchmarks and collect Prometheus metrics: + +```bash +k6 run -e PORT= --out json=benchmarks/output.json -o experimental-prometheus-rw --tag testid= dist/benchmarks/.script.js +``` + +Once again, use to differentiate your test run in the results. ## Release flow diff --git a/benchmarks/1-vu.script.js b/benchmarks/1-vu.script.js deleted file mode 100644 index 36271210..00000000 --- a/benchmarks/1-vu.script.js +++ /dev/null @@ -1,25 +0,0 @@ -import http from "k6/http"; -import { check, sleep } from "k6"; -import { describe } from 'https://jslib.k6.io/expect/0.0.4/index.js'; - -// Test configuration -export const options = { - thresholds: { - // http request will be crossed with - http_req_duration: ["p(10) > 30000"], - }, - // better to make it longer to cross with validators update - // we will need this benchmark to check how long 1 request is handled depends on kapi job execution - duration: "80s", - vus: 1 -}; - -// Simulated user behavior -export default function () { - // this endpoint process the - describe('Test /keys endpoint', () => { - let res = http.get("http://localhost:3000/v1/keys"); - // Validate response status - check(res, { "status was 200": (r) => r.status == 200 }); - }) -} diff --git a/benchmarks/1-vu.script.ts b/benchmarks/1-vu.script.ts new file mode 100644 index 00000000..4e3dbb85 --- /dev/null +++ b/benchmarks/1-vu.script.ts @@ -0,0 +1,19 @@ +import { get } from 'k6/http'; +import { check } from 'k6'; + +// Test configuration +export const options = { + thresholds: { + http_req_duration: ['p(10) > 30000'], + }, + // better to make it longer to cross with validators update + duration: '80s', + vus: 1, +}; + +// Simulated user behavior +export default function test() { + const res = get(`http://localhost:${__ENV.PORT}/v1/keys`); + // Validate response status + check(res, { 'Should return response with status 200': (r) => r.status == 200 }); +} diff --git a/benchmarks/415.script.js b/benchmarks/415.script.js deleted file mode 100644 index 56fc7702..00000000 --- a/benchmarks/415.script.js +++ /dev/null @@ -1,29 +0,0 @@ -import http from "k6/http"; -import { check } from "k6"; -import { describe } from 'https://jslib.k6.io/expect/0.0.4/index.js'; -import { FormData } from 'https://jslib.k6.io/formdata/0.0.2/index.js'; - -// Test configuration -export const options = { - duration: "60s", - vus: 8 -}; - -export default function () { - describe('Test unsupported content/type', () => { - const fd = new FormData(); - fd.append('text', http.file('test', 'test.txt', 'text/plain')); - - const params = { - headers: { - 'Content-Type': 'multipart/form-data; boundary=' + fd.boundary - }, - }; - - let res = http.post("http://localhost:3000/v1/keys", fd.body(), params); - - check(res, { - 'is status 415': (r) => r.status === 415, - }); - }) -} diff --git a/benchmarks/415.script.ts b/benchmarks/415.script.ts new file mode 100644 index 00000000..6a25efba --- /dev/null +++ b/benchmarks/415.script.ts @@ -0,0 +1,23 @@ +import { post, file } from 'k6/http'; +import { check } from 'k6'; + +// Test configuration +export const options = { + duration: '60s', + vus: 1, +}; + +export default function test() { + const data = { + field: 'this is a standard form field', + file: file('some data', 'test.bin'), + }; + + console.log('Test unsupported content/type'); + + const res = post(`http://localhost:${__ENV.PORT}/v1/keys`, data); + + check(res, { + 'Should return response with status 415': (r) => r.status === 415, + }); +} diff --git a/benchmarks/batch.script.js b/benchmarks/batch.script.js deleted file mode 100644 index ba18bc5b..00000000 --- a/benchmarks/batch.script.js +++ /dev/null @@ -1,25 +0,0 @@ -import http from "k6/http"; -import { check, sleep } from "k6"; -import { describe } from 'https://jslib.k6.io/expect/0.0.4/index.js'; - -// Test configuration -export const options = { - duration: "60s", -}; - -const url = 'http://localhost:3000/v1/keys' -const parallelRequestsNum = 5; - -// Simulated user behavior -export default function () { - // always get OOM error with this test - // if decrease duration to 30s, will not get OOM error - describe('Test parallel requests to /keys', () => { - let requests = Array(parallelRequestsNum).fill(['GET', url]) - let responses = http.batch(requests) - // Validate response status - for (let resp of responses) { - check(resp, { "status was 200": (r) => r.status == 200 }); - } - }) -} diff --git a/benchmarks/multiple-vu.script.js b/benchmarks/multiple-vu.script.js deleted file mode 100644 index 13dc14f4..00000000 --- a/benchmarks/multiple-vu.script.js +++ /dev/null @@ -1,22 +0,0 @@ -import http from "k6/http"; -import { check, sleep } from "k6"; -import { describe } from 'https://jslib.k6.io/expect/0.0.4/index.js'; - -// Test configuration -export const options = { - duration: "60s", - vus: 10 -}; - -// sometime get Unable to acquire a connection -// DriverException: Unable to acquire a connection - -// Simulated user behavior -export default function () { - // this endpoint process the - describe('Test /keys endpoint', () => { - let res = http.get("http://localhost:3000/v1/keys"); - // Validate response status - check(res, { "status was 200": (r) => r.status == 200 }); - }) -} diff --git a/package.json b/package.json index 3550b9c4..dd48d9bf 100644 --- a/package.json +++ b/package.json @@ -72,7 +72,8 @@ "reflect-metadata": "^0.1.13", "rimraf": "^3.0.2", "rxjs": "^7.5.2", - "typechain": "^7.0.0" + "typechain": "^7.0.0", + "@types/k6": "~0.44.2" }, "devDependencies": { "@nestjs/cli": "^8.2.5", diff --git a/tsconfig.json b/tsconfig.json index 984727eb..ebd33a96 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -19,5 +19,5 @@ "forceConsistentCasingInFileNames": false, "noFallthroughCasesInSwitch": false, }, - "include": ["src/**/*", "test/**/*"], + "include": ["src/**/*", "test/**/*", "benchmarks/*"], } diff --git a/yarn.lock b/yarn.lock index 27fea475..e2503a3a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1582,6 +1582,11 @@ resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee" integrity sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ== +"@types/k6@~0.44.2": + version "0.44.3" + resolved "https://registry.yarnpkg.com/@types/k6/-/k6-0.44.3.tgz#bbd6c7ebed1970f537fce8d46af41d3d19631ac2" + integrity sha512-Ulu+xGZZQhZfk3hpLRo7oj69oSMBUfK7+FYdUtD3Dsj+nPyWQAH6j/GX5rLuHJ4SWZkdyEZ7Xmam7tBM8G2Srw== + "@types/node-fetch@^2.5.12": version "2.6.2" resolved "https://registry.yarnpkg.com/@types/node-fetch/-/node-fetch-2.6.2.tgz#d1a9c5fd049d9415dce61571557104dec3ec81da"