What is the best way to script a hearbeat

Hi,

Although not exactly a heartbeat, our system polls for information once a minute. If I want to create a realistic work load, I would need to simulate these requests once per minute for any logged in user.

The naïve approach would be, for every VU in a scenario that does some other workflow at a given rate, have a VU in a scenario that does nothing but do this polling. But, the downside of that is that I end up with double the number sessions being managed on the server.

Ideally, I could do it with something like window.setTimeout, but I don’t have a window object in k6… do I?

Hi @Martin !

Welcome to the community forum :wave:

Before the answer the main question, let me clarify. Are you planning to perform additional requests apart from heartbeat?

Cheers

Hi @olegbespalov,

I plan to have several scenarios that perform actions and these actions should be dispersed with the heartbeat if they are alive for a long enough period. I also plan to have a few VUs simulate a logged in user that, aside from the heartbeat, are idle.

I ended up implementing something that seems to be working but it is complex. It involved setting an env var for each VU that contains a timestamp for the last time that the heartbeat was issued, then I intersperse calls to a heartbeat function that checks the relevant env var and, if it is time, sends a heartbeat and updates the env var.

It would be good if I could do something a bit more elegant but, as I say, it seems to be working for now.

Cheers

Hi @Martin !

Thanks for the input!

It seems like you have already found a workaround that I was going to suggest to you :smile:

Unfortunately, for now, there is no support for the setTimeout/setInterval in the k6.

We’re working on implementing event loop features. The upcoming v0.40.0 release will introduce experimental modules, and one of them, k6/experimental/timers contains this functionality. Important to say, that it’s still experimental and API can be changed.

Hope that answers,
Cheers!