{"id":24142,"date":"2023-09-14T05:00:00","date_gmt":"2023-09-14T03:00:00","guid":{"rendered":"https:\/\/sii.pl\/blog\/?p=24142&#038;category=hard-development"},"modified":"2023-09-28T15:53:45","modified_gmt":"2023-09-28T13:53:45","slug":"performance-under-control-with-k6-recording-parametrization-and-running-the-first-test-scenario","status":"publish","type":"post","link":"https:\/\/sii.pl\/blog\/en\/performance-under-control-with-k6-recording-parametrization-and-running-the-first-test-scenario\/","title":{"rendered":"Performance under control with k6 \u2013 recording, parametrization, and running the first test scenario"},"content":{"rendered":"\n<p>To effectively utilize tools and frameworks, it is necessary to have a full understanding of their fundamental principles and functionalities. In today&#8217;s part of the series &#8220;<a aria-label=\" (opens in a new tab)\" href=\"https:\/\/sii.pl\/blog\/en\/performance-under-control-with-k6-introduction\/?category=testing&amp;tag=k6-en,tosca-en,pros-and-cons,tools\" target=\"_blank\" rel=\"noreferrer noopener\" class=\"ek-link\">Performance under control with k6<\/a>,&#8221; we will present the process of recording test scenarios and ways to parameterize them. We will also discuss how to group scenarios and prepare code to ensure easy long-term maintenance.<\/p>\n\n\n\n<figure class=\"wp-block-embed aligncenter is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"Testy wydajno\u015bciowe z k6: nagrywanie, parametryzacja i uruchamianie pierwszego scenariusza | cz\u0119\u015b\u0107 2\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/juEt_OL2_Cc?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Recording Test Scenarios<\/strong><\/h2>\n\n\n\n<p>The essential element in preparing test scenarios is covering HTTP requests and operations performed by other protocols used in the application. The most common case is creating simple requests to the API interface.<\/p>\n\n\n\n<p>There are two main methods of preparing scenarios:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>the first involves manually creating requests based on documentation and visible requests in the browser,<\/li>\n\n\n\n<li>the second method is recording network traffic using an available proxy.<\/li>\n<\/ul>\n\n\n\n<p>Let&#8217;s discuss the two most popular ways of recording traffic through a proxy:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>using the k6 Browser Recorder extension created by the k6 tool developers,<\/li>\n\n\n\n<li>utilizing a HAR (HTTP Archive) file from the browser.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>K6 Browser Recorder<\/strong><\/h2>\n\n\n\n<p>K6 Browser Recorder is an extension for Firefox and Chrome browsers that allows generating a k6 script based on the traffic generated in the browser. It is the equivalent of the Blazemeter extension for JMeter. The only difference is that the data is saved in the k6 cloud, instead of locally, as in the case of Blazemeter.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><a href=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-01.png\"><img decoding=\"async\" width=\"860\" height=\"470\" src=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-01.png\" alt=\"K6 Browser Recorder\" class=\"wp-image-24105\" srcset=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-01.png 860w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-01-300x164.png 300w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-01-768x420.png 768w\" sizes=\"(max-width: 860px) 100vw, 860px\" \/><\/a><figcaption class=\"wp-element-caption\">Fig. 1 K6 Browser Recorder<\/figcaption><\/figure>\n\n\n\n<p>After clicking the &#8220;Start recording&#8221; button and executing the test path, you should stop the recording, which will take you to the panel with the saved scenario.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-02.png\"><img decoding=\"async\" width=\"1024\" height=\"543\" src=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-02-1024x543.png\" alt=\"K6 Browser Recorder \u2013 save your recorded test\" class=\"wp-image-24107\" srcset=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-02-1024x543.png 1024w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-02-300x159.png 300w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-02-768x407.png 768w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-02-1536x814.png 1536w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-02-2048x1086.png 2048w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Fig. 2 K6 Browser Recorder \u2013 save your recorded test<\/figcaption><\/figure>\n\n\n\n<p>From the scenario panel, we will have the ability to perform various actions, such as excluding requests to third-party domains, adding automatic delays (sleeps), and including static files.<\/p>\n\n\n\n<p>In our case, we are only interested in requests related to the main domain, without considering static files. After proceeding with the generation process, a test scenario will be generated based on the specified configuration.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><a href=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-025.png\"><img decoding=\"async\" src=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-025-1024x539.png\" alt=\"Test scenario based on the specified configuration\" class=\"wp-image-24109\" width=\"840\" height=\"442\" srcset=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-025-1024x539.png 1024w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-025-300x158.png 300w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-025-768x404.png 768w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-025-1536x809.png 1536w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-025-2048x1079.png 2048w\" sizes=\"(max-width: 840px) 100vw, 840px\" \/><\/a><figcaption class=\"wp-element-caption\">Fig. 3 Test scenario based on the specified configuration<\/figcaption><\/figure>\n\n\n\n<p>In its current form, many elements, such as headers, are repetitive. Furthermore, there is a lack of assertions and appropriate naming. However, before proceeding with editing, we will discuss a second method of recording test scenarios.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>HAR File Conversion<\/strong><\/h2>\n\n\n\n<p>The Grafana developers have provided the ability to generate test scenarios from sources other than the Firefox and Chrome browsers. All browsers generate network traffic during user interactions on a web page. This traffic can be saved in the HAR (HTTP Archive) format.<\/p>\n\n\n\n<p>The HAR file contains:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>descriptions of HTTP requests,<\/li>\n\n\n\n<li>responses,<\/li>\n\n\n\n<li>resource files,<\/li>\n\n\n\n<li>loading times,<\/li>\n\n\n\n<li>errors, and other metadata related to interactions.<\/li>\n<\/ul>\n\n\n\n<p>HAR files can be generated using various diagnostic and developer tools, such as built-in developer tools in web browsers, proxy servers, or performance testing tools for websites. For example, in the Firefox browser, we can download a HAR file with requests from the &#8220;Network&#8221; tab in the developer tools.<\/p>\n\n\n\n<p>A sample part of a HAR file has the following format:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-03.png\"><img decoding=\"async\" width=\"1024\" height=\"742\" src=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-03-1024x742.png\" alt=\"Fragment pliku HAR\" class=\"wp-image-24111\" srcset=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-03-1024x742.png 1024w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-03-300x217.png 300w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-03-768x557.png 768w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-03-1536x1113.png 1536w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-03.png 2048w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Fig. 4 A part of a HAR file<\/figcaption><\/figure>\n\n\n\n<p>The developers at Grafana Labs have created a tool called &#8220;har-to-k6,&#8221; which allows the conversion of HAR files into k6 test scenarios. To install this tool, you need to use the Node.js package manager.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><a href=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-04.png\"><img decoding=\"async\" width=\"826\" height=\"428\" src=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-04.png\" alt=\"Instalation of har-to-k6 tool\" class=\"wp-image-24113\" srcset=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-04.png 826w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-04-300x155.png 300w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-04-768x398.png 768w\" sizes=\"(max-width: 826px) 100vw, 826px\" \/><\/a><figcaption class=\"wp-element-caption\">Fig. 5 Instalation of har-to-k6 tool<\/figcaption><\/figure>\n\n\n\n<p>After successfully installing the &#8220;har-to-k6&#8221; tool, we can use the converter to process the downloaded HAR file.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><a href=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-05.png\"><img decoding=\"async\" width=\"928\" height=\"422\" src=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-05.png\" alt=\"Use of a converter\" class=\"wp-image-24115\" srcset=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-05.png 928w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-05-300x136.png 300w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-05-768x349.png 768w\" sizes=\"(max-width: 928px) 100vw, 928px\" \/><\/a><figcaption class=\"wp-element-caption\">Fig. 6 Use of a converter<\/figcaption><\/figure>\n\n\n\n<p>After using the &#8220;har-to-k6&#8221; tool, a k6 test scenario named scenario.js will be generated. Here is the sample content of the generated script:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-06.png\"><img decoding=\"async\" width=\"931\" height=\"1024\" src=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-06-931x1024.png\" alt=\"Content of the genereted script\" class=\"wp-image-24117\" srcset=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-06-931x1024.png 931w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-06-273x300.png 273w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-06-768x845.png 768w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-06-1397x1536.png 1397w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-06-1862x2048.png 1862w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-06.png 2048w\" sizes=\"(max-width: 931px) 100vw, 931px\" \/><\/a><figcaption class=\"wp-element-caption\">Fig. 7 Content of the generated script<\/figcaption><\/figure>\n\n\n\n<p>In comparison to using the k6 Browser Recorder, using the &#8220;har-to-k6&#8221; converter is less flexible. The HAR file structure does not have comprehensive documentation, which sometimes leads to issues with unhandled fields in the tools. Evidence of this can be seen in <a href=\"https:\/\/github.com\/mitmproxy\/mitmproxy\/pull\/5814\" target=\"_blank\" aria-label=\" (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"ek-link\" rel=\"nofollow\" >a recently detected problem in the well-known tool &#8220;mitmproxy<\/a>&#8220;.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Reviewing the Generated Scenario<\/strong><\/h2>\n\n\n\n<p>After generating the test scenario, it is necessary to edit and customize it. Let&#8217;s go back to the example scenario created by the k6 Browser extension to discuss it further.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-07.png\"><img decoding=\"async\" width=\"594\" height=\"1024\" src=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-07-594x1024.png\" alt=\"Test scenario\" class=\"wp-image-24120\" srcset=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-07-594x1024.png 594w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-07-174x300.png 174w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-07-768x1324.png 768w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-07-891x1536.png 891w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-07-1188x2048.png 1188w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-07.png 2048w\" sizes=\"(max-width: 594px) 100vw, 594px\" \/><\/a><figcaption class=\"wp-element-caption\">Fig. 8 Test scenario<\/figcaption><\/figure>\n\n\n\n<p>In the above case, the scenario consists of two main elements. The first one is the options object.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><a href=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-08.png\"><img decoding=\"async\" width=\"738\" height=\"550\" src=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-08.png\" alt=\"Options object\" class=\"wp-image-24122\" srcset=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-08.png 738w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-08-300x224.png 300w\" sizes=\"(max-width: 738px) 100vw, 738px\" \/><\/a><figcaption class=\"wp-element-caption\">Fig. 9 Options object<\/figcaption><\/figure>\n\n\n\n<p>By defining the options object, we can specify various parameters such as:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>the number of concurrently running virtual users (vus),<\/li>\n\n\n\n<li>the test duration (duration),<\/li>\n\n\n\n<li>quality thresholds (thresholds).<\/li>\n<\/ul>\n\n\n\n<p>We will discuss this object and its capabilities in detail in the upcoming parts of the series. Currently, there are over 50 different options that can be defined.<\/p>\n\n\n\n<p>The second element of the generated scenario is the main() function. It is where we define HTTP requests and data operations.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Parameterization<\/strong><\/h2>\n\n\n\n<p>Currently, one of the most common mistakes is the inability to reuse HTTP headers and URLs multiple times. To address this issue, we can modify the scenario slightly to avoid redefining them for each HTTP request.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-09.png\"><img decoding=\"async\" width=\"1024\" height=\"850\" src=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-09-1024x850.png\" alt=\"Scenario after modification\" class=\"wp-image-24125\" srcset=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-09-1024x850.png 1024w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-09-300x249.png 300w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-09-768x638.png 768w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-09-1536x1275.png 1536w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-09.png 2048w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Fig. 10 Scenario after modification<\/figcaption><\/figure>\n\n\n\n<p>Another important element of the test is the interaction time between different parts of the page. In the case of the generated scenario, the user&#8217;s rest time is 9.1s and 9.6s. We assume that the response time can vary from 3 to 10 seconds.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><a href=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-10.png\"><img decoding=\"async\" width=\"826\" height=\"422\" src=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-10.png\" alt=\"response time\" class=\"wp-image-24127\" srcset=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-10.png 826w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-10-300x153.png 300w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-10-768x392.png 768w\" sizes=\"(max-width: 826px) 100vw, 826px\" \/><\/a><figcaption class=\"wp-element-caption\">Fig. 11 Response time<\/figcaption><\/figure>\n\n\n\n<p>At this stage, let&#8217;s also introduce some randomness when opening specific articles. For this test, we assume that there are only five fixed articles. To introduce randomness, we can use the <a href=\"https:\/\/k6.io\/docs\/javascript-api\/jslib\/utils\/\" target=\"_blank\" aria-label=\" (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"ek-link\" rel=\"nofollow\" >k6-utils<\/a> library.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-11.png\"><img decoding=\"async\" width=\"649\" height=\"1024\" src=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-11-649x1024.png\" alt=\"Introducing randomnes\" class=\"wp-image-24129\" srcset=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-11-649x1024.png 649w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-11-190x300.png 190w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-11-768x1212.png 768w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-11-973x1536.png 973w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-11-1298x2048.png 1298w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-11.png 2048w\" sizes=\"(max-width: 649px) 100vw, 649px\" \/><\/a><figcaption class=\"wp-element-caption\">Fig. 12 Introducing randomnes<\/figcaption><\/figure>\n\n\n\n<p>To extract data from the application, such as the article name or source address, we used built-in functions for manipulating HTML files. These functions may resemble those known from functional testing frameworks like Cypress or Selenium.<\/p>\n\n\n\n<p>Let&#8217;s pay attention to the placement of variables articleName and articleHref \u00ad\u2013 <strong>why are they defined before the group?<\/strong> Let&#8217;s discuss this a bit more broadly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Grouping Requests<\/strong><\/h2>\n\n\n\n<p>To maintain consistency in test steps, it&#8217;s beneficial to use grouping. This can be likened to transaction controllers in JMeter. In k6, all variables defined within a group are only accessible within that group. For example, if we define articleName inside the second group, its value won&#8217;t be accessible in the third group.<\/p>\n\n\n\n<p>Grouping is also useful when we want to collect several requests made at the same stage of the test. For instance, when we visit the homepage, we often make multiple parallel HTTP requests. In this case, all these requests can be in one group named &#8220;Homepage Visit.&#8221; Let&#8217;s introduce a change in the naming in our test.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-12.png\"><img decoding=\"async\" width=\"1024\" height=\"897\" src=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-12-1024x897.png\" alt=\" a change in the naming in our test.\" class=\"wp-image-24132\" srcset=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-12-1024x897.png 1024w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-12-300x263.png 300w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-12-768x673.png 768w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-12.png 1066w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Fig. 13 A change in the naming in our test<\/figcaption><\/figure>\n\n\n\n<p>In this way, we will easily maintain our tests even if we add or remove relevant requests. There will be no issue with their identification, and as we will see in the upcoming parts, it will facilitate analysis in third-party tools.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Assertions and Checks<\/strong><\/h2>\n\n\n\n<p>Let&#8217;s notice the absence of assertions in our test scenario. In programming, an assertion is a mechanism used to verify conditions or assumptions during program execution. It is a way of verifying whether certain expectations are met, which helps in error detection. In the case of an incorrect assertion during the test, its execution is interrupted.<\/p>\n\n\n\n<p>In the implementation of k6, assertions have been replaced with checks. Checks are similar to assertions, with the difference that the test is not automatically interrupted after receiving an erroneous response.<\/p>\n\n\n\n<p>Checks take the <a href=\"https:\/\/k6.io\/docs\/javascript-api\/k6-http\/response\/\" target=\"_blank\" aria-label=\" (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"ek-link\" rel=\"nofollow\" >response<\/a> object as their first argument. This object contains several fields on which assertions can be made.<\/p>\n\n\n\n<p>The most commonly used fields in checks are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>the server status (status),<\/li>\n\n\n\n<li>response body (body),<\/li>\n\n\n\n<li>and response headers (headers).<\/li>\n<\/ul>\n\n\n\n<p>Let&#8217;s define a sample check that verifies whether the response status is correct and the response body contains the expected text.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-13.png\"><img decoding=\"async\" width=\"1024\" height=\"296\" src=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-13-1024x296.png\" alt=\"Defining a sample check\" class=\"wp-image-24135\" srcset=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-13-1024x296.png 1024w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-13-300x87.png 300w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-13-768x222.png 768w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-13-1536x444.png 1536w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-13.png 2048w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Fig. 14 Defining a sample check<\/figcaption><\/figure>\n\n\n\n<p>Using the above mechanism, let&#8217;s edit our script.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-14.png\"><img decoding=\"async\" width=\"573\" height=\"1024\" src=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-14-573x1024.png\" alt=\"Script edition\" class=\"wp-image-24137\" srcset=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-14-573x1024.png 573w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-14-168x300.png 168w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-14-768x1372.png 768w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-14-860x1536.png 860w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-14-1147x2048.png 1147w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/p2-attachment-14.png 2048w\" sizes=\"(max-width: 573px) 100vw, 573px\" \/><\/a><figcaption class=\"wp-element-caption\">Fig. 15 Script edition<\/figcaption><\/figure>\n\n\n\n<p>If we decide to use traditional assertions, we will have to use our own custom-defined function. Unlike the built-in check function, which returns true or false, we can utilize this information to stop the current test iteration (goroutine) and start a new one.<\/p>\n\n\n\n<p>For this purpose, we can use the built-in fail function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Summary<\/strong><\/h2>\n\n\n\n<p>In today&#8217;s part of our series, we familiarized ourselves with the basic functionalities of the k6 tool. We learned how to record test cases, parameterize them, and create a basic test scenario. In the upcoming parts, we will focus on setting quality thresholds, discuss tagging, and explore various types of metrics.<\/p>\n\n\n\n<p>If you haven&#8217;t had a chance to read <a href=\"https:\/\/sii.pl\/blog\/en\/performance-under-control-with-k6-introduction\/?category=testing&amp;tag=k6-en,tosca-en,pros-and-cons,tools\" target=\"_blank\" aria-label=\"the previous introductory part (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"ek-link\">the previous introductory part<\/a>, we encourage you to do so. There, you will find more information about our introduction and the motivation behind choosing the k6 tool.<\/p>\n\n\n\n<p><a href=\"https:\/\/sii.pl\/blog\/en\/search\/k6\/\" target=\"_blank\" aria-label=\" (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"ek-link\">In the following parts of the series<\/a>, we will continue to develop our test scenario to tailor it to more advanced needs and delve into the functionalities of k6. We encourage you to follow our series to learn more about this powerful performance-testing platform.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Useful link<\/strong>s<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/k6.io\/docs\/test-authoring\/create-tests-from-recordings\/using-the-browser-recorder\/\" target=\"_blank\" aria-label=\" (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"ek-link\" rel=\"nofollow\" >K6 \u2013 using the browser recorder<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/github.com\/grafana\/har-to-k6\" target=\"_blank\" aria-label=\" (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"ek-link\" rel=\"nofollow\" >Grafana, har-to-k6<\/a><\/li>\n\n\n\n<li><a aria-label=\" (opens in a new tab)\" href=\"https:\/\/k6.io\/docs\/javascript-api\/k6-http\/response\/\" target=\"_blank\" rel=\"noreferrer noopener\" class=\"ek-link\" rel=\"nofollow\" >K6 \u2013 response<\/a><\/li>\n<\/ul>\n\n\n<div class=\"kk-star-ratings kksr-auto kksr-align-left kksr-valign-bottom\"\n    data-payload='{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;24142&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;bottom&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;2&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;starsonly&quot;:&quot;&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;11&quot;,&quot;greet&quot;:&quot;&quot;,&quot;legend&quot;:&quot;5\\\/5 ( votes: 2)&quot;,&quot;size&quot;:&quot;18&quot;,&quot;title&quot;:&quot;Performance under control with k6 \u2013 recording, parametrization, and running the first test scenario&quot;,&quot;width&quot;:&quot;139.5&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} ( {votes}: {count})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>\n            \n<div class=\"kksr-stars\">\n    \n<div class=\"kksr-stars-inactive\">\n            <div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 11px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 18px; height: 18px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 11px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 18px; height: 18px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 11px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 18px; height: 18px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 11px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 18px; height: 18px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 11px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 18px; height: 18px;\"><\/div>\n        <\/div>\n    <\/div>\n    \n<div class=\"kksr-stars-active\" style=\"width: 139.5px;\">\n            <div class=\"kksr-star\" style=\"padding-right: 11px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 18px; height: 18px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 11px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 18px; height: 18px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 11px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 18px; height: 18px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 11px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 18px; height: 18px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 11px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 18px; height: 18px;\"><\/div>\n        <\/div>\n    <\/div>\n<\/div>\n                \n\n<div class=\"kksr-legend\" style=\"font-size: 14.4px;\">\n            5\/5 ( votes: 2)    <\/div>\n    <\/div>\n","protected":false},"excerpt":{"rendered":"<p>To effectively utilize tools and frameworks, it is necessary to have a full understanding of their fundamental principles and functionalities. &hellip; <a class=\"continued-btn\" href=\"https:\/\/sii.pl\/blog\/en\/performance-under-control-with-k6-recording-parametrization-and-running-the-first-test-scenario\/\">Continued<\/a><\/p>\n","protected":false},"author":517,"featured_media":24141,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_editorskit_title_hidden":false,"_editorskit_reading_time":0,"_editorskit_is_block_options_detached":false,"_editorskit_block_options_position":"{}","inline_featured_image":false,"footnotes":""},"categories":[1321],"tags":[1792,1790,1791,1590],"class_list":["post-24142","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-testing","tag-test-scenario","tag-k6-en","tag-tosca-en","tag-tools"],"acf":[],"aioseo_notices":[],"republish_history":[],"featured_media_url":"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/Wydajnosc-pod-kontrola-\u2013-co-sklania-mnie-do-wyboru-k6.jpg","category_names":["Testing"],"_links":{"self":[{"href":"https:\/\/sii.pl\/blog\/en\/wp-json\/wp\/v2\/posts\/24142"}],"collection":[{"href":"https:\/\/sii.pl\/blog\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sii.pl\/blog\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sii.pl\/blog\/en\/wp-json\/wp\/v2\/users\/517"}],"replies":[{"embeddable":true,"href":"https:\/\/sii.pl\/blog\/en\/wp-json\/wp\/v2\/comments?post=24142"}],"version-history":[{"count":3,"href":"https:\/\/sii.pl\/blog\/en\/wp-json\/wp\/v2\/posts\/24142\/revisions"}],"predecessor-version":[{"id":24537,"href":"https:\/\/sii.pl\/blog\/en\/wp-json\/wp\/v2\/posts\/24142\/revisions\/24537"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sii.pl\/blog\/en\/wp-json\/wp\/v2\/media\/24141"}],"wp:attachment":[{"href":"https:\/\/sii.pl\/blog\/en\/wp-json\/wp\/v2\/media?parent=24142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sii.pl\/blog\/en\/wp-json\/wp\/v2\/categories?post=24142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sii.pl\/blog\/en\/wp-json\/wp\/v2\/tags?post=24142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}