Graphql test serveral stages and every stage have multiple request

three stages:
stage1:{ duration: ‘1m’, vus: 100 }
stage:2{ duration: ‘1m’, vus: 200 }
stage3: { duration: ‘1m’, vus: 300 }
and every stage have multiple post requests use graphql query
footballquery1 = query football{...}
footballquery2= query football2{...}
racingquery1=query racingquery{...}
racingquery2=query racingquery2{...}

footballquery1 and footballquery2 are same categories
racingquery1 and racingquery2 are same categories

stage1,stage2,stage3 just different vus, but they must do those four post graphqlqueries: footballquery1 , footballquery2, racingquery1, racingquery4

i want to ouput those
stage1 average response time | rate of response time < 6s | error rate(404/500) | request/sec
football
racing

stage2 average response time | rate of response time < 6s | error rate(404/500) | request/sec
football
racing

stage3 average response time | rate of response time < 6s | error rate(404/500) | request/sec
football
racing

I’m a k6 newer and I read k6 tutorials, still can’t solve this problem, please help me, thanks a lot

Hi @solaris !

Welcome to the community forums! :wave:

I think the most proper way of solving your need is using End of test.

However, you achieve something similar to your needs by using the thresholds and tags.

So the following script:

import http from "k6/http";
import { sleep } from "k6";
import { tagWithCurrentStageIndex } from 'https://jslib.k6.io/k6-utils/1.3.0/index.js';

export let options = {
    stages: [
        { target: 5, duration: "5s"},
        { target: 5, duration: "5s"},
        { target: 15, duration: "5s"},
    ],
    thresholds: {
      'http_req_duration{stage:0,sport:football}': ['avg < 200'],
      'http_req_duration{stage:0,sport:racing}': ['avg < 200'],
      'http_reqs{stage:0,sport:football}': ['count>0'],
      'http_reqs{stage:0,sport:racing}': ['count>0'],
      'http_reqs{stage:1,sport:football}': ['count>0'],
      'http_reqs{stage:1,sport:racing}': ['count>0'],
      'http_reqs{stage:2,sport:football}': ['count>0'],
      'http_reqs{stage:2,sport:racing}': ['count>0'],
    }    
};

export default function() {
   tagWithCurrentStageIndex();

   
   http.get("https://test.k6.io/", {
      tags: {sport: "football"}
   })

   http.get("https://test.k6.io/", {
      tags: {sport: "racing"}
   })

   http.get("https://test.k6.io/", {
      tags: {sport: "racing"}
   })
    
   sleep(1);
}

can produce output like:

Let me know if that answers,
Cheers!

thanks for your answer a lot.
I just add some custom metrics, group my post request and config my thresholds
is “post” method add tags to my graphql query post like “get” method.

 group("football", () => {
    const footballResponse = http.post(
      API_BASE_URL,
      JSON.stringify(footballSubgraphqlQuery),
      { headers, tags: { sport: "football"} },
    );
    footballLessThan6sRate.add(footballResponse.timings.duration < 3600);
    footballErrorRate.add(
      footballResponse.status === 500 || footballResponse.status === 404
    );
    footballRequestPerSecond.add(1/60)
  });

Yes, it should, like any other HTTP request.

Sure thing! It seems like you’re dealing with load testing scenarios in k6, and you want to monitor various metrics for GraphQL queries in different stages. It’s a bit complex, but I’ll try to break it down.

Firstly, ensure you’ve defined your queries and stages properly. Then, for each stage, you can use k6’s built-in metrics and custom functions to calculate the desired values. Here’s a rough outline:

javascriptCopy code

import http from 'k6/http';
import { sleep } from 'k6';

// Define your GraphQL queries here
const footballQuery1 = '...'; // Replace with actual query
const footballQuery2 = '...';
const racingQuery1 = '...';
const racingQuery2 = '...';

export const options = {
  stages: [
    { duration: '1m', target: 100 }, // Adjust targets as needed
    { duration: '1m', target: 200 },
    { duration: '1m', target: 300 },
  ],
};

export default function () {
  // Perform GraphQL queries here using http.post

  // Sleep to control the request rate [service](https://servicezone.ae/furniture-movers), if needed
  sleep(1);
}

// Add custom metrics and calculations
export function handleSummary(data) {
  const footballResponseTimes = data.metrics.http_req_duration.filter((metric) => metric.tags.group === 'football');
  const racingResponseTimes = data.metrics.http_req_duration.filter((metric) => metric.tags.group === 'racing');

  // Calculate average response time, rate of response time < 6s, error rate, and request/sec for each stage
  console.log(`Stage 1 - Football: Avg RT: ${average(footballResponseTimes)}, Rate < 6s: ${rateBelowThreshold(footballResponseTimes, 6000)}, Error Rate: ${errorRate(data.metrics.http_req_failed, 404, 500)}, Req/Sec: ${requestsPerSecond(data)}`);

  // Repeat for other stages and categories
}

function average(values) {
  // Calculate average of an array of values
  return values.reduce((sum, value) => sum + value.value, 0) / values.length;
}

function rateBelowThreshold(values, threshold) {
  // Calculate the rate of values below a threshold
  return values.filter((value) => value.value < threshold).length / values.length;
}

function errorRate(failures, ...statusCodes) {
  // Calculate error rate for specified status codes
  const errorCount = failures.filter((failure) => statusCodes.includes(failure.tags.status)).length;
  return errorCount / failures.length;
}

function requestsPerSecond(data) {
  // Calculate requests per second
  return data.metrics.http_reqs.rate;
}

This is a basic template to get you started. You may need to adapt it based on your specific service queries and requirements. Feel free to ask if you have more questions!