%% Last Updated: - [[2021-02-11]] Related: - [[k6 (Company)]] %% Author: [[Leandro Melendez]] URL: %%This is a video that k6 paid Leandro for, the second part of a two-part series. The first part was [[Basics of Thresholds and SLOs]]%% ## Transcript Hello, and welcome to this second episode of this K6 series of videos. Here we are continuing the topic of SLOs and thresholds. Today we will learn how to implement them in the K6 tool. In the previous episode we reviewed SLOs, as well as SLAs, SLIs and how do they relate to one of the functionalities of the K6 tool, Thresholds. So today we will see how to establish SLO thresholds in our K6 automations. But before we begin, a quick note. In the last episode we provided a wide variety of examples of where you can apply all the SLs and thresholds. Such as CPU % and disk space among others. But the the K6 tool, focuses mostly on thresholds over default http metrics or on custom metrics that you can create. For a list of available metrics and ways to create custom metrics, please go to [k6.io/docs](http://k6.io/docs) and refer to the metrics section. So for the rest of this video, we will not get too deep into details about metrics, but mostly on the thresholds applied to them. All right? With that cleared out, lets move on to the thresholds. For this example we will use the Visual Studio Code editor. But to work with K6 scripts, you can use any text editor of your preference. So. Lets get on to this semi ready script that I prepared for this video. As you can see, it just does 5 requests to the k6 test page. I named it ThresholdsExample.js. Now lets just run that script to see the basic results that we receive just from running it. To run the script we will use command: k6 run ThresholdsExample.js After running it you will see the default metrics that K6 reports. As you can see we have metrics such as data sent and received. A bunch of http request metrics such as the number of requests, duration, bytes sent and received and many other basic metrics. Many of those metrics, mainly the http requests, have more than just a single metric, such as maximum, minimum, med, percentages and average. The different outputs on a metric depend on its type. K6 has 4 types of metrics, which are: Counter, Gauge, Rate and Trend. For more information on each please check the k6 Metrics documentation. So, Lets pick a metric to create a threshold on it. How about we choose the http request duration, which is a Trend. I like that one a lot!! and many times that metric is one of our biggest concerns. So, lets work on the average for the http request duration. As you can see from the panel, it took this amount of milliseconds, so lets create on that metric a threshold that we know it will easily pass. For this first we will append another command export let options section, as we will be adding up some more options. Now lets add there a thresholds section. Everything after that will be within the curly brackets defining anything about thresholds, lets make space between them to add stuff. As I mentioned, I want to focus in this example on the http request duration, so lets indicate that by typing first http\_req\_duration followed by a colon. Now we will finally add our first threshold on it. Lets add some regular brackets now, as inside them we will be able to define each threshold. For now lets add only one, shall we? Thresholds can be applied on the variables that each metric type has. Since this one is a Trend type it has many variables on which we can apply the threshold, so lets do it for now over the average. We will write it as it was displayed in the results, a v g. On it we will apply our first threshold in milliseconds. So I want something easy to pass, so lets put that threshold for the avg to be smaller than ten thousand, in other words 10 seconds. Easy peasy. Definitely it will pass that. Lets run it again. As you can see, now we have a green check mark left to the request duration line. Congratulations! That means the test passed our first threshold. Now. Lets change it for a value that it will definitely not pass. Lets say that it must be smaller than 1 millisecond. pretty tough! Lets run it! As expected, see, now instead of the green check mark, we have a red ex. And not only that, at the bottom we have a message with a return code different that 0, indicating that some thresholds failed. Lets go and add another one. To add more thresholds, first if they are under the same metric, we do it inside those regular brackets I mentioned earlier. Here you can add more for that same metric! So lets pick one more from the available variables for that metric. How about the maximum duration. Generally that is another one to keep under check. So we add a comma and then specify the variable to which we will apply the new threshold, m a x, to make an easy pass it must be smaller than lets say ten seconds, or in milis, 10 thousand. Lets also return our average to something realistic, lets say 2 seconds. Now we save it and run it. All right another green mark, yay! But beware, both must pass. If only one threshold is not passed, we will get another red mark. Lets do that quickly. Again we change the average to 1 millisec, of course non realistic. Lets run it. So only the max will pass and the average will fail, thus failing the whole thing. Lets quickly make it passable again so we can add one more in that same metric. How about the 90 percent to be also under 3 seconds. Lets run it. Again all cool passing with still one green check. OK, now we have created our first set of thresholds for a single metric. Multiple thresholds on a metric as we mentioned in the examples of the previous video. Lets move on and add another threshold but now on another metric. Lets do it over the iteration duration. For that we just add a new line inside of the thresholds section, write iteration duration and again inside brackets lets just add that threshold over the med to be more than 1 millisecond easy to pass. Save and run. Easy peasy. Now we have another check mark in our run's output! Two base metrics with their own thresholds. Moving on, thresholds do not work only on the basic metrics. You can create custom metrics and apply a threshold to it. So lets see how to do it. First I am going to bring a script from the documentation that already has a custom metric defined. Again if you want to learn more about custom metrics, it is right there in the documentation. The script already has a custom metric named "failed requests". It is a Rate metric. To create a threshold on it we follow a very similar pattern to the standard metrics, only with the difference that we must place it's name in between single quotes. Then the rest is the same, inside regular brackets we create thresholds on the variables of the type of the custom metric. Since the custom metric "failed requests" is a rate type, we can only evaluate over the rate. So we type rate as the name of the variable and the threshold to be smaller than point 1. All right, save and run. As you can see, we have an extra metric displayed there, together with its green check mark. With that we have created another threshold now for a Custom Metric. AS you can see, K6 has a very straight forward process to implement the thresholds in the standard metrics and custom metrics. Now lets go over another two elements that you can apply thresholds to. They are groups and tags. We can apply thresholds to groups and to tags in a very similar way to the metrics. Lets check them quickly. I have here an example script that already has tags and groups created. If you want to learn more about them, again, please go to the Tags and Groups section in the documentation page. So lets open that script. In this script there is a group and a tag already created. The group is named groupDuration. It has inside some metrics such as individual requests and batch requests. So lets write up the thresholds. As you can see we declare them in between single quotes, very similar to the custom metrics. The only difference here is that instead of the custom metric name, we will add curly brackets at the end, and add the instruction groupname and then after a colon the names for each group. In this case, Individual, and batch requests. Both are also of the trend type, so lets create the threshold on the average, we want them to be smaller than 2000 both. Now before we run this, lets add as well a threshold for a tag. We have another tag in the http request duration named API. So again in the same way of the single quotes, we write the name of the metric, http request duration, and again within the curly brackets, now we will write TYPE as the tag and specify it to be API. For this threshold we will set it on the 95 percentile, same inside of the brackets we add the 95 percent and put the threshold to be smaller than 5000. Now lets save it and check it. As you can see, the script completed, and had passes indicating them with th green checks in their respective metrics. And speaking of completed, before we finish, thresholds can be configured to not to allow the script to finish if any threshold does not pass. The last components of a threshold are a couple of identifiers we can add at the end. By default k6 keeps running if thresholds do not pass. So the first is an indicator to stop everything if the threshold fails. The second is an indicator of how long to wait before stopping, after the threshold is not passed. s. Lets quickly view them in the first example. Here a quick difference is that inside of the regular brackets of the metric we will separate and type the threshold inside of curly brackets to define more elements. Here we add the other elements. abort on fail. And delay abort eval. Giving you even more control to your scripts! And well amigos, with that you now have learned some examples on the types of thresholds and how to implement them in K6. I hope you enjoyed this. If you have any questions don't doubt to submit them over the channels that the K6 team has for help and support. My name is Leandro AKA Señor Performo. see you in the next adventure! Adios amigos! ## See also - [[Basics of Thresholds and SLOs]] - [[Performance Thresholds]] - [[Set multiple thresholds]] - [[Requirements]] - [[What is a test requirement]] - [[Writing a good test requirement]]