Basics of load testing with k6 and Grafana in 20 minutes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone thank you for coming i am nicole van der hoeven i'm a developer advocate at k6 and a few months ago we joined grafana labs this is testing with kasich and grafana better together it's a love story so in mid-june just three months ago or four now actually kasich's joined the grafana labs family many people didn't quite understand what the connection was between k-6 and grafana but i thought it made a lot of sense if you've read the the site reliability engineering book by google they talk about four golden signals that we should measure we should measure in a system latency traffic errors and saturation but load testing provides only three of those that's latency traffic and errors but it doesn't show saturation and it also doesn't give us visibility into the system grafina brings observability and k6 brings data so i think they work really well together which is why we've been using each other's products for a few years now even before there's any talk of an acquisition so we don't have a lot of time so i'm just going to show you how to get started with k6 and then and i'll jump in with a demo i guess the very first thing that i should mention here is if you don't have it installed already if you're on mac os just do a brew install k6 or we have depending on your operating system we will have other instructions at our docs page that's casex dot io slash docs so i'm on mac so i've already done that let me bring up my ide and this is the most basic k6 script i've got an import statement here and a default function that just com is comprised of one get request but that's a little that's a i think that's a little too simple for our use case because i actually want to simulate load simulate a real user that is querying a database for a particular pokemon so i would use a script more like this so now it's the same function here with the same request for pikachu but for one thing i have these checks here that checks for the response code that is returned by this request i'm checking to make sure it's 200 and i'm also checking the response body for the text pikachu in the response this is a think time that i'm adding just to space out the requests a bit and make it seem like a real user that's waiting for requests and then i'm running this over five minutes with five users so let me just show you what that looks like in my terminal here so once k6 is installed all you need to do is run k6 run simple.js that's the name of this script that i've got here and this is now running locally as you can see here and we'll also see how many virtual users are up we see that all five are now up and then it begins to to count how many interruptions are going now this would normally run for five minutes but we don't i don't want to wait for five minutes so i'm just going to stop that here and this is what we get as output now this is just if we're running locally we do get this wall of text but probably what most peop most of the time you're going to be looking for is this http rec duration which is the response time so you'd look at the average response time and if you're only interested in looking at the response time for for requests that passed then i would you'd look at this one so that where the expected response was returned so that's how to run it locally but the cool thing about k6 is although k6 oss is the free and open source software and you can actually run up to 40 000 users just on on a single machine depending on the scenario that you're testing a cool way to extend it and ramp it up and be able to select different geographical regions is by using k6 cloud so let me show you how to do that now normally you would have to do a k6 login cloud which will ask you for your credentials and your api token but i already have that going so once that's set up then there are two ways to do it one is you can just run it locally and have your results output to the cloud so that would still be k6 run and then the test name but then you're adding this output flag and you say cloud and when i start that running you'll see that the location is still local but now there's a link for the output to view the output in the cloud so let's just click on that and now we're being taken to my my kasich's account we can see the simple.js here so let's click on that this is the test that's running still locally as you can see here but now we get a lot more feedback rather than just the output from the console so sometimes this can be useful if you very quickly want to be able to see real-time metrics without having to set anything up so still though we're running locally and let's let me show you how to just run it on the cloud to do that we do k6 cloud now and still the same file this time instead of running on this machine it's actually k6 is transferring those scripts over to the cloud and running it on our aws servers so this is why it's taking a little bit longer to start up because it is provisioning those machines from scratch making sure that the right version of k6 is on there and also transferring over the script and the csv file that i'm using that's being transferred over to the cloud looks like it has started now well it's 15 seconds into it so let's have a look at that go back to this project and that is the the new one now that is currently running should say yes so now it says that this is running from ashburn so as you can see it's pretty easy to switch back and forth between the two different modes of k6 so it's k6 oss if you just want to do some troubleshooting maybe or have a simpler use case and it's k-6 cloud if you want to either ramp up to tens of thousands over several geographical regions or you just want to see the cool dashboard and now i want to show you what this has to do with grafana so there are two ways to integrate k6 with grafana the first is the casex data source plugin it sets up casex cloud in particular as a data source in grafana and it works with any instance of grafana but it doesn't work with k6 oss the second way to integrate is prometheus remote right k6 doesn't yet expose load testing results in prometheus format although we're working on that but this way lets you utilize the remote right feature of prometheus to effectively push the results to prometheus anyway that works for k-6 oss and k6 cloud and with any instance of prometheus so let me show you how both of these two integrations work first is the k6 cloud data source plugin let me go here okay so what you need to do is go to this to the k6 cloud data source plug-in on the grafana marketplace and then click install plugin so mine is already installed but you'll be able to choose which one you'll have to be signed in as well and then you can install it on there now when you when you already have it installed you'll just have to go to configuration and then data sources which is the screen that i'm already in and then do search for k6 it should show up there and then it'll ask you for an api token so what that means is you have to go into your k6 cloud account now i won't do it so that i don't expose my api token to everybody but you just go to your account settings and click api token you copy the string that's there and paste it here and then you can then save and test but before you exit out of this i would also suggest that you go to dashboards and then import these two dashboards because that way you just have it up and running really quickly so let me show you what that looks like so this cloud this is a the list of all of your all of your test results so you can also select the project i think we were running on default before and yeah it was simple.js it oh it's actually still running is that right yeah it's got a little less than two minutes to go so where were we there oops this one so then um we see the list of all the things that i've run for this project and this script before and let's go into the one that's currently running we click on that this is the other dashboard that we saw click zoom to data and we don't have any threshold data yet because the test is still running but we already have the response times looks like there was a spike still just over a second we have the checks over here that we can see and the transactions so both the 200 and the 301 that's being returned so this is a really good way of just being able to get up to speed really quickly with what's happening on k6 and seeing that in grafana so i like it because there's not really much you don't have to create your own dashboards i do want to show you what the thresholds look like too though so let me do that this is another test that i ran previously that shows throughput and response time and now you see what it looks like when those thresholds have been populated so that's after the test you see that all four of them pass so this is just a nice dashboard to have if you're already monitoring your your other metrics on other dashboards and griffin it's kind of nice to have you know this well this interface be replicated over here in grafana okay so to head back the next thing that we're going to look at is the prometheus remote right extension that's also on k6 cloud now there are two ways right now to use the prometheus remote right extension one and they both require k6 cloud for now so the first way is through the ui and that's the one that i'm going to show you but there is also a way that you can just put a snippet a code snippet into your script that identifies basically your prometheus url and your api your key so heading back to case x cloud on k6 cloud you just go to the cloud apm screen and i already have grafana cloud setup but you create a new configuration you'll see something like this and you can use any other provider but obviously the one that that you're probably interested in if you're in this meetup is grafana cloud so then you have the option to select which metrics are going to be available for this integration you the reason so we have a bunch of them already here but the reason that we allow you to put in more is to be able to fine-tune what exactly you're looking at and because k-6 also allows you to create your own custom metrics you can create one and then make sure in the configuration that you're also checking for it and you can decide whether you need to have the run id set for every metric as well here's where you would put your remote right url let me show you where to get that if you go to my account on grafana cloud then you'll go to prometheus here and click on details and then you'll see this information you'll need the remote right endpoint is what you're going to copy over to that k6 cloud page you'll need your username and you're going to have to generate a an api key you put all of that information here and you click save so that's going to save your configurations just to anybody that's on your your organization but you're still going to need to select when you want to use that and enable that on a test by test basis because you may not want if you're just doing like a shakeout test you may not necessarily want that cluttering up the results on on your dashboard so let me show you how to do that so this is the test builder of k6 it's like a graphical ui it's a graphical way to create a load test but this will also work for scripts that you put in so see you can click script here and you've got all of those details there as well of course you can add requests if you want here but let's go to the cloud apm section and there's grafana cloud so you can enable that or or disable that and then you can go ahead and run that so then we can save and run that so this is when it's it's doing the same thing that we saw in the cli except this time we fired it off from the um from the the ui and this cool little animation is just showing that all of those scripts are being moved over to the new load generator on aws cloud on aws on our cloud so we i think that k6 cloud is is really handy for instances where you don't really want to set up your own infrastructure and you just want to be able to get started without any monthly commitments without worrying about you know the quota that you have to set with aws and that quickly we're already getting results from the test this one was a 20 virtual user test you can see the requests here you can also look at the you can look at more charts here you can add charts actually if there's something in particular that you are interested in and that creates a another graph that you you can also add to comparison here except this one doesn't have i probably should have chosen another one but you get the idea let's have a look though at what that looks like because the prometheus remote right uh plugin does something else so it doesn't come with any ready-made dashboards but here's what it looks like so you just select the prometheus for your grafana cloud on here as the data source and then it's only k6 that i've got set up for for this one so it's all k6 metric so you can select what you've got so this is just the one that i did let's head back out this is for a test that i that i ran previously but we can definitely check out the test that we're running right now so let's just say the last five minutes that should work yep that makes sense so that was like a minute ago so this is our test 20 users uh one iteration per second so far with a response time of 5.72 seconds at least that was the last one so these are two different ways to use to integrate graffana with k6 okay so the question is when should you use the k6 data source plugin and when should you use prometheus remote right well the k6 data source plugin is better for recreating k6 cloud dashboards and if you like what you see already on k6 cloud then you this is the easiest way of just bringing that experience over to grafana if you have many past tests that you ran before integrating with grafana then this is the better way to bring all of that over without having to rerun them again it's good because it comes with a pre-built dashboard so it's probably the fastest way and prometheus remote right is better if you're load testing with k6 oss and not k6 cloud i didn't show that way for the sake of time but you can output k6 results to telegraph which will then forward the metrics to prometheus we're also working on an integration that an extension that will do this without having to go through telegraph but that's going to be a few weeks out yet but it's it's very close it's also good for incorporating k6 into a stack that already uses prometheus that way you'll have your load testing results where the rest of your metrics are too and then if you're utilizing multiple apm providers that already use prometheus this is the way for you i think that prometheus remote write is probably the easiest setup if you're using both grafina cloud and k6 cloud way you choose using k-6 and grafana together helps you get a better idea into the inner workings of your application so kasich's and grafina gets you both sides of the puzzle an observability tool stack to see what's happening and a testing tool to generate the data and that is it we're out of time so just in time as well here are feel free to ask me any questions if you have any that occur to you right now otherwise here's a list of resources that might be helpful
Info
Channel: k6
Views: 866
Rating: undefined out of 5
Keywords:
Id: gvounvDSDGg
Channel Id: undefined
Length: 20min 1sec (1201 seconds)
Published: Tue Oct 12 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.