Promise.all with k6

Hello,
If in my application I run multiple promises together using Promise.all, how can I replicate that with k6?
I didn’t see anything mentioned about that in the docs/github.

Basically our homepage prepares 4 graphql requests as promises, then we use Promise.all to run them in parallel.

How can I replicate this with k6 to get more accurate results?

Hi Omar, welcome to the forum!

For parallel HTTP requests within a single VU, you would want to make use of http.batch.

2 Likes

Thank you Tom. That’s exactly what I needed.

On a different topic, does k6 support sending graphql queries with variables like apollo client?

Meaning when I send the request it has two parts:

1-the string query that has the selection set and the query types like [CustomerWhereUniqueInput].

2- the variables object that we set all data inside like say when I want to query, I add this variables object:

const variables = {
   where: {
       name: {
            equals: "Omar"
       }
   }
};

The only example I found was this https://k6.io/blog/load-testing-graphql-with-k6/

and it hardcodes the variables in the query string.

So my question is, how can I pass variables?

Nevermind, managed to find out how. If the blog article enable users to submit changes from the website(like Prisma docs), then I would happily submit a PR explaining how I solved it so other people won’t fall into the same issue.

Thank you@

I don’t think our blogs allow edits, but the docs site itself does.

I could ask the blog writer to modify their post, but I do note that the basic example linked toward the bottom of the post does contain a parameterized “mutation” query:

Well both the query and mutation are doing basically the same thing; substituting values in the query string, which is nice in custom resolvers that only have flat fields.

Anyway, here’s how I got it to work:

   const getQueryAndVariables = (where, orderBy, numberOfRecords) => {
      const variables = {};
      if (where) {
        variables.where = where;
      }
      if (orderBy) {
        variables.orderBy = orderBy;
      }
      if (numberOfRecords) {
        variables.take = numberOfRecords;
      }
          return {
            variables,
            query: `
             query ($orderBy: [CampaignOrderByInput!], $where: CampaignWhereInput,$take: Int) {
              campaigns( where: $where, orderBy: $orderBy,take: $take) {
                  id
                  description
                }
            }`,
          };
    }


const campaignsQuery = getQueryAndVariables(argsValues);
let res = http.post(ENDPOINT, JSON.stringify({ query: campaignsQuery.query, variables: campaignsQuery.variables }), { headers: headers });`

That’s basically it. But there’s one thing that was weird, the spread operator doesn’t work at the http.post line, although this is a javascript file. It says unexpected token, like this example:

JSON.stringify({ ...campaignsQuery }); //Unexpected token ...