ehcache集群测试报告

这篇测试报告详细描述了在Java环境下使用Ehcache进行集群配置的测试过程。测试涉及缓存的添加和获取操作,通过在两台服务器上进行并发线程测试,观察缓存同步效率。测试结果显示,40000个元素从一台服务器添加到缓存后,另一台服务器在首次同步时耗时0.708秒。测试提供了缓存配置信息、代码解析和测试日志,为理解和优化Ehcache集群性能提供了参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.测试环境

CPU核心数内存主机地址
Server1816G10.2.6.77
Server2816G10.2.6.78

2.测试代码

TestController

/**
 * @Description:测试
 * @Author: gezongyang
 **/
@Api(tags="test")
@RestController
@RequestMapping("/test")
@Slf4j
public class TestController {

    @AutoLog(value = "缓存添加测试")
    @ApiOperation(value="缓存添加测试")
    @GetMapping(value = "/testPutCache")
    public Result testPutCache() throws InterruptedException {

        ShareData shareData = new ShareData();
        CountDownLatch countDownLatch = new CountDownLatch(4);
        CachePutThread cachePutThread = new CachePutThread(shareData, countDownLatch);
        ThreadPoolExecutor executor = new ThreadPoolExecutor(4, 1000, 5, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
        LocalDateTime start = LocalDateTime.now();
        for (int i = 0; i < 4; i++) {
            executor.execute(cachePutThread);
        }
        countDownLatch.await();
        LocalDateTime end = LocalDateTime.now();
        int i = end.getSecond() - start.getSecond();
        log.info("缓存添加开始时间:{}-缓存添加结束时间:{} 用时:{} 秒",start.toString(),end.toString(),String.valueOf(i));
        executor.shutdown();
        return Result.OK("success");
    }

    @AutoLog(value = "缓存获取测试")
    @ApiOperation(value="缓存获取测试")
    @GetMapping(value = "/testGetCache")
    public Result testGetCache() throws InterruptedException {

        ShareData shareData = new ShareData();

        CountDownLatch countDownLatch = new CountDownLatch(4);
        CacheGetThread cacheGetThread = new CacheGetThread(shareData, countDownLatch);
        ThreadPoolExecutor executor = new ThreadPoolExecutor(4, 1000, 5, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());

        for (int i = 0; i < 4; i++) {
            executor.execute(cacheGetThread);
        }
        countDownLatch.await();
        LocalDateTime start = shareData.getStartTime();
        LocalDateTime end = LocalDateTime.now();
        int i = end.getSecond() - start.getSecond();
        log.info("缓存获取开始时间:{}-缓存获取结束时间:{} 用时:{} 秒", start.toString(), end.toString(), String.valueOf(i));
        executor.shutdown();
        return Result.OK("success");
    }

    @AutoLog(value = "缓存清除")
    @ApiOperation(value="缓存清除")
    @GetMapping(value = "/testFlush")
    public Result flushCache() throws InterruptedException {


        CacheUtil.clear();
        System.out.println("----------缓存获取完成-----------");

        return Result.OK("success");
    }
}

ShareData

/**
 * @Description TODO
 * @Version 1.0
 * @Author gezongyang
 */
@Slf4j
@Data
public class ShareData {

    private int number = 0;
    Lock lock = new ReentrantLock();
    Condition condtion = lock.newCondition();
    LocalDateTime startTime;

    public void put() throws InterruptedException {
        lock.lock();
        try {
            ++number;
            CacheUtil.set(String.valueOf(number), String.valueOf(number));
            LocalDateTime now = LocalDateTime.now();
            log.info("放入缓存时间:{} - key:{} -> value: {}", now.toString(), String.valueOf(number), String.valueOf(number));
            condtion.signalAll(); //this.notify()
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void get() throws InterruptedException {
        lock.lock();
        try {
            ++number;
            Object value = CacheUtil.get(String.valueOf(number));
            while (value == null) {
                value = CacheUtil.get(String.valueOf(number));
                if (value != null) {
                    LocalDateTime now = LocalDateTime.now();
                    if (number == 1) {
                        log.info("获取缓存开始时间:{} - key:{} -> value: {}", now.toString(), String.valueOf(number), value);
                        startTime = now;
                    } else {
                        log.info("获取缓存时间:{} - key:{} -> value: {}", now.toString(), String.valueOf(number), value);
                        break;
                    }
                }
            }

            condtion.signalAll(); //this.notify()
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        System.out.println(LocalDateTime.now());
    }

}

CacheGetThread

/**
 * @Description TODO
 * @Version 1.0
 * @Author gezongyang
 */
public class CacheGetThread implements Runnable {

    ShareData shareData;
    CountDownLatch countDownLatch;

    public CacheGetThread(ShareData shareData, CountDownLatch countDownLatch) {
        this.shareData = shareData;
        this.countDownLatch = countDownLatch;
    }

    @Override
    public void run() {
            try {
                for (int i = 0; i < 10000; i++) {
                    shareData.get();
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                countDownLatch.countDown();
            }
    }
}

CachePutThread

/**
 * @Description TODO
 * @Version 1.0
 * @Author gezongyang
 */
public class CachePutThread implements Runnable {

    private final Logger logger = LoggerFactory.getLogger(CachePutThread.class);
    ShareData shareData;
    CountDownLatch countDownLatch;

    public CachePutThread(ShareData shareData, CountDownLatch countDownLatch) {
        this.shareData = shareData;
        this.countDownLatch = countDownLatch;
    }
    
    @Override
    public void run() {
        try {
            for (int i = 0; i < 10000; i++) {
                shareData.put();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            countDownLatch.countDown();
        }

    }
}

测试代码解析:

类名类说明
TestController测试类,提供缓存添加、获取、删除的请求接口
ShareData共享数据,里面封装缓存的获取和添加方法
CacheGetThread缓存获取线程
CachePutThread缓存放入线程

/testPutCache: 调用4个线程向缓存中添加数据,每个线程添加10000条

/testGetCache: 调用4个线程从缓存中取数据,每个线程取10000条

3.缓存配置信息

 10.2.6.77

 cluster:
   provider:
     peer-discovery: manual
     manual:
       rmi-urls: 10.2.6.78:40002  #缓存提供者需要通知的rmi地址 多个 | 分隔
   listener:
     host-name: 10.2.6.77  #缓存监听者端口地址 默认:127.0.0.1
     port: 40002
     socket-timeout-millis: 2000 #监听socket连接超时时间 默认2000毫秒

10.2.6.78

 cluster:
   provider:
     peer-discovery: manual
       manual:
         rmi-urls: 10.2.6.77:40002  #缓存提供者需要通知的rmi地址 多个 | 分隔
   listener:
     host-name: 10.2.6.78  #缓存监听者端口地址 默认:127.0.0.1
     port: 40002
     socket-timeout-millis: 2000 #监听socket连接超时时间 默认2000毫秒

4.缓存测试

(1)在 服务器10.2.6.78 上调用缓存的获取方法,这个时候因为缓存中还没有数据,四个线程会先等待

/test/testGetCache

(2)在 服务器10.2.6.77 上调用缓存的添加方法

/test/testPutCache

5.缓存测试日志

10.2.6.77

2022-11-15 17:00:01.361 [pool-1-thread-2] [34mINFO [0;39m [36mcom.icfcc.fcit.dp.modules.msg.test.ShareData:35[0;39m - 放入缓存时间:2022-11-15T17:00:01.360 - key:1 -> value: 1
2022-11-15 17:00:01.361 [pool-1-thread-2] [34mINFO [0;39m [36mcom.icfcc.fcit.dp.modules.msg.test.ShareData:35[0;39m - 放入缓存时间:2022-11-15T17:00:01.361 - key:2 -> value: 2
2022-11-15 17:00:01.361 [pool-1-thread-2] [34mINFO [0;39m [36mcom.icfcc.fcit.dp.modules.msg.test.ShareData:35[0;39m - 放入缓存时间:2022-11-15T17:00:01.361 - key:3 -> value: 3
2022-11-15 17:00:01.361 [pool-1-thread-2] [34mINFO [0;39m [36mcom.icfcc.fcit.dp.modules.msg.test.ShareData:35[0;39m - 放入缓存时间:2022-11-15T17:00:01.361 - key:4 -> value: 4
2022-11-15 17:00:01.362 [pool-1-thread-2] [34mINFO [0;39m [36mcom.icfcc.fcit.dp.modules.msg.test.ShareData:35[0;39m - 放入缓存时间:2022-11-15T17:00:01.362 - key:5 -> value: 5
2022-11-15 17:00:01.362 [pool-1-thread-2] [34mINFO [0;39m [36mcom.icfcc.fcit.dp.modules.msg.test.ShareData:35[0;39m - 放入缓存时间:2022-11-15T17:00:01.362 - key:6 -> value: 6
2022-11-15 17:00:01.362 [pool-1-thread-2] [34mINFO [0;39m [36mcom.icfcc.fcit.dp.modules.msg.test.ShareData:35[0;39m - 放入缓存时间:2022-11-15T17:00:01.362 - key:7 -> value: 7
2022-11-15 17:00:01.362 [pool-1-thread-2] [34mINFO [0;39m [36mcom.icfcc.fcit.dp.modules.msg.test.ShareData:35[0;39m - 放入缓存时间:2022-11-15T17:00:01.362 - key:8 -> value: 8
2022-11-15 17:00:01.362 [pool-1-thread-2] [34mINFO [0;39m [36mcom.icfcc.fcit.dp.modules.msg.test.ShareData:35[0;39m - 放入缓存时间:2022-11-15T17:00:01.362 - key:9 -> value: 9
2022-11-15 17:00:01.362 [pool-1-thread-2] [34mINFO [0;39m [36mcom.icfcc.fcit.dp.modules.msg.test.ShareData:35[0;39m - 放入缓存时间:2022-11-15T17:00:01.362 - key:10 -> value: 10
...
2022-11-15 17:00:03.503 [http-nio-9000-exec-2] [34mINFO [0;39m [36mcom.icfcc.fcit.dp.modules.msg.controller.TestController1:190[0;39m - 缓存添加开始时间:2022-11-15T17:00:01.349-缓存添加结束时间:2022-11-15T17:00:03.503 用时:2 秒

10.2.6.78

2022-11-15 17:00:02.068 [pool-1-thread-1] [34mINFO [0;39m [36mcom.icfcc.fcit.dp.modules.msg.test.ShareData:54[0;39m - 获取缓存开始时间:2022-11-15T17:00:02.068 - key:1 -> value: 1
2022-11-15 17:00:02.068 [RMI TCP Connection(4)-10.2.6.77] [39mDEBUG[0;39m [36mnet.sf.ehcache.distribution.RMICachePeer:180[0;39m - RMICachePeer for cache common-cache: remote put received. Element is: [ key = 2, value=2, version=1, hitCount=0, CreationTime = 1668502802000, LastAccessTime = 1668502802068 ]
2022-11-15 17:00:02.068 [RMI TCP Connection(4)-10.2.6.77] [39mDEBUG[0;39m [36mnet.sf.ehcache.distribution.RMICachePeer:180[0;39m - RMICachePeer for cache common-cache: remote put received. Element is: [ key = 3, value=3, version=1, hitCount=0, CreationTime = 1668502802000, LastAccessTime = 1668502802068 ]
2022-11-15 17:00:02.068 [RMI TCP Connection(4)-10.2.6.77] [39mDEBUG[0;39m [36mnet.sf.ehcache.distribution.RMICachePeer:180[0;39m - RMICachePeer for cache common-cache: remote put received. Element is: [ key = 4, value=4, version=1, hitCount=0, CreationTime = 1668502802000, LastAccessTime = 1668502802068 ]
2022-11-15 17:00:02.068 [RMI TCP Connection(4)-10.2.6.77] [39mDEBUG[0;39m [36mnet.sf.ehcache.distribution.RMICachePeer:180[0;39m - RMICachePeer for cache common-cache: remote put received. Element is: [ key = 5, value=5, version=1, hitCount=0, CreationTime = 1668502802000, LastAccessTime = 1668502802068 ]
2022-11-15 17:00:02.068 [RMI TCP Connection(4)-10.2.6.77] [39mDEBUG[0;39m [36mnet.sf.ehcache.distribution.RMICachePeer:180[0;39m - RMICachePeer for cache common-cache: remote put received. Element is: [ key = 6, value=6, version=1, hitCount=0, CreationTime = 1668502802000, LastAccessTime = 1668502802068 ]
2022-11-15 17:00:02.068 [pool-1-thread-1] [34mINFO [0;39m [36mcom.icfcc.fcit.dp.modules.msg.test.ShareData:57[0;39m - 获取缓存时间:2022-11-15T17:00:02.068 - key:6 -> value: 6
2022-11-15 17:00:02.069 [RMI TCP Connection(4)-10.2.6.77] [39mDEBUG[0;39m [36mnet.sf.ehcache.distribution.RMICachePeer:180[0;39m - RMICachePeer for cache common-cache: remote put received. Element is: [ key = 7, value=7, version=1, hitCount=0, CreationTime = 1668502802000, LastAccessTime = 1668502802068 ]
2022-11-15 17:00:02.069 [RMI TCP Connection(4)-10.2.6.77] [39mDEBUG[0;39m [36mnet.sf.ehcache.distribution.RMICachePeer:180[0;39m - RMICachePeer for cache common-cache: remote put received. Element is: [ key = 8, value=8, version=1, hitCount=0, CreationTime = 1668502802000, LastAccessTime = 1668502802069 ]
2022-11-15 17:00:02.069 [RMI TCP Connection(4)-10.2.6.77] [39mDEBUG[0;39m [36mnet.sf.ehcache.distribution.RMICachePeer:180[0;39m - RMICachePeer for cache common-cache: remote put received. Element is: [ key = 9, value=9, version=1, hitCount=0, CreationTime = 1668502802000, LastAccessTime = 1668502802069 ]
2022-11-15 17:00:02.069 [pool-1-thread-1] [34mINFO [0;39m [36mcom.icfcc.fcit.dp.modules.msg.test.ShareData:57[0;39m - 获取缓存时间:2022-11-15T17:00:02.069 - key:9 -> value: 9
2022-11-15 17:00:02.069 [RMI TCP Connection(4)-10.2.6.77] [39mDEBUG[0;39m [36mnet.sf.ehcache.distribution.RMICachePeer:180[0;39m - RMICachePeer for cache common-cache: remote put received. Element is: [ key = 10, value=10, version=1, hitCount=0, CreationTime = 1668502802000, LastAccessTime = 1668502802069 ]

...
缓存获取开始时间:2022-11-15T17:00:02.068-缓存获取结束时间:2022-11-15T17:00:05.033 用时:3 秒

6.测试结论

40000个元素放入缓存集群的一个节点,从另一个节点获取元素

总耗时(秒)添加一个元素平均耗时(秒)
添加40000个元素2.1540.00005385
同步40000个元素2.9650.000074125

注意:从第一元素放入观察,发现缓存同步时,第一次同步时耗时较长,耗时0.708

放入缓存时间:2022-11-15T17:00:01.360 - key:1 -> value: 1

获取缓存时间:2022-11-15T17:00:02.068 - key:1 -> value: 1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

独行客-编码爱好者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值