I currently have a test which calls functions in other files. I want to be able to set different thresholds for each of the functions. Is this possible?
I have included an example below but I have made it really generic so it didn’t include any sensitive data from my company.
This is the test that I execute in the command line:
import { createUser, createPost, createComment } from "./user-load-test.js";
import { createExternalBooking, createInternalBooking } from "./booking-load-test.js";
export const options = {
vus: 10,
duration: "10s"
};
export default function () {
var userId = createUser();
createPost(userId);
createComment(userId);
createExternalBooking(userId);
createInternalBooking(userId);
}
This is an example of the user-load-test.js and I also have another file with functions that I would need to set the thresholds for.
import http from "k6/http";
import { sleep } from "k6";
import { Rate, Trend } from "k6/metrics";
const CreateUserTrend = new Trend("Create user", true);
const CreateUserErrorRate = new Rate("Create user errors", true);
const CreatePostTrend = new Trend("Create post", true);
const CreatePostErrorRate = new Rate("Create post errors", true);
const CreatePostTrend = new Trend("Create post", true);
const CreatePostErrorRate = new Rate("Create post errors", true);
const params = {
headers: {
"Content-Type": "application/json",
},
};
export function createUser() {
let createUserRes = http.post(
'http://localhost:1111/api/user,
JSON.stringify({
firstName: "Joe",
surname: "Bloggs",
emailAddress: "test@test.com",
}),
params
);
check(createUserRes, {
'is status 200': (r) => r.status === 200
}) || CreateUserErrorRate.add(1);
CreateUserTrend.add(createUserRes.timings.duration);
}
export function createPost(userId) {
let createPostRes = http.post(
'http://localhost:1111/api/post,
JSON.stringify({
user: "userId,
postText: "This is a test"
}),
params
);
check(createPostRes, {
'is status 200': (r) => r.status === 200
}) || CreatePostErrorRate.add(1);
CreatePostTrend.add(createPostRes.timings.duration);
}