Last active
October 31, 2021 01:11
-
-
Save agusrichard/ac9e00a6710849a3961aa48d371e98fa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import http from 'k6/http' | |
import { sleep, check } from 'k6' | |
import { Rate, Trend } from 'k6/metrics' | |
// Custom defined metrics | |
const lowerThan2sRate = new Rate('lower_than_2s') | |
const durationInSeconds = new Trend('duration_in_seconds') | |
// This BASE_URL won't work if you're using Docker. | |
// You'll need to know the IP address of the host. | |
// Then replace localhost with the IP address. | |
const BASE_URL = 'http://localhost:3000' | |
export const options = { | |
vus: 1000, // 1000 users will be simulated | |
duration: '1m', // the test will run for 1 minute | |
thresholds: { // you can define threshold here. all criteria of a successful test | |
lower_than_2s: [{ // custom defined metrics | |
threshold: 'rate>0.75', // the result rate should be above 75 percents | |
abortOnFail: true, // if the criteria wasn't met, then the test is aborted | |
}], | |
} | |
} | |
export default function(data) { | |
// We need this to pass the authorization and authentication middleware | |
const params = { | |
headers: { | |
'Content-Type': 'application/json', | |
Authorization: `Bearer ${data.token}` | |
} | |
} | |
data.incomeExpenseTypes.forEach(t => { | |
const payload = { | |
value: 10000, | |
description: 'Test', | |
income_expense_type_id: t.id, | |
is_income: false | |
} | |
const res = http.post(`${BASE_URL}/income-expense`, JSON.stringify(payload), params) | |
// this check function will run at each iteration | |
// making sure that the checks criteria is met | |
check(res, { | |
'is success': (r) => r.json().success, | |
'duration below 2s': r => r.timings.duration < 2000 | |
}) | |
// lowerThan2sRate is added by one if the duration is below 2s | |
lowerThan2sRate.add(res.timings.duration < 2000) | |
// we know that the duration is in millisecond | |
// but for demonstartion purposes, we convert it to second | |
durationInSeconds.add(res.timings.duration / 1000) | |
}) | |
// sleep for one second at each iteration | |
sleep(1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment