Want to obtain individual transaction response time time

I am new to K6. I have the following script and I want to get the individual response time.

import http from "k6/http";
import { check, sleep, group } from "k6";

export let options = {
  stages: [
    { duration: "2s", target: 3 }, // simulate ramp-up of traffic from 1 to 3 virtual users over 5s.
    { duration: "5s", target: 4 }, // stay at 4 virtual users for 10s
    { duration: "2s", target: 0 }, // ramp-down to 0 users
  ],
  thresholds: {
    'http_req_duration': ['p(95)<500'], // 95% of requests must complete below 500ms
    'http_req_failed': ['rate<0.01'],   // Error rate must be below 1%
  },
  ext: {
    loadimpact: {
      projectID: 3629234,
      // Test runs with the same name groups test runs together
      name: "Baseline Tet B2B"
    },
  },
};

function login() {
  let authResponse = http.post(
    "<auth_url>"
  );
  return authResponse.json().data;
}

function generateRandomSleepTimer(){
  let sleepTime = Math.floor((Math.random() * 3) + 1);
  return parseInt(sleepTime);
}

export function fetchOrderDetails(authToken, sleepTime) {
  group("TC01 - Fetch Order Details", function() {
    const start = new Date().getTime();
    const response = http.get(
      "<get_request_01>",
      {
        headers: {
          Authorization: authToken,
        },
        tags: { name: "Fetch Order Details" },
      }
    );
    const duration = new Date().getTime() - start;
    const checkResult = check(response, {
      "is status 200": (r) => r.status === 200,
    });
    const responseMessage = response.json();
    check(responseMessage, {
        "Success!": (r) => r.message === "Success!",
    });
    console.log(`TC01 - Fetch Order Details: ${checkResult ? "PASSED" : "FAILED"} ${responseMessage ? "PASSED" : "FAILED"} - Duration: ${duration} ms`);
    sleep(sleepTime);
  });
}

export function fetchListOfOrganisations(authToken, sleepTime) {
  group("TC02 - Retrieve a list of organisations", function() {
    const start = new Date().getTime();
    const response = http.get(
      "<get_request_02>",
      {
        headers: {
          Authorization: authToken,
        },
        tags: { name: "Retrieve a list of organisations" }
      }
    );
    const duration = new Date().getTime() - start;
    const checkResult = check(response, {
      "is status 200": (r) => r.status === 200,
    });
    const responseMessage = response.json();
        check(responseMessage, {
            "Success!": (r) => r.message === "Success!",
        });
    console.log(`TC02 - Retrieve a list of organisations: ${checkResult ? "PASSED" : "FAILED"} ${responseMessage ? "PASSED" : "FAILED"} - Duration: ${duration} ms`);
    sleep(sleepTime);
  });
}


export default function () {
  const authToken = login();
  const sleepTime = generateRandomSleepTimer();
  fetchOrderDetails(authToken, sleepTime);
  fetchListOfOrganisations(authToken, sleepTime);
}

And the response as a result I am getting

INFO[0001] TC01 - Fetch Order Details: PASSED PASSED - Duration: 347 ms  source=console
INFO[0002] TC01 - Fetch Order Details: PASSED PASSED - Duration: 242 ms  source=console
INFO[0003] TC01 - Fetch Order Details: PASSED PASSED - Duration: 220 ms  source=console
INFO[0003] TC02 - Retrieve a list of organisations: PASSED PASSED - Duration: 131 ms  source=console
INFO[0003] TC02 - Retrieve a list of organisations: PASSED PASSED - Duration: 361 ms  source=console
INFO[0004] TC01 - Fetch Order Details: PASSED PASSED - Duration: 139 ms  source=console
INFO[0005] TC02 - Retrieve a list of organisations: PASSED PASSED - Duration: 146 ms  source=console
INFO[0006] TC01 - Fetch Order Details: PASSED PASSED - Duration: 116 ms  source=console
INFO[0006] TC02 - Retrieve a list of organisations: PASSED PASSED - Duration: 125 ms  source=console
INFO[0007] TC02 - Retrieve a list of organisations: PASSED PASSED - Duration: 119 ms  source=console
INFO[0007] TC01 - Fetch Order Details: PASSED PASSED - Duration: 102 ms  source=console
INFO[0008] TC01 - Fetch Order Details: PASSED PASSED - Duration: 193 ms  source=console
INFO[0008] TC01 - Fetch Order Details: PASSED PASSED - Duration: 277 ms  source=console
INFO[0008] TC02 - Retrieve a list of organisations: PASSED PASSED - Duration: 234 ms  source=console
INFO[0009] TC02 - Retrieve a list of organisations: PASSED PASSED - Duration: 295 ms  source=console
INFO[0009] TC01 - Fetch Order Details: PASSED PASSED - Duration: 268 ms  source=console
INFO[0009] TC02 - Retrieve a list of organisations: PASSED PASSED - Duration: 108 ms  source=console
INFO[0011] TC02 - Retrieve a list of organisations: PASSED PASSED - Duration: 160 ms  source=console

     █ TC01 - Fetch Order Details

       ✓ is status 200
       ✓ Success!

     █ TC02 - Retrieve a list of organisations

       ✓ is status 200
       ✓ Success!

     checks.........................: 100.00% ✓ 36       ✗ 0
     data_received..................: 73 kB   5.6 kB/s
     data_sent......................: 18 kB   1.4 kB/s
     group_duration.................: avg=1.65s    min=1.11s med=1.29s    max=2.37s    p(90)=2.3s     p(95)=2.36s
     http_req_blocked...............: avg=25.84ms  min=0s    med=0s       max=145.24ms p(90)=78.33ms  p(95)=88.24ms
     http_req_connecting............: avg=5.74ms   min=0s    med=0s       max=20.05ms  p(90)=19.49ms  p(95)=19.83ms
   ✓ http_req_duration..............: avg=210.61ms min=102ms med=231.76ms max=360.43ms p(90)=303.52ms p(95)=344.75ms
       { expected_response:true }...: avg=210.61ms min=102ms med=231.76ms max=360.43ms p(90)=303.52ms p(95)=344.75ms
   ✓ http_req_failed................: 0.00%   ✓ 0        ✗ 27
     http_req_receiving.............: avg=49.72µs  min=0s    med=0s       max=696.3µs  p(90)=36.72µs  p(95)=415.61µs
     http_req_sending...............: avg=24.13µs  min=0s    med=0s       max=512.2µs  p(90)=0s       p(95)=97.64µs
     http_req_tls_handshaking.......: avg=17.87ms  min=0s    med=0s       max=82.56ms  p(90)=58.2ms   p(95)=58.71ms
     http_req_waiting...............: avg=210.54ms min=102ms med=231.76ms max=360.43ms p(90)=303.52ms p(95)=344.37ms
     http_reqs......................: 27      2.068609/s
     iteration_duration.............: avg=3.61s    min=2.47s med=2.81s    max=5.23s    p(90)=4.85s    p(95)=5.04s
     iterations.....................: 9       0.689536/s
     vus............................: 1       min=1      max=4
     vus_max........................: 4       min=4      max=4

                                                                                                                                                                                                                                     
running (13.1s), 0/4 VUs, 9 complete and 0 interrupted iterations                                                                                                                                                                    
default ✓ [======================================] 0/4 VUs  9s

I want to get individual response time like for TC-01 and TC-02. IN Jmeter If I put them in Transaction Controller I can get individual response time. How to achieve this in K6

Hi
You can define thresholds for groups - see docs here Thresholds

2 Likes

Hi @jyotip :wave:

I’m not entirely certain what you meant by individual transaction response time.

I would suggest looking into using custom tags, groups, or scenarios to segment the results to your taste.

I would also suggest looking into the handleSummary function, where you should be able to modify the standard end-of-test summary of k6 to fit your needs. If you use a separate metrics/dashboard backend such as prometheus+Grafana, you could tag each of your requests and create dedicated graphs for each too :bowing_man:

1 Like

Thank you so much for your reply I achieved this with using Trends

I added following snippet into my code :

let TC01_FetchOrderDetails = new Trend("TC01_FetchOrderDetails");
let TC02_FetchListOfOrganisations = new Trend("TC02_FetchListOfOrganisations");

And the following changes to the code block,

export function fetchOrderDetails(authToken, sleepTime) {
  group("TC01 - Fetch Order Details", function () {
    const response = http.get(`${BASE_URL}/order?leadId=278`, {
      headers: {
        Authorization: authToken,
      },
      tags: { name: "Fetch Order Details" },
    });

    // Measure response duration
    const responseDuration = response.timings.duration;
    TC01_FetchOrderDetails.add(responseDuration);

    // Check if status is 200
    const isStatus200 = check(response, {
      "is status 200": (r) => r.status === 200,
    });

    // Parse JSON response and validate message
    let isSuccess = false;
    let responseMessage = null;
    try {
      responseMessage = response.json();
      isSuccess = check(responseMessage, {
        "Success!": (r) => r.message === "Success!",
      });
    } catch (e) {
      console.error("Failed to parse JSON response:", response.body);
    }

    // Log results
    console.log(
      `TC01 - Fetch Order Details: ${
        isStatus200 && isSuccess ? "PASSED" : "FAILED"
      } - Response Time: ${responseDuration} ms`
    );

    // Sleep for a random duration
    sleep(sleepTime);
  });
}

Now I am getting summary report as

TC01_FetchOrderDetails................: p(95)=214.4225   p(99.9)=227.816846
TC02_FetchListOfOrganisations.........: p(95)=181.964275 p(99.9)=187.977971
2 Likes