JMeter Tutorial: How to Create Dynamic JSON?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome in today's video we will see the Advan Jeter concept of creating Json data payload using an groovy before actually going into the creating a data and why we should use an groovy let's try to understand the some business cases so suppose you have to do the load testing of an user registration where you are expecting that you're going to onboard 10,000 users in an are so obviously to do that you would need a 10,000 credentials like an email and password and for testing purposes even if you wanted to use a dummy or imaginary email ID you would to need to have 10,000 super record present right so in that case instead of maintaining 10,000 email addresses and password in some file you can use this particular approach it's very lightweight and easily scalable on any kind of an distribution so if you want to go from a 10,000 to million you should be able to do that without much of an H the Second Use case is about about an e-commerce site suppose you wanted to check how particular product works if you have like thousands open reviews and you wanted to see performance open review service if you want to add like a th000 review a particular product within 10 minutes or 15 minutes how would that particular service will perform and how will that product will get loaded so those kind of a scenarios or even if you wanted to add multiple r or multiple product so in those scenario having to multip you know maintain files won't be that easy you can just you know use this groovy to create the dynamic Json payload and that should you know suffice your need now that we talked about like two simple business use cases where we can use creating and dynamic Jon payload using in groovy you know let's try to understand what we will learn after by end of this video so you will learn the limitation of an CSV for parameterization don't get me wrong CSV works great and it would suffice your 80% of a need but 20% of a need It's Kind up an hassle so let me go through the limitation of CSV file so as we go increasing the number of users or number of scenarios or number of data items the size of CSV file will increase think about if you wanted to go with 10,000 software record it would been like a huge file and it if has like 100 columns so so file will be huge again the if you want to change something in this 10,000 so record you have to use some notepad++ or Excel or some kind of software to make those changes because opening it in Simple Text Editor it's not going to easy to maintain or make those changes into 10,000 so record and suppose you have to created a n nested Json or aggregated Json payload uh for your request then mapping this variable from CSV file to the actual J payload it's not easy and it's going to be time consuming and most importantly it's going to be error PR so so that's why one should not try to use CSV in such cases and they should always think about using creating a dynamic payload on the fly right you will also understand very simple concept how one can use thread group to do the load testing then uh understand what is basically the pre-processor you will also use DX sampler while developing the script you will also get introduced to the gsr2 to three preprocessor which supports The Groovy out of the box and then you will use this pre-processor to create the objects and the Chon payload and basically how to pass the Chon payload to make the particular call so this will be the Hands-On session we will most focus on using dor sampler and uh you know use Json server uh to make this call uh Json server is something that I have covered in last video so if you're not aware of it you know go through it right so let me show you what we have in a Json server so uh I am opening a particular let me open this in terminal I hope this is visible to you and I'm opening this in code [Music] so I hope this is visible now so I just have like one post one comment and one profile in my db. Json file and I'm just going to run a Json server file to watch this TV so you can see that this is now visible on okay I increased the overall font so it should be visible to everyone now okay yeah so Local Host is running on Port 3000 so I'll open this local host on 3,000 and you you can see that all this post comment and profile the object that we have seen in our code file you should be able to see it over here one post one comment and one type code the funny thing is if we change something here like uh Advance Jeter dynamic dynamic Jason P suppose we create something and if I save it you can see that it got reloaded again and if I go to my app and if I refresh this post page you will see that whatever title we have changed it got updated in this particular endpoint as well why Versa if we something you know get changes into this it will get reflected into this if we make changes using this particular Endo it will basically reflect in this database. Json file so instead of having real time apis we are just picking apis for our purpose so so that's about in Json server and how to have a Json server install getting started with a Json server I created it I covered it in my my last video so you should definitely check that out as well if you wanted to get equitance with Jason server so let's open on Jeter so let me create a new script add thread group okay so basically thread group is nothing but anything that we wanted to do like an user or our journey plan or our load modeling so that is what comes under the thread group I'll you can give any name like whatever you think is appropriate for your plan I'm just giving my channel name here for reference testing and tooling and and yeah that should be it I'm saving it first we will try to say you know uh try to get a particular call [Music] into JM what we have seen on the UI so posts right so I'll try to make this call and obviously we wanted to see the result how does it look like so I'll add a view result Tre and and I am making first call okay it is passed request and response Okay this may not been visible to you folks so let me increase the font of L Lo okay so basically I now I think it should be visible I am just making an get call Basic to post and try to see what kind of data that we are getting and I am making a request again so this is what we have here and I don't know whether it's visible to you folks or not so let me Zoom it in some way so you can see the response that you know something that we have seen in the browser here the same thing is being displayed here as well right so so that is how the get request look likees and it's very simple this also shows that you know whatever we have done as part of uh our our Json server uity is working fine as expected right so let's move to their next part so so we talked about that we going to cover lot many things thread group pre-processor debug sampler so uh first of all I'll show you you know if I can just make a post call and add an data and how can we do that right so we are making a post call and if you see the body it has title and author ID will be automatically generated by the Jon server so we don't have to take care of it so I'll just add another title uh title as a learn play right and author is and I am saving this and it most likely fail because we have not added any kind of an oh it worked okay it worked but it showing no ID so if I refresh this you can see the ID is gotting created but data that we are passed as part of an post request it is not working it is not working because it is not in Json so let me add it as in Jon I doubt it will still work so we will fix it anyway whatever issue that we get okay okay so same issue and before we fix the issue I wanted to show you the Json file as well so you can see that this this is automatically Chang change and you know these two records got added so I'll resave this I'll go to the browser refresh the page and I should only able to see the first page right okay so we have not added content type basically we were making a Json request but we have not specified anywhere in our request that what kind of uh request header would be there so this request header doesn't specify content type and it still shows content type text stand plan which is not true and we expect our Json server expect uh request type open application Json so let me add that config element and I'll add an HTTP header manager where we can specify the specify the our headers so and application Json right so we want type as an application Json okay it's not very visible let me zoom out a bit and I can show you in a better way so yeah so I have saved this and yep if it is if it is not visible just added an content type application Json so that you know whatever data that required is getting displayed over there so now I'll run the data and and you will see that in request body we are saving passing and title and response response we are getting full response and you can see that this request is you know shown title and author as well so if we go to the browser refresh it you can see the particular title is added it okay cool right so now this is covered uh if you see this in the rtb doj you can see this you know particular call has been post call has been made it took this much of a time and this particular has been added to this right so suppose you know if you want to add a 10 entries and you want it to some Dynamic data you don't have to you know pass this or create this data pass it through the particular CSV file you can think think about you how can you create an object or model an object right uh so we will do that but prior to that we talked about how can we uh use a debug sampler and pre-processor right so debug sampler is something that you know you wanted to print or before you wanted to make a request you wanted to see what kind of data that you are sending you can use the debug sampler for that particular reason right so let me post create post so we are creating post resultly header manager so that is what we are doing and I'll just add a debu sampler here so that you can see uh debu sampler basically shows all the properties and uh you know all the variables that you have created so if I make a run you can see that the default variable in the jmeter are being displayed over here as part of an debug sampler and that's it right in the response body and uh nothing in that response head and if we Define something it will get displayed over here okay now we talked about debug sampler we will get introduced to the what is the pre-processor suppose you want to make an any request but before making that request you want to do some pre-processing on the data that you have that is where pre-processor comes into the picture and there are different type of pre-processor available in jmeter right so if you go in sampler uh no not in sampler but in pre-processor you will see that multiple type of pre-processor like uh user parameters then regex uh regx user parameters JD pr3 processor and jsr pre-processor so these are like most used pre-processor there are some extension as well that comes with the J that you can use to add other pre-processors but we will not get into that right now uh so so we will just talk about jsr to the pre-processor so this pre-processor uh so I have added it to the scope so this is now added as a part of a child of debug sampler what does that mean so if I create something that will go you know that that is only applicable to this particular sampler right so so earlier we talked about you know uh while we were talking about business cases we we talked about how can we create so many different users email and password right so so let's take that as an example uh and try to create something I'll just zoom it out so that you know the code is available so before you know actually we have to before we actually use a Json uh in a gry we have to import the Json Builder how Json Builder works if you're not sure there is a video on the channel for same as well so I have just imported the Json Json Builder uh okay now let's say we have want an user as an something uh which has an username password and email address something kind of an object that we need to create so let's define it here in groovy and very simple that okay we need an username uh let's send username is Amo let's keep it simple uh then let's say the password email okay then you can email me at to Johan gmail.com and Suppose there is a password this is what you need you know as to create an user so I'm just creating a thing let's say password one 123 right and obviously this is an an object that we have creating aray object let's convert into the Json because mostly with race TPS we want to use in Json so let's use in Def Define the Json and something that we have imported earlier here so we will create an object of it using new Json Builder and so object that we have created and let's print it to let's you know change the format to the pretty string you can just you know have it as a string as well but pretty string will give you in a readable format and let's put this into some variable as an uh user or user registration user registration and we have Json so mind that the Jeter only support creating a variable in string format so whatever variable that you create even it is in Json you have to convert into the string So within Json Builder there comes two functions that you can use two methods two function two string or two pretty string I'm going with the pr string so we can print it now that I don't think if code is visible but let me show the code you again I'm importing this dependency I'm defining what kind of an object that we will need and once we know the object uh using the imported dependency pass the object as an parameter and as I mentioned the geometer accept the variables only in format open string so converting it into the string using an printing string method there is another method but for a time being we will just focus on this now we are putting this in here and let us run it one more time I'll just disable this for now so that we don't create lot of D data similar data and I'll run it and you should be only able to see this debug sampler right and if you see the response data response body and you should be able to see in debug sampler this user registration payload that we created right so you say that okay whatever you know you wrote in uh pre-processor you can see it here but how can we passed that asn't parameter that's very easy uh and I I should be able to show you that uh if you go to this request there is nothing is showing in the request right so if you want in deug sampler okay no you need a demi sampler for that but let me show you using a demi sampler if I have a Dy sampler Deb sampler Jr okay I have not added the D sampler but you can fake the sampler as well there is an plugin as an dumy sampler that you can choose maybe we can you know make a separate PD for dumy sampler okay so you saw what we have you know we were able to create but you said that this is all hardcoded there is no point in writing this down instead of that it wouldn't it been easier to just parameterize but let me show you know if we can just use something like very simple that that is already there within Jeter and using our code and that should just help us out right so let me show thread n so this is an one of the method that Jeter provides thread function in Jer so if you go to the functions you would be able to see that it it actually provides give you okay we are not at thread um yeah so you can see over here so it's it's part of an official documentation you should be able to see it over here thread number functions and variables how can when we use a thread number and other things right so I'm just using this thread number in uh in the Jeter here so that you know I'll create a oneor as thread and I'll just add something in my email ID as well so that you know I get a different email ID there is a no need to change the password but for time being you know I'm just adding a password here as well so that we can see so if I run this now in D sampler you see thread one so what is thread one so I don't know if it's visible so so you can see you know this everything has underscore 1 underscore 1 underscore 1 and this is because in our pre-processor code I have added this underscore and whatever this thread number is right so so this underscore and thread number so that is you know helping us out creating this particular kind of an object but this is still one how can we go and create multiple kind of an object using groovy right so let me show instead of here one why don't we use something as an you know variable and let's say thread count okay so thread count and then this thread count and could been Dynamic value that you pass to the JM at a run time uh so I just went to the you know thread group and I given the given is thread ground so so thread ground is something that I'm going to use but obviously it has to be variable so we have to choose an geometer variable kind of a syntax like this right so so this is the syntax and thread count and we can pass this in command line parameter the Jeter but for now what we will do we will just create an config element CSV data set conf no not in CSV we not going to use CSV so no point in add CSV so let's say user defined config us a def variable we write so add add add and add right so okay and just to understand you know what is the value that we are giving I'll just print it over here so let me show the changes that I made so the debex sampler I added a thread count as an you know in the name so that we should be able to identify the debug sampler belong to the which thread and in the pre-processor I am not making any changes but I have created this thread count and given initial value as and five for user defined variable right and let me put it over here so this thread group now has a five thread so it will run Within five thread if I run this okay 1 2 3 4 okay because okay thread count it took value okay I my bad so I given value as a thread I am not using a function that I am using right so ideally I have to use this function to get this 1 2 3 4 5 but if you see the result of this you can see if I okay let me run it one more time and show the result it will make much more sense okay now if I show you the result so you see third number sampler and it's you know having the underscore 3 as an like a post piix because of kind of a code we need right so first if if I choose to 2 2 two right so so using this pre-processor you can create Dynamic object and you know pass it suppose you now have to create like a 10,000 users you just need to you know change users depend processor and it should be able to create that many you know that kind of an huge data right so this is how you can use debug sampler and pre-processor in a j meter right so you saw very dummy example where we are creating a data so let's put this to some use where we actually you know saw we were creating a post where we like uh we created an Post in Json data right here okay and you could see that same data because we made the same call twice and it just have a different ID but same data so let's try to use this and you know create something thing like a nice thing and okay so we have to make this is an Dynamic thing right so I'll just increase the font while I type it out so that you focus don't miss out on anything and I'll again walk you through the uh I'll again walk you through so that uh you don't miss out on anything uh from the code so I I'll walk you through again okay so let's say I have to import the importing gry W dependency groovy do Json do Json Builder right so okay and let's say we need this particular thing right so let's I am defining post and post has this title and just taking this out because we will use the converter so title and post this should definitely work but we want something as a dynamic right so we won't be using it here using directly in body we have to use an pre-processor which I actually missed so let me add it here as in pre-processor uh I was trying to pass on parameter and then I realized parameter option is not there and then I realized oh I am not actually you know creating it I'm just writing it in Pre instead of prepro processor I'm just writing in data body data open request so we are here and we want to pass in some parameter as in random string right so so there is some function in Jeter for that as well which is random string so we will use that random random string okay and you have to mention uh how big string you need and then you need to [Music] mention characters that you will need in your string right so a b c d e f g h i j k l m n o q so I'm going to just restrict it to this so it will give create a string and it will give me string from this particular characters for up in the 10 right but these strings I need to consume in my in my code in my groovy code right I just wrot things so for to do that I'll just use diff input and input and accept the argument of this particular parameter that we are passing if there are multiple parameter parameters that you want to pass you can just you know uh separate as an uh comma and you can see that you know parameter type string as always and you can accept as an string I'm accepting as zero if it is second I'll accept as an one argument right which is already given over here so if it is not visible I can show you right so so parameter type string and then consume how can we consume it over here and then you can see this is the function that we are using I'm not sure how I make a typo but we will figure it out and then the code remains same right so instead of this what we will see learn playright uh learn gometer and we will just add this input to this string right so we we should be able to do that right so so there could be multiple ways we can do that we can create an parameter we can create a class so so let's try to keep it simple let let me see if you know this going to work if not we will try to figure it out and way to do that so author I'm going to keep the same I'm just going to change the title and obviously we have to uh convert this into the Json and Json to the payload again um new Json Builder why don't I copy this and use it and I'll give this as an post and to pretty string okay to pretty string or to string the anything should work fine but we need this as a variable what should be the variable name let's say post post and thenon right before running this I'll just you know change this thread count to the one so that we will just see and okay we are making call here but you know why don't I add it under this Json sampler and I keep this disabled so this new w we will test and then if it is looks fine then we will put it in our code okay okay create post to F because we have not added a data okay response data response body let me check okay create sample let me disable this let me clean this up and let me run debug sampler okay error yes there is some looks like there is error yes what is the error problem in jsr script no search property AR for class okay what did I miss AR for class so let me check okay AR it should be ARs and I wrote an AR so that's why it's not working as expected it's good that you so okay there is no error now I should be able to see it okay so post you could see that you know in post there is this this data is getting appended right we are getting getting the required Json so let's use this Json as part of this is pre-processor that means whatever we are putting here is working fine and I'll just use post in my body and let me see if it is working create post yeah it is working you can see now that this something you know got created and let me change this and let me show you know if we create using like 10 you can see you know this is not needed for our need but you should see that so much data is got created and same here right if I delete everything all the posts and if I run this there is post API don't have any data but if I run this now it should have 10 results because essentially we are running it 10 time right create post and you can see the Json file is already updated and if I go to the API it will work out of the box okay so that's it for video we are we have not seen any embedded or uh aggregated J in this video but if people shows interest we may cover that as well thank you
Info
Channel: Testing&Tooling
Views: 2,382
Rating: undefined out of 5
Keywords: jmeter, jmeter tutorial, performance testing, JSR223 PreProcessor, Debug Sampler, JSR223, PreProcessor, groovy, Dynamic Json, Json Payload, jmeter load testing, jmeter tutorial for beginners, Jmeter Advance
Id: KHnhoEsyu0g
Channel Id: undefined
Length: 36min 51sec (2211 seconds)
Published: Wed Oct 04 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.