How to Use k6 to Run Load Testing for a Website (for free)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone this is tony teachers tech i'm tony and in this video i'm going to show you how to do some basic load testing for your website with k6 so if that's something you're interested in learning how to do let's get on into the tutorial here okay so um the first thing you're going to do is to get k6 install it on your system whatever that may be i'm going to be using ubuntu version 20.04 as you can see up here so let's go ahead and install that the first thing we need to do is tell ubuntu that this package is available for install out there somewhere so there's a couple commands we have to execute for that and by the way i'll have all these commands uh down below linked in the description so first one is this apt key command and once you have executed that we want to go ahead and do an echo something like this i have to use my cheat sheet over here to copy it so it's uh echo this is where the um the binary lives for that installer k6 installer so we'll go ahead and add that to our sources list and now that we have done that uh just to make sure that your system is up to date to us apt-get update and then finally after that is finished we can do an apt-get install k6 so let's go ahead and finish up with that and this is what will actually install k6 onto our system all right so that's about it for the installation is concerned let's go ahead and run an example and k6 if you're working on the command line here in terminal whether you're on mac or windows doesn't matter or on a remote server somewhere you will be having to create some type of script okay and we'll start with the most basic simple script example so just create a file on your system called let's just call it script.js it's going to be javascript code again don't worry if you don't know anything about javascript or coding or anything like that just kind of like see what we're doing here and then you can expand upon that so we're importing um a couple different libraries here from k6 and then this is our main function right here okay and all we're doing is going to this ip address and making requests to it and then waiting one second so here is where you can customize what your on the test so this is just like a default ip address for me i have a website a wordpress website that i just created here at this ip address okay and that's what i'm going to be testing today so i'm going to copy that and it doesn't have to be an ip address okay this can be anything like a domain name it could be http https anything like that so whatever you're testing goes in here the http get okay so let's go ahead and save that and i also want to pull up another terminal window here and ssh into that same ip address just so we can kind of monitor what's going on if anything on the other side so you can do that with h-top uh that one is not found so let's do let's go ahead and install that sudo app to get install htap and that'll take no time at all all right so this is all of our processes going on the wordpress server we can see our cpu usage our memory usage so the goal here is as we run tests to the server we can see the cpu usage and memory usage maybe fluctuate a little bit okay so we got our monitor on the left we got our test server on the right let's go ahead and execute the script so you can simply do that with k6 run script.js do that and it's going to run the test and we barely see anything happen over on the left hand side let's make this a little bit wider so we can see everything on one line and we got lots of information here lots of data to look through we got and actually we won't go through all of these the one that we care about the most actually in my opinion is http request duration and this is how long basically from the time that the request was sent until you receive the response back that you're waiting and because this was just one test based on the code that we ran this is the average or the actual amount of time that it took for that test so 24.77 milliseconds now what if you wanted to run that test multiple times to simulate like multiple users going to your website well we can pass in a couple arguments to that same command that we just ran so instead of k6 run script we can do k6 run dash dash v u a v yeah dash dash v u s ten dash dash duration thirty seconds and what this is going to do is uh v u s virtual users it's gonna you know simulate 10 users coming to your website at a time for the duration of 30 seconds so those 10 users are basically you're not going to have any more than 10 at most 10 users coming to your website and they're just going to keep making your own requests like once they receive the request then they're going to make another request and then repeat that so on and so forth for the duration of 30 seconds so let's go ahead and run that and see what happens over on the left hand side with our monitor so it's going to run the same type of thing this time it's going to take 30 seconds and you can see the cpu usage moving around the memory usage kind of hanging steady there not too much action going on but we see all these www data processes that's um these 10 requests being sent out and actually the my sequel processes too because this is a wordpress website it has to go gather all that information from the wordpress database mysql in this case and send it back to php and all that stuff so that looks good so we got the results of our test done here now we see the average time that it took for those you know how many tests that it did it did 283 tests in those 30 seconds so and on average it took 30 wait no no 82 milliseconds this time so it went up because there is more of a demand on the server 10 users at a time at any given time making requests to the website so um cool so that's those are you know kind of two basic examples with load testing with k6 if you're interested we'll do a a more complex example with uh load testing so i'm going to use my cheat sheet again over here and we're going to modify our script that we had created so let's go ahead and open that up again and by the way if you don't know how to use vim or a text editor i'll have a video for you about how to do that so let's edit the script.javascript file and we're just going to kind of like scrap this and start from start from scratch okay i'll paste in the code here a few more lines of code but still nothing too extreme the first thing i want to do before i forget is to change this to the test url that we want to test out so what was that url that was and we can look over here it was http i forget if it's an s or not it's not secure so http colon slash slash 45.79.227.226. and i'll throw on the slash there okay so let's go through this code again we're just importing some some k6 packages here and this is what um is going to simulate a an increasing demand on the server and then it's going to plateau and then it's going to decrease back down to no demand so basically for for 15 seconds we're going to try to slowly but surely increase the amount of users hitting the website to 100 so we're going to start at zero seconds and there's going to be zero users on the website and then by 15 seconds into the test there's going to be 100 users on the remaking request to the server then for the next 30 seconds we're gonna maintain 100 users constantly at most constantly making requests to the server and then at the end of those 30 seconds we're going to slowly but surely again go from 100 users making the server making requests to the server down to zero users making requests to the server and that's going to be over the course of those 15 seconds so in total 15 plus 30 plus 15 that's a 60 second test and let's go ahead and run that so let's save it and instead of passing arguments we'll just do k6 run script and that's going to execute all that code that we just created so let's do that and monitor the left hand side here oops unexpected character okay so that is this first one we don't want that comment here so sorry if you copied that from me but we don't need that okay k6 run script uh let's go ahead and do that and now like i said this is going to be 30 or a 60 second test and over here on the well monitor the cpus it's going up over here on the left and we can see since we're in the first 15 seconds of the test we're slowly but surely increasing to 100 virtual users we are at 100 virtual users now we are for the next 30 seconds going to consistently maintain making 100 having 100 users make those requests for the next 30 seconds so um cpu usage is almost spiking to 100 here and there the memory usage is very close to maxed out we are 700 out of so maybe like three quarters of the way there and lots and lots of www data processes um and i'm sure if we scroll down we'll see all the my sequel processes too um lots of lots of stuff going on over here on the left-hand side so we are in the last 10-5 seconds of the test that means we're slowly reducing the number of virtual users on the server and now we are done so this time let's look at the results here we did 2418 tests over the course of those those 60 seconds and let's look at the http request duration this time 895 milliseconds so close to on average one second each of those virtual users had to wait for their request to be fulfilled from the server so as you can see the demand on the server has caused the server to slow down in the time that it's able to fulfill those requests um definitely a lot more than you can do with k6 than what we've seen here they actually have a whole like web interface that you can pay for like this is all free this is free and open source you can do whatever you want locally with on your servers as far as load testing and then they have extensive documentation for that which is great and that's that's i encourage you to take full advantage of that but um if you want something simple click and like instead of writing code kind of like drag and drop then they have that available for you too so something else to consider i hope you have a better idea about how to do load testing for your website again very basic examples but it should get you started in the direction of being able to do that um from here on into the future so if you like this video give it a thumbs up if you want to see more from me then definitely consider subscribing to this channel and if you do i'll see you in the next one
Info
Channel: Tony Teaches Tech
Views: 16,922
Rating: undefined out of 5
Keywords: k6 load testing, k6 test, k6 load testing tutorial, k6 load testing example, k6 performance testing, load test website, load test website free, k6 load test, k6 load impact tutorial, k6 load testing tool
Id: ukoC319npUw
Channel Id: undefined
Length: 12min 24sec (744 seconds)
Published: Fri Oct 30 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.