JMeter_User
JMeter_User

Reputation: 303

How to clear cache for a sampler in 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).

Upvotes: 1

Views: 2553

Answers (2)

SushilG
SushilG

Reputation: 731

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();

enter image description here

Upvotes: 1

Dmitri T
Dmitri T

Reputation: 168092

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:

  1. Add JSR223 PreProcessor as a child of the HTTP Request sampler which cache you want to clear
  2. Put the following code into "Script" area:

    sampler.getCacheManager().clear() 
    

Upvotes: 1

Related Questions