-1

i have this k6 script but i can't seem to get the errors to show up in my html file although if i console.log i see them

    import http from "k6/http";
    import { sleep, check } from "k6";
    import { htmlReport } from "https://raw.githubusercontent.com/benc-uk/k6-     reporter/main/dist/bundle.js";

    export const options = {
    stages: [
    { duration: "1s", target: 100 }, // Ramp-up to 100 users in 1 minute
    { duration: "3s", target: 200 }, // Ramp-up to 200 users over 3 minutes
    { duration: "5s", target: 500 }, // Sustain 500 users for 5 minutes
    { duration: "2s", target: 100 }, // Ramp-down to 100 users over 2 minutes
    { duration: "1s", target: 0 }, // Gradual cooldown to 0 users in 1 minute
    ],
    thresholds: {
    http_req_duration: ["p(95)<200"], // 95% of requests must complete within 200ms
http_req_failed: ["rate<0.01"], // Less than 1% of requests should fail
},
};

// Initialize shared state for failed requests
let globalFailedRequests = \[\];

export default () =\> {
const url =
"https://test-url/limit=50&langcode=en";
const res = http.get(url);

const success = check(res, {
"status is 200": (r) =\> r.status === 200,
"response time \< 200ms": (r) =\> r.timings.duration \< 200,
});

if (!success) {
globalFailedRequests.push({
url: url,
status: res.status,
error: res.body || "No error message",
responseTime: res.timings.duration,
});

    console.log(
      `Request success: ${success}, Status code: ${res.status}, timing: ${res.timings.duration}`
    );

}

sleep(6); // Simulate delay between requests
};

export function handleSummary(data) {
const failedRequestsHtml = globalFailedRequests
.map(
(req) =\>
`<li><strong>URL:</strong> ${req.url} | <strong>Status:</strong> ${req.status} | <strong>Error:</strong> ${req.error}</li>`
)
.join("");

// Add failed request information to the HTML summary
const additionalHtml = `   <h3>Failed Requests</h3>     <ul>${failedRequestsHtml || "<li>No failed requests</li>"}</ul> `;

// Modify the final HTML report by appending the additional HTML
const reportWithFailures = `
  <!DOCTYPE html>
  <html lang="en">
  <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Load Test Summary Report</title>
      <style>
        body { font-family: Arial, sans-serif; }
        h1 { color: #333; }
      </style>
  </head>
  <body>
      <h1>Load Test Summary Report</h1>
      ${htmlReport(data)}
      ${additionalHtml}
  </body>
  </html>
`;

// Return the modified HTML report
return {
"getLoad.html": reportWithFailures, // Generate HTML report with failed requests
};}

the console.log shows the errors but it seems they don't get added to the array in handleSummary() this is an example of an error i seen to be able to view in my terminal Request success: false, Status code: 200, timing: 28824.404

i am also able to output the errors in a json file but i need them to be within the html file in this usecase

1 Answer 1

0

Every VU (virtual user) in k6 gets their own "state" (they are completely separate; think of it as distinct processes). Since k6 could also run a distributed test, different VUs are not even guaranteed to run on the same machine.

So unfortunately, what you want to do is not possible with k6 out of the box: You cannot share state between different VUs. handleSummary runs in its own context, similar to the init context. That means you cannot access any values that you might have set from a VU outside that VU.

If you want to publish or persist state, you must do so external to k6. You could use Redis to persist your objects or you could POST them to a web service.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.