How to use thresholds from separate files

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);
 }

Hi @karenc,
sorry for the late response.

It seems you are splitting functions for each single HTTP request so you could indirectly set thresholds for them matching tags on the request, check the section in the documentation regarding it: https://k6.io/docs/using-k6/thresholds/#thresholds-on-tags

Instead, if you are doing metrics like you posted for CreateUser then you could also match a custom metric. The documentation has an example for it: https://k6.io/docs/javascript-api/k6-metrics/counter/#counter-usage-in-thresholds

Let me know if it helps.
Ivan