Understanding K6 Execution Flow

Hi Team,

I wrote a test script for an api, which basically first authenticated with login api.

e.g. have to test performance of api X, which uses the cookie/ session which got generated once we execute the api y (login one)

So I wrote the test like -

export default function () {
   http.get(loginapi(username, password)
   http.get(Xapi)
}

Now I executed the test with 1 virtual user for 10 mins and when I observe the result I found-
Total Iteration - 80
-Successful Login - 70
-Failed login - 10

  • 2nd Api (X) Successful - 80
  • 2nd Api (X) Failed - 0

Just wanted to understand if login is successful only 70 times, how did 2nd api got success-full response 80 time.

I am assuming that on each iteration its creating a new session where its login and executing 2nd request.

Also how can we ensure 2nd api is not using caching here…any option to ensure that through script

Hi @vish_s02

Is the VU code you shared the same you are running?

http.get(loginapi(username, password)

Is this an http.get to a url that is returned after the login? Or are you just invoking a function that does the login? With the incomplete code it’s difficult to tell.

http.get(Xapi)

If this is another http.get, it makes sense that it can be successful for the 80 iterations while the login fails. In the example you don’t stop the iteration if the login in the first step fails, and the second request does not seem to use any of the login information (an authentication token, etc.). So your test would be executing 80 logins (10 fail), and 80 calls to the second endpoint (none failing), if the second request is actually not needing anything from the first one (authentication token or similar).

If you can share a bit more of your code we can help you out. This seems a simplified example that might not be giving is enough information.

You can also have a look at API CRUD Operations, for an example. You’ll see that if an operation fails, the script continues unless we put a return to stop that iteration.

I hope this helps.

Cheers!

Thanks for answering.

Yes it’s exactly the same code which I am running…let me more clarify.

Forget about this is coming from a function http.get(loginapi(username, password). Just consider this is an api which accepts user id and password. Something like

export default function () {
const params = {
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
    };
    const payload = "targetUrl=&username=" +username + "&password=" +password + "&login=";
    const response = http.post(http.url`${Utils.getBaseUrl()}/test/login`, payload, { "headers": params.header, tags: { name: 'Login Request /test/login' } });
    cookies = response.cookies;
    let checkRes = check(response, {
      'Login Request /test/login status is 200': () => response.status === 200,
    });
// then running second request
http.get(Xapi)

}

if we have to run second request (Xapi) only, then we need to get and pass some cookies to authorise this api and pass as headers in Xapi. So here login apis basically returns those cookies.

Now Scenario 1- if we are running both api together in default function, 2nd api (Xapi) working fine without passing cookies in it header. So its looks like its maintaining the session.

Scenario 2 - if I use the login api in setup function and Xpai in default function then, Xapi will fail if we will not provide cookies returned from login api in Xapi header.

Question1 - if I have to test Xapi only, then whats the best way to write the code, we should write login api code in setup function or it can be like I am doing right now i.e. both apis in default function.

Question2 - is there any way to ensure Xapi is not using cached data? is there any provision to pass some parameter which will ensure data is not cached.

Hi @eyeveebe Any thoughts on this?

Hi @vish_s02

Thanks for clarifying with a bit more context.

Question1 - if I have to test Xapi only, then whats the best way to write the code, we should write login api code in setup function or it can be like I am doing right now i.e. both apis in default function.

If you need to only run Xapi and don’t want to include the login request in the load test, or repeat that for each user and request, the example is the one I shared: API CRUD Operations. You will have to check what headers your requests need, or cookies, it will depend on your endpoint behavior. If you do the login operation in the setup, this is only done once and you need to pass the data to the code each VU is executing. That is why if you do that it fails for you, if you don’t pass the data and set the right headers http.get(Xapi, params).

This can also help:

Question2 - is there any way to ensure Xapi is not using cached data? is there any provision to pass some parameter which will ensure data is not cached.

The caching happens at server-side, k6 will not be caching. I understand you will need to send the right headers in the get request, to disable cache if you are seeing the content is cached on the server-side and you really wanted to prevent that: https://reqbin.com/req/doog8aai/http-headers-to-prevent-caching

Cheers!

Thanks @eyeveebe

What about when we are using xk6 browser, how we can ensure response is not returning from cached data.

Hi @vish_s02

When using browser you usually are looking at the same behavior the browser will have, which usually is to follow the cache headers and don’t download in the same session content that should be cached. The BrowserContext provides a way to operate multiple independent sessions, with separate pages, cache, and cookies if needed.

If you are testing websites, have a look at How to Load Test a Website: The k6 Guide. Maintaining cookies is usually more realistic.

Determine cache and cookie behaviour. k6 automatically resets cookies between iterations, but you can also change this behavior if maintaining cookies would be more realistic.

Cheers!