You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在JMeter中实现多用户共享HTTP缓存管理器缓存?

Solution for Sharing Cache Across All JMeter Threads

Hey there, I totally get what you're trying to do—you want a single cache instance that all 10 of your test threads can reuse, instead of each thread maintaining its own separate cache. The default HTTP Cache Manager in JMeter is thread-isolated by design, even when placed outside the Thread Group, so we need to work around that. Here are three reliable approaches to make global cache sharing work:

1. Use the HTTP Cache Manager (Global) Plugin

This is the simplest and most straightforward solution, purpose-built for cross-thread cache sharing.

  • First, install the JMeter Plugins Manager if you haven't already.
  • Open the Plugins Manager, search for HTTP Cache Manager (Global), and install it.
  • Remove your existing HTTP Cache Manager, then add the HTTP Cache Manager (Global) to the root of your Test Plan (outside all Thread Groups).
  • Configure it just like you would the regular cache manager—set cache policies, exclude/include URLs, etc. Now all threads will share this single global cache pool.

2. Manually Implement Global Cache with JSR223 Scripts

If you prefer not to use plugins, you can build a custom global cache using JMeter's shared properties and JSR223 elements:

  • Step 1: Populate the global cache
    Add a setUp Thread Group (this runs once before your main test threads) and put your cache-warming HTTP request inside it. Then add a JSR223 PostProcessor to the request with this Groovy code:
    // Use the request URL as the unique cache key
    def cacheKey = sampler.getUrl().toString()
    // Store the response content in JMeter's global properties
    def cachedResponse = prev.getResponseDataAsString()
    props.put(cacheKey, cachedResponse)
    
  • Step 2: Reuse the cache in main threads
    In your main Thread Group, add a JSR223 PreProcessor to every HTTP request that should use the cached content. Use this code:
    def cacheKey = sampler.getUrl().toString()
    if (props.containsKey(cacheKey)) {
        // If cache exists, override the request to return cached data
        sampler.setResponseData(props.get(cacheKey).getBytes())
        sampler.setSuccessful(true)
        sampler.setResponseCode("200")
        sampler.setResponseMessage("Served from global cache")
    }
    
    This checks if the URL is in the global cache first—if yes, it skips sending the real request and returns the cached response instead.

3. Use a Shared File Cache

You can also configure JMeter to use a single file-based cache that all threads read from:

  • First, run a single thread to populate the cache:
    • Add a regular HTTP Cache Manager to a temporary Thread Group, set Cache Storage Implementation to File Cache, and specify a local file path for the cache (e.g., ./jmeter_global_cache).
    • Run this thread once to generate the cache file.
  • Then, in your main test plan:
    • Remove the temporary Thread Group, and add the HTTP Cache Manager to your main Thread Group (or root level).
    • Set the same Cache Storage Implementation to File Cache and point it to the same cache file path you used earlier.
    • Enable Use Cache Control/Expires header when processing requests if needed. Now all threads will read from this shared cache file.

Important Notes

  • For the script-based approach, JMeter's props object is thread-safe, so you don't have to worry about race conditions when accessing the cache.
  • If your cached content has expiration rules, you'll need to add logic to check and invalidate stale entries (either in the plugin's settings or your custom scripts).
  • The plugin method is recommended for most cases since it handles edge cases like cache invalidation and HTTP compliance out of the box.

内容的提问来源于stack exchange,提问作者Jugi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:18:44