How to clear cache for a sampler in JMeter - jmeter

I have a jmeter script where I need to open a page multiple times. Hence, I kept this action under loop controller. What I observed is the response time of loading page is decreasing for every iteration. This could be due to caching. Can anyone help me on how to clear cache for this sampler after every iteration (I am referring to loop controller iteration here not thread group one).

Your requirement doesn't really make a lot of sense as well-behaved JMeter test should represent a real user using a real browser and real browsers caching strategy assumes minimizing the amount of downloaded assets by obeying Cache-Control headers
If you're aware of this good practice and still want to discard the cache for each iteration:
Add JSR223 PreProcessor as a child of the HTTP Request sampler which cache you want to clear
Put the following code into "Script" area:
sampler.getCacheManager().clear()

You can use HTTP Cache Manager in JMeter and check this checkbox to Clear cache each iteration?. You can follow the page to learn more about this. This applies to all the requests in the current thread. I know this is not what you are looking for but JMeter has only this option as UI setting.
Another way can be to use Beanshell PreProcessor to intercept the request and clear the cache on the run.
import org.apache.jmeter.protocol.http.control.CacheManager;
CacheManager clearCache = ctx.getCurrentSampler().getProperty("HTTPSampler.cache_manager").getObjectValue();
clearCache.clear();

Related

jmeter cache doesn't clear in the next iteration

I've faced the problem that JMeter doesn't clear the cache and extract variables in the next loop iteration, ruining the following request in the next loop.
problem itself
I tried check-box 'Clear cache each iteration.
Then I turned off the cache manager at all, but at the beginning of the next loop, I noticed that variables were left from the previous one.
1-2
I even ad the pre-processor at the beginning of the scenario to remove all variables and clear the cache. Didn't help.
3
JMeter does clear the cache, here is the evidence:
As you can see
when the box is ticket both requests download embedded resources
when the box is not ticked 2nd request only fetches HTML DOM, no requests to embedded resources are made
So most probably your expectations regarding how HTTP caching should work are a little bit wrong.
Also JMeter doesn't "clear" any defined variables so you might see "old" values during the next iteration, if you have Logic Controllers which rely on certain variables presence - you need to reset the values manually somehow.
Have spoken with my colleague about it and he said that it's well-known bug in JMeter.
More details are described here:
https://groups.google.com/g/jmeter-plugins/c/3RU_O9JXiYw/m/aovwl1SuAQAJ?utm_medium=email&utm_source=footer&pli=1

JMeter understand Thread Group / Throughput and INCLUDE

I have a few question to clarify on my understanding of how JMeter works.
a. Thread Group determine the number of users but it does not determine how many HTML requests are generated per sec ? By default, I notice that every user will send a HTML request at a rate of 2 RPS.
b. If I want to change the RPS per user, then I need to use the Through Put Timer. But the Timer can only lower the request rate from 2 RPS to a lower number. It does not increase the RPS.
c. In order to increase the RPS, I need to add more Threads.
d. Does this mean we are limited to 2 RPS per user ? I see some website have links to many other websites so a webpage refresh would make many requests.
Is this the way JMeter works ?
I have a load test which has 8 transaction (eg CRUD,...). I intend to create a overall Test Plan and I want to use INCLUDE to add all the 8 txn. Do I just record the website and INCLUDE ? What should I include, only the HTML requests ?
I'm also thinking of adding Think Time and Add Variables in the 8 scripts before I INCLUDE.
Do I add the Config Element (eg CSV Dataset Config) in the 8 scripts or the overall Test Plan ?
Thanks.
By default each JMeter thread (virtual user) executes requests as fast as it can. If you want to slow JMeter down to mimic a real user which doesn't hammer the server non-stop and needs some time to "think" between operations - use Timers. More information: How do I Correlate the Number of (Concurrent) Users with Hits Per Second
If you want more RPS - add more threads (assuming that the system under test can give you more RPS)
You should INCLUDE everything which is related to your website (images, scripts, styles, fonts, sounds, etc.) but in the same manner as your browser does, i.e. don't record these requests and instead configure JMeter to download embedded resources and use HTTP Cache Manager so JMeter would request these resources just like browser does. Any requests to "external" websites should be excluded (unless they're also developed and supported and in scope for testing)
That's a good approach, if you use a value more than once it makes sense to declare it via User Defined Variables so you would be able to amend the value only in one place
You add it according to your scenarios, be informed about JMeter Scoping Rules

Jmeter, delay http request with many extracted urls

i have a issue with to many calls to the server.
I have extracted several urls with the "regex extractor".
In the next step, a "http request" calls these urls by ${extractet-urls}
But all requests after the 8th url gets a error 500 response from the server.
I tried to input several timers between, before and everywhere else, but it hasn't an impact.
So my question is:
how can i delay in this single http request which calls all the extracted urls?
Thanks for your help :)
After the requeat you can add sampler ->Java Request. Then change classname to SleepTest and it'll wait 1 second (configurable)
Add a Constant Timer as a child of the HTTP Request sampler (see Scoping Rules for details) and provide desired delay there (in milliseconds). It will cause the relevant thread to "sleep" for the defined amount of milliseconds before executing the HTTP Request. See A Comprehensive Guide to Using JMeter Timers to learn more about using Timers in JMeter tests.
Another option could be using Test Action sampler to create a delay, it doesn't generate sample result so you won't see it in .jtl results file.
The final approach is depending on what you're trying to achieve and how your test is designed.
Alternatively, you can add a Thread Group and define a ramp up time, then put the request inside this group. The ramp up time takes the startup overload too.

Delay In JMeter

I am using JMeter for tasting a site. I am creating a GET request to an URL which returns a html document. From that document I am extracting a value using XPath Extractor which is the key of next request URL I am going to make.
The problem is, for single user its working fine, but for multiple users sometimes it fails to extract because of getting partial(not full) html as response.
How can I make the extractor to delay a bit before starting to extract the key or Is there any other way to get the entire html document before starting extraction?
Can anyone help me with this?
Well, partial response indicates a problem and setting a delay is not what you should be looking for as XPath Extractor is a Post Processor hence response data will remain the same no matter for now long you will be sleeping before XPath Extractor fires.
Few things to consider:
All post processors and assertions have their cost and XPath Extractor is the most "expensive" one as it builds DOM in memory. In case of large responses and severe loads it's behavior may be flaky. Try switching to Regular Expression Extractor or CSS/JQuery Extractor
Make sure that JMeter has enough memory allocated. For Windows look for set HEAP=-Xms512m -Xmx512m line in jmeter.bat file and increase max heap value (Xmx) to be approx 75% of available RAM.
If you're running tests using JMeter GUI - consider switching to command-line, Ant, Maven or whatever mode. See 5 Ways To Launch a JMeter Test without Using the JMeter GUI for options available
Make sure that you're following recommendations from JMeter Performance and Tuning Tips post.
Hope this helps.
Use "Constant timer"
right click on your HTTP request --> Timer --> Constant Timer
hope this will help.

jMeter Multiple HTTP Requests

I want to test a fully functioning website for load using a constant, known number of users - to that end I'm trying to recreate the "Retrieved All Embedded Resources" functionality for a web-page, only manually, because I really don't know if it fetches all resources grabbed by JS. So the first question is - how do I check to see what these subsequent fetches retrieve?
Second question is - how do I make the multiple requests atomic, like "Retrieve All Embedded Resources"? I need to use "Constant Throughput Timer" for making sure the number of vusers is constant, but:
When using "Retrieve All Embedded Resources", this counts as one request, and one thread handles it right (hopefully, again - can't tell what goes on beyond the scenes)
When using a recorded session with numerous elements, each element is one action and occupies the queue (counts as 1 sample for Constant Throughput Timer). Therefore, it's not atomic.
I guess I can count the elements and define them as number of samples for throughput per minute, but this won't do in the long run.
First of all, jmeter does not execute any javascript in the pages retrieved. Clicking "Retrieve all embedded resources" does the following if you check the documentation:
Tell JMeter to parse the HTML file and send HTTP/HTTPS requests for all images, Java applets, JavaScript files, CSSs, etc. referenced in the file.
So it will check the current sample for any references and retrieve those, but it will not run any scripts that are retrieved.
If you want to check which resources jmeter is actually retrieving you could run for example Fiddler to check which requests are being made.
You can use Transaction Controller to consider all embedded resources requests and master request as one sample, aggregate time will be logged and reported.

Resources