Maximize Performance Testing | Integrating k6 with Grafana for Cloud Testing (Part 2)
By Huzaifa Asif
Summary
Topics Covered
- Run k6 Tests Locally, Visualize on Cloud
- Grafana Test Builder Simulates Global Loads
- Cloud Limits Virtual Users to 100 Max
- For Loops Multiply Requests Beyond Limits
Full Transcript
hello everyone so in this video we will be discussing about how you can run your load testing on the cloud using k6 and
grafana so we will be running that all the test cases on our local machine but the results will be displayed on grafana and we also have option to run all the
test cases on grafana as well so it is a very exciting video and let's get started so this is grafana so we will be running
all the test cases using grafana and we have k6 installed in our local uh machine so and we have all the scripts for load testing so what we will be doing is like we will be running the
commands from our local machine it will be uh executing all the load testing different scripts and all the results will be pushed to grafana and uh let's
get started like let me show you the script so we have like different scripts like smoke testing load testing and you can uh see my previous video where I go
into detail like how you can install uh k6 in very depth I have showed you and explained you regarding all the test cases what are these test cases means so
if you have missed that like uh do click uh on the link above and you can watch that video first and then come to this video so basically what you have to
first do is like running a normal test case is basically if you have k6 install you can do k61 and then the test case
name right like this so you can do this and it will start a particular test case so but we need to run that test case and on the cloud and
we also need to visualize all the graphs that how many requests are processed and how many are failed so for that we need to use grafana so
let me open up the browser and I need to create an account so I create an account
and then we can uh show you how it works so now you can see like I have successfully signed up in grafana and it is telling me that it is loading my
whole instance and setting up everything so I will give like few minutes and then uh I will show you
so now my instance is initialized so now it will show all the options so or either what you can do is you can like
on the Google you can search uh grafana load testing and it will also show you this website like this particular page
and you can click on like create free account and you can create account from there as well so and then like in my case I have just like created my account
so I will click on my account and then I will see the settings page and it is telling me that you have a free trial for 14 days so
I need to launch the grafana instance and I need to set up the keys first uh of grafana in my local machine and then
I will start running the test cases so now you can see like uh my grafana cloud
is set up and grafana has automatically set up a subdomain for me and I can see this right here and then what I need to do is on the side menu as well you can
see performance testing in uh on the dashboard as well you can see okay you can see k6 I will click on start testing and
it will take me to the testing section of grifana and it will ask me to set up the environment so I will click on start testing it is telling me that you can
install a grifana k6 first you need to install k6 uh so like if you're using Mac you can just use Brew to install it so I have actually installed it so I
won't do that and then what you need to do is you need to copy this and you need to run this particular command so I have opened up my terminal and I
will run this particular command okay so this is the terminal and this is the particular command so it is saying me that you have
successfully set this up and then you can create a script I already have a script as you can see like I have different scripts like smoke testing
load stress so I don't need that then it is saying me that you can run the script using k6 Cloud previously what we are doing is we are doing k6 run and the
script name right and but if you need to run it on cloud then you can do k6 Cloud so this is the only difference so in my
case for instance like I do this like A6 cloud and then I put the test case name here and
then like all these uh particular like test case results will be displayed in that particular
uh cloud grafana cloud so yeah I have like set it up everything and if I go to projects it is telling me that a default project is created and if
I open that it is telling me that this smoke test is running right now so it is showing me you can see like this is the test case name smoke test and
you can see like it is showing me all the test case like right now it is running and uh there are three virtual users and the status is running and once
this test case is finished you can see all the results like how many HTTP calls are made and how many are like uh failures
so let's wait for a few seconds and then we can see the results and other than that you can go to settings and you can see your API token and environment
variables uh you can uh do like a lot of like you can schedule your test cases you can like uh have like different sort of test cases and
then you can schedule them like uh on every day uh like at night run these test cases so that you can check like your server capacity and your everything on the back inside or front inside is
running uh as planned so you can do that you can see like different dashboards and everything so now it is telling me that the test case is finished and
these are the results right so 150 requests are made 35 are failures and uh request per second is three and a P95
like 95 percent of the request uh have taken like 256 millisecond and it is showing me the whole like uh request rate response uh time failure rate
everything so right now few requests are failing because I have rate limiter attached so if I make like a lot of requests then the rate limiter actually stops them so that no one can exploit
the server so I will uh basically turn off the rate limiter and I will then show you uh how it works so you can see like all the requests pass all the
requests that are failed okay and you can also see the logs so if you have console log in your script and with every request like I have you can see actually all the logs here as well like
this is the request and this is the uh basically like uh instance and then this is the particular log that has been logged like and then you can see all of that like even in the case of uh for
instance like if I get an error that you can see like what is the error like messages too many requests please try again later status code 429 like this is the rate limiting error
and you can see like all of that you can see on the check section like how many requests are successful how many are failure what is the success percentage
and what is the failure percentage so this is the whole uh flow and you can like run tests from here as well if you want to rerun it you can just like
rather than running it from terminal you can run it from grafana as well so you can see like there's no activity here so whatever script you have run from
your local machine you can also see that uh in grafana so what you have to do is you have to basically like uh go to that particular test case select it and click
on configure and then you can see this is the test script like this is the exact test script and all that code that I have run from my local machine uh I can see it but I cannot edit it and uh
it is telling me that it is read only the script was executed from CLI so if you want to edit it just like copy it
like and paste it in your terminal and edit it and run a case X cloud and the file name so but if I need to like rerun
it like then I can do it like I can rerun it like that's not a problem so if I want to create a new test case I can do that as well so
they have a script editor as well like this is the script editor and uh this is the particular URL you can like change it like for instance I change this to
like 20 second and I change this to 30 seconds and then again in the end 10 seconds right uh this is like I think
their default test case like create and run and then you can see like you can also like just simply create a test case using uh grafana editor and then they
also have a visual editor in the visual editor like it's not code it is like more uh settings drag and drop so it is more easy so for instance let me
show you that as well so if you click on create new test and one thing is like just create it using the backend in your own local machine and then run it using
uh k6 Cloud but um then this option script editor then we have test Builder and test Builder you can first select the load Zone what is the load Zone what is the distribution you can add like
multiple uh loads on like I have like for instance I select like two three different loads on so you can do that as well so and then
you can like uh divide the distribution you can see that here right even or manual manual mean like a lot of requests from this region a lot of requests from other country you can
simulate like what uh are like you you know that your application you will have like a lot of user from India then you have a lot of user from us you can
specify that according to your own application and uh most in most cases what I do is I just like keep the region like one I do not like play around with
this then you can select the threshold uh the URL and all that conditions so like for instance my threshold is like
uh response time then you can it is asking me about the URL so it is asking me about like all that thing so this is
like basic uh like scenarios I can add scenarios like test Builder script I can do that and uh basically I can specify my script and whole scenario and then I
can um also like we have like both option like we can use like a test Builder to to specify a scenario using this particular
uh thing and you can also have a script to specify a scenario you can click on script and just like write the code or
either click on options and uh specify the options right so this is uh pretty much simple and then you can then in the end like click on Create and run test
case and it will like run that particular test case and you can see the results okay let me go back and see like this is the test case that we
run right like it is telling me that this this is the result of that particular test case this is how you can do it and one more thing like if you have like a lot of virtual users then
the the cloud will give you error because on your local machine you can have you can specify like thousands and uh in like millions of virtual users so
because all the processing and CPU intensive and RAM intensive work is done on your local machine you can like create whatever script you want but if
you are running a test case on cloud and you have like a lot of virtual users and uh then it will not allow you like let me show you that like uh so you do not
need to worry about that like it is not allowing me to run the test case so for instance in my stress test I have like a lot of
virtual users right so let me open up the spike test for instance right like I have two thousand so if I basically do k6
cloud and then specify this particular I'll name it will tell me that uh yeah you see uh you have like a grafana cloud
uh free subscription so requested number of virtual users not allowed 2000 is greater than 100 like I have a limit of 100 maximum virtual users I can either
upgrade my plan or either like send request less than 100 users okay so this is it like and if you want to like send
a lot more requests than your particular limit then what you can do is you can also do this you can add a for Loop like what for Loop will do is it will like
multiply the number of requests for that those particular virtual users like for instance without for Loop you are sending like 1000 requests and you if you add a for Loop you can send like
around 10 000 requests you can like just like multiply that like so you can also use this technique I have personally used it as well so let me just quickly run the spike
test for instance I cannot run it on cloud so I would recommend like for all these heavy test cases you can just like run them on your local machine and
there's another technique as well like you can set up an inflex DB instance and whatever results we get we can uh set up influx DB locally and we put all these
results into influx DB and then connect that influx DB to our grafana cloud and also install like grafana locally as well so you have like a grafana locally
installed inflex DB locally installed and k6 locally installed then you run the test cases all the output of k6 is stored into influx DB and then you are
using grafana to fetch the data from inflex DB and show that so you can also do that but it is like uh very time consuming so yeah but if uh someone uh
wants to do it and he's like I don't want to pay for the out and I want to like set it up all by myself then there is an option for that as well and if you
guys like uh to see like how it works then do let me know in the comments I can also make a video on that as well so yeah and this is basically how the
request uh and you can see like we have like 2 000 virtual users and on local we have like no limitation like a lot of requests are sending and then we get the
data back as well and on top of that let me run a test case on the cloud as well for instance this particular test case
k6 cloud and this load testing so this test case has like 20 virtual
users so uh this test case will be executed on the cloud so right now in the start like whenever you run the test case on cloud it will first validate the
test case the options and then run the test particular test case so it is validating it and then it will
start running it so let me stop that it was just to Showcase you like if you want to run heavy test cases then you can do it locally and then again we I
have like a separate video for that where I have explained like what is the meaning of all these test cases how to set it up what is meant by
each particular line of code so if you haven't watched that like go check it out and this particular video is basically for like how you can integrate
k6 with grafana so let me rerun this particular test case and you can see like it is uh running that particular test case it is
showing me that it is that script is created and it is validated initializing and then it will start eventually start running and then you can see it here
like like load test right so it is showing me that it is basically uh initializing this particular test case and it will
about to run this particular test so yeah so yeah this test case is like started running and like if you open up like you
can see this test case is running right and I run this test case and then I stop it so it is like showing me that error is stopping this particular test case and this test case is right now running
and you can see right now like all the requests that are being sent and uh you do not have to like wait for the test case to end you can also like see in
real time all the results so this is also like very good uh so you can see like when how is the system performing and all that like nfq server is like
under intense load you can also like just uh press Ctrl C and stop the test case as well so yeah and you see like a lot of requests are
being made and you can see like there are no failures previously uh the previous test cases I run there are a few failures because I have as I told you like rate limiter uh applied on my
backend so I just like comment the rate limiter just to Showcase you like uh that if there are no like rate limiter or limitation then you can see like all
uh API calls or success so yeah like and then I think this is pretty much it like uh for grafana and
k6 so if you guys need like more insights into that then do let me know in the comment and until next time have a nice day
Loading video analysis...