%% date:: [[2023-03-17]] %% # [[Organizing code in k6]] [[k6 (tool)|k6]] is a code-only load testing tool, so it can get unwieldy to organize in one long script. Here are some best practices for structuring code in k6 so that you don't get lost in code. <iframe width="560" height="315" src="https://www.youtube.com/embed/zDtEzp_JUOE" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> ## Things that affect how response time is reported ### Groups [Groups](https://k6.io/docs/using-k6/tags-and-groups#groups) are a way to tell k6 to report the response time of several requests together. They are good to use for requests that were triggered by the same single action and are executed simultaneously. Example: all resources on a homepage ### Tags [Tags](https://k6.io/docs/using-k6/http-requests#http-request-tags) can be set on a per-request level, and many requests can share the same tag. This is best used for requests that are not simultaneously executed but are similar in some way. For example, you might use `item_page` as a tag for different products, even though they are separate pages and have different resources. ### URL grouping [URL grouping](https://k6.io/docs/using-k6/http-requests#url-grouping) by using `http.url` is like tagging, but for dynamic requests, so that they're tracked as one. ## Things that don't affect response time Solutions in this section are primarily implemented for pure organizational purposes rather than to change which requests are included in the response time. ### Comments Use a comment in the script when you want to say something about several requests that don't necessarily have anything to do with the response time of those requests. Groups and tags affect response time. ## Functions Use functions, either within the script or as a local JavaScript module that is imported into the script, to collect multiple actions at a higher level. Example: `Items()`could be a function that involves several groups: - View product A page - View product B page - Click Add to Cart on Product B ## [[k6-chai-js]]