|
| 1 | +import { check } from "k6"; |
| 2 | +import http from "k6/http"; |
| 3 | +import { textSummary } from 'https://jslib.k6.io/k6-summary/0.0.2/index.js'; |
| 4 | + |
| 5 | +const now = new Date(); |
| 6 | +const stamp = '2_12__xxxx__' + now.getFullYear() + |
| 7 | + String(now.getMonth() + 1).padStart(2, '0') + |
| 8 | + String(now.getDate()).padStart(2, '0') + |
| 9 | + String(now.getHours()).padStart(2, '0') + |
| 10 | + String(now.getMinutes()).padStart(2, '0'); //__ENV.STAMP; |
| 11 | +const tag = "localhost" //__ENV.TAG; |
| 12 | +const records = 50; //Number(__ENV.RECORDS || "10") |
| 13 | +const duration = "60s"; //__ENV.DURATION || "60s"; |
| 14 | +const target = 100; //Number(__ENV.TARGET || "100"); |
| 15 | + |
| 16 | +const url = 'http://localhost:5000/api/test-data' + "?" + |
| 17 | + Object.entries({ |
| 18 | + _records: records, |
| 19 | + _text_param: 'ABCDEFGHIJKLMNOPRSTUVWXYZ', |
| 20 | + _int_param: 1234567890, |
| 21 | + _ts_param: new Date('2014-12-31').toISOString(), |
| 22 | + _bool_param: true |
| 23 | + }) |
| 24 | + .map(([key, value]) => `${key}=${encodeURIComponent(value)}`) |
| 25 | + .join('&'); |
| 26 | + |
| 27 | +// define configuration |
| 28 | +export const options = { |
| 29 | + // define thresholds |
| 30 | + thresholds: { |
| 31 | + http_req_failed: [{ threshold: "rate<0.01", abortOnFail: true }], // availability threshold for error rate |
| 32 | + http_req_duration: ["p(99)<1000"], // Latency threshold for percentile |
| 33 | + }, |
| 34 | + // define scenarios |
| 35 | + scenarios: { |
| 36 | + breaking: { |
| 37 | + executor: "ramping-vus", |
| 38 | + stages: [ |
| 39 | + { duration: duration, target: target }, |
| 40 | + ], |
| 41 | + }, |
| 42 | + }, |
| 43 | +}; |
| 44 | + |
| 45 | +export default function () { |
| 46 | + const res = http.get(url); |
| 47 | + check(res, { |
| 48 | + [`${tag} status is 200`]: (r) => r.status === 200, |
| 49 | + [`${tag} response is JSON`]: (r) => r.headers['Content-Type'] && r.headers['Content-Type'].includes('application/json'), |
| 50 | + [`${tag} response has data`]: (r) => r.body && JSON.parse(r.body).length > 0, |
| 51 | + }); |
| 52 | +} |
| 53 | + |
| 54 | +export function handleSummary(data) { |
| 55 | + return { |
| 56 | + [`${stamp}_${tag}_summary.txt`]: textSummary(data, { indent: ' ', enableColors: false }), |
| 57 | + [`${stamp}_${tag}.csv`]: `${data.metrics.http_reqs.values.count},${data.metrics.http_reqs.values.rate},${data.metrics.http_req_failed.values.rate}`, |
| 58 | + //[`/results/${stamp}/${tag}_summary.json`]: JSON.stringify(data, null, 2), |
| 59 | + } |
| 60 | +} |
0 commit comments