Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
246 views
in Technique[技术] by (71.8m points)

javascript - K6 Request an API sequentially based on VU Id

I am making a load testing program using K6, JavaScript in which I want to request an API sequentially based on VU id.

For example, let suppose I have 5 different payloads and 5 VU one for each payload.

I want my program to run such as, for the 1st min only VU-1 will get executed requesting continuously with my 1st payload, then for next min, VU-2 will execute and so on till 5th and then again 1st VU will get executed.

Based on the VU Id I will be changing the payload.

I have tried setTimeout() method of JavaScript but not working with k6.

Also I have tried the scenario option from k6 documentation but its running parallelly. here is the code snippet I tried.

export let options = {
  scenarios: {
    execution: {
      scenario1: {
          type: "per-vu-iterations",
          vus: 1,
          iterations: 1,
          maxDuration: "15s",
          exec: "scenario1",
      },
      scenario2: {
          type: "per-vu-iterations",
          vus: 1,
          iterations: 1,
          maxDuration: "15s",
          exec: "scenario2",
      },
    }
  }
}


export function scenario1() {
  let res = http.get("https://test.loadimpact.com/");
  console.log(__VU);
  check(res, {
    "is status 200": (r) => r.status === 200
  });
};

export function scenario2() {
  let res = http.get("https://test.loadimpact.com/");
  console.log(__VU);
  check(res, {
    "is status 200": (r) => r.status === 200
  });
};

Is their any method in K6 to achieve this?

question from:https://stackoverflow.com/questions/65952304/k6-request-an-api-sequentially-based-on-vu-id

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

One solution for this problem I found, is using the Scenerios options with executer and startTime parameters. But the issue in this solution I am facing is that I am not able to view the metrics result for the individual scenario.

If there is any way to display the metrics do share.

This is a sample code for sequential running.

export let options = {
  scenarios: {
    scenario_1: {
        executor: "constant-vus",
        vus: 1,
        duration: "1m",
        exec: "scenario1",
        tags: { test_type: 'api-test1' },        
    },
    scenario_2: {
        executor: "constant-vus",
        vus: 1,
        duration: "1m",
        exec: "scenario2",
        startTime: "1m",
        tags: { test_type: 'api-test2' },
    },  
  },
}

export function scenario1() {

  let res = http.get("https://example.com/");
  console.log(JSON.stringify(res));
};

export function scenario2() {

  let res = http.get("https://example.com/");
  console.log(JSON.stringify(res));
  
};

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...