k6-docs icon indicating copy to clipboard operation
k6-docs copied to clipboard

Document the importance of sleep

Open MattDodsonEnglish opened this issue 2 years ago • 5 comments

For developers who don't come from a testing background, the sleep() function can be confusing. Why waste CPU cycles doing nothing?

We have sleep recommendations in the browser recorder, a mention in the "high CPU" insight, and some mentions in k6-learn, but it'd be nice to have a more findable description, since sleep is in so many of our example scripts, and little "best practices" like this are some of the most requested docs.

MattDodsonEnglish avatar Nov 16 '22 15:11 MattDodsonEnglish

We could just link to https://www.sleepfoundation.org/how-sleep-works/why-do-we-need-sleep :trollface:

But to add something constructive: sleep() is very important to simulate actual user behavior. Users don't just hammer a web site like a bot would, and a lot of time is spent rendering and interacting with the frontend. Running a browser test represents that scenario better, and part of the use case for k6 is to stress the SUT and not behave exactly like a user would. But even in that case, sleep() is useful to pace the generated load, and not overload the load generator machine. Given that k6 is not strictly a load testing tool, but encompasses many different areas of observability, waiting for a specific amount of time is useful in other testing scenarios as well. So I agree that we should have a document to mention some of these examples.

imiric avatar Nov 16 '22 16:11 imiric

I think the most important is to document that sleep() is a bad idea for arrival rate executors. At least, sleep should not be added at the end of the iteration, as we recommend for other executors.

sniku avatar Jan 27 '23 16:01 sniku

On similar note :point_up: we likely should get something like delay for asynchronous code as sleep is blocking and synchronous.

mstoykov avatar Jan 27 '23 16:01 mstoykov

I think the most important is to document that sleep() is a bad idea for arrival rate executors. At least, sleep should not be added at the end of the iteration, as we recommend for other executors.

I just made a small note to account for this, if someone wants to review.

https://github.com/grafana/k6-docs/pull/1008/files

MattDodsonEnglish avatar Jan 27 '23 17:01 MattDodsonEnglish