@@ -26,22 +26,35 @@ public void before() {
2626 redissonClient = Redisson .create (c );
2727 // 在每次测试前清空所有数据
2828 redissonClient .getKeys ().flushall ();
29+
30+ // 预热逻辑
31+ warmup ();
32+ }
33+
34+ private void warmup () {
35+ try {
36+ // 预热:执行一些基本操作让系统进入稳定状态
37+ CountDownLatch warmupLatch = new CountDownLatch (WARMUP_COUNT );
38+ for (int i = 0 ; i < WARMUP_COUNT ; i ++) {
39+ redissonClient .getBucket ("warmup" + i ).setAsync ("warmup" + i ).exceptionally (throwable -> {
40+ warmupLatch .countDown ();
41+ return null ;
42+ }).thenRun (warmupLatch ::countDown );
43+ }
44+ warmupLatch .await ();
45+
46+ // 清除预热数据
47+ redissonClient .getKeys ().flushall ();
48+ } catch (InterruptedException e ) {
49+ Thread .currentThread ().interrupt ();
50+ } catch (Exception e ) {
51+ // 忽略其他异常
52+ }
2953 }
3054
3155
3256 @ Test
3357 public void asyncSet () throws InterruptedException , ExecutionException {
34- // 预热
35- CountDownLatch warmupLatch = new CountDownLatch (WARMUP_COUNT );
36- for (int i = 0 ; i < WARMUP_COUNT ; i ++) {
37- redissonClient .getBucket ("warmup" + i ).setAsync ("warmup" + i ).exceptionally (throwable -> {
38- warmupLatch .countDown ();
39- return null ;
40- }).thenRun (warmupLatch ::countDown );
41- }
42- warmupLatch .await ();
43- redissonClient .getKeys ().flushall (); // 清除预热数据
44-
4558 CountDownLatch latch = new CountDownLatch (SET_COUNT );
4659 long start = System .currentTimeMillis ();
4760 for (int i = 0 ; i < SET_COUNT ; i ++) {
@@ -68,21 +81,6 @@ public void asyncSet() throws InterruptedException, ExecutionException {
6881
6982 @ Test
7083 public void asyncGet () throws InterruptedException {
71- // 预热:先设置预热数据
72- for (int i = 0 ; i < WARMUP_COUNT ; i ++) {
73- redissonClient .getBucket ("warmup" + i ).set ("warmup" + i );
74- }
75-
76- // 执行预热访问
77- CountDownLatch warmupLatch = new CountDownLatch (WARMUP_COUNT );
78- for (int i = 0 ; i < WARMUP_COUNT ; i ++) {
79- redissonClient .getBucket ("warmup" + (i % WARMUP_COUNT )).getAsync ().thenRun (warmupLatch ::countDown );
80- }
81- warmupLatch .await ();
82-
83- // 清除预热数据
84- redissonClient .getKeys ().flushall ();
85-
8684 // 先设置数据
8785 for (int i = 0 ; i < SET_COUNT ; i ++) {
8886 redissonClient .getBucket ("test" + i ).set ("test" + i );
@@ -123,17 +121,6 @@ public void asyncGet() throws InterruptedException {
123121
124122 @ Test
125123 public void concurrentSet () throws InterruptedException {
126- // 预热
127- CountDownLatch warmupLatch = new CountDownLatch (WARMUP_COUNT );
128- for (int i = 0 ; i < WARMUP_COUNT ; i ++) {
129- redissonClient .getBucket ("warmup" + i ).setAsync ("warmup" + i ).exceptionally (throwable -> {
130- warmupLatch .countDown ();
131- return null ;
132- }).thenRun (warmupLatch ::countDown );
133- }
134- warmupLatch .await ();
135- redissonClient .getKeys ().flushall (); // 清除预热数据
136-
137124 AtomicInteger successCount = new AtomicInteger (0 );
138125
139126 Thread [] threads = new Thread [CONCURRENT_CLIENT_COUNT ];
@@ -170,19 +157,6 @@ public void concurrentSet() throws InterruptedException {
170157
171158 @ Test
172159 public void concurrentGet () throws InterruptedException {
173- // 预热:先设置预热数据
174- for (int i = 0 ; i < WARMUP_COUNT ; i ++) {
175- redissonClient .getBucket ("warmup" + i ).set ("warmup" + i );
176- }
177-
178- // 执行预热访问
179- for (int i = 0 ; i < WARMUP_COUNT ; i ++) {
180- redissonClient .getBucket ("warmup" + (i % WARMUP_COUNT )).get ();
181- }
182-
183- // 清除预热数据
184- redissonClient .getKeys ().flushall ();
185-
186160 // 先设置数据
187161 for (int i = 0 ; i < SET_COUNT ; i ++) {
188162 redissonClient .getBucket ("test" + i ).set ("test" + i );
0 commit comments