k6 icon indicating copy to clipboard operation
k6 copied to clipboard

functions queued in eventLoop only run after the main function is done executing in a VU

Open onprem opened this issue 1 year ago • 0 comments

Brief summary

Due to how it is implemented internally, the main function is added as the first item in the eventLoop queue. Due to this, other functions added to the eventLoop during the execution will be only run after the main test function has exited.

https://github.com/grafana/k6/blob/f947acb0bc57957d04d8e2a151c573fdc978e65e/js/runner.go#L858-L861

This makes using long running VUs impractical in many use cases.

k6 version

0.51.0

OS

Arch Linux

Docker version and image (if applicable)

No response

Steps to reproduce the problem

This can be reproduced by running a VU for a long duration where other functions are being added to the eventLoop queue, and observing when they execute.

A quick way to do that would be to use the test script I am working on here https://github.com/onprem/entity-store-loadtest

Expected behaviour

I expected the eventLoop queue to be independent of the test function. Running the eventLoop in parallel would allow modules/users to rely on it when the test function also need to run for a long duration (like for full test duration).

Actual behaviour

The functions added in the evenLoop queue are executed only after the test function has finished running.

I ran into this while trying to load test a gRPC API that exposes a stream of events. The callback functions defined by me in the script execute only after the main function ends (one iteration).

stream.on('data', func(resp) {
  // process the message
  // calculate metrics for latency etc.
})

We need a long running VU/test function to receive all of the events. But the callback functions won't be called untile the end, due to this the latency metrics etc. are not usable. You can see in the screenshot below that the watch_event_latency metric only shows up in the end, with the latency being about same as the test duration itself.

Screenshot_20240523_161100

onprem avatar May 23 '24 11:05 onprem