1.测试环境
CPU核心数 | 内存 | 主机地址 | |
---|---|---|---|
Server1 | 8 | 16G | 10.2.6.77 |
Server2 | 8 | 16G | 10.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.154 | 0.00005385 |
同步40000个元素 | 2.965 | 0.000074125 |
注意:从第一元素放入观察,发现缓存同步时,第一次同步时耗时较长,耗时0.708
放入缓存时间:2022-11-15T17:00:01.360 - key:1 -> value: 1
获取缓存时间:2022-11-15T17:00:02.068 - key:1 -> value: 1