简体   繁体   English

如何清除 JMeter 中采样器的缓存

[英]How to clear cache for a sampler in JMeter

I have a jmeter script where I need to open a page multiple times.我有一个 jmeter 脚本,我需要多次打开一个页面。 Hence, I kept this action under loop controller.因此,我将此操作保留在循环 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).任何人都可以帮助我在每次迭代后如何清除此采样器的缓存(我指的是循环 controller 迭代这里不是线程组一)。

You can use HTTP Cache Manager in JMeter and check this checkbox to Clear cache each iteration?您可以在 JMeter 中使用 HTTP 缓存管理器并选中此复选框以清除缓存每次迭代? . . 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.我知道这不是你要找的,但 JMeter 只有这个选项作为 UI 设置。

Another way can be to use Beanshell PreProcessor to intercept the request and clear the cache on the run.另一种方法可以是使用 Beanshell PreProcessor 来拦截请求并在运行时清除缓存。

import org.apache.jmeter.protocol.http.control.CacheManager;

CacheManager clearCache = ctx.getCurrentSampler().getProperty("HTTPSampler.cache_manager").getObjectValue();

clearCache.clear();

在此处输入图像描述

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您的要求并没有多大意义,因为表现良好的 JMeter 测试应该代表使用真实浏览器的真实用户,真实浏览器缓存策略假定通过遵守Cache-Control标头来最小化下载资产的数量

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添加JSR223 PreProcessor作为 HTTP 请求采样器的子项,您要清除哪个缓存
  2. Put the following code into "Script" area:将以下代码放入“脚本”区域:

     sampler.getCacheManager().clear()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM