Skip to content

Commit 5ea129f

Browse files
committed
refactor(benchmark): 重构预热逻辑并移除重复代码
- 将预热逻辑提取到独立的 warmup() 方法中 - 移除了各测试方法中的重复预热代码- 添加了对 InterruptedException 的处理 - 统一了 Redisson 和 Redisun 基准测试的预热实现
1 parent dfa73d3 commit 5ea129f

2 files changed

Lines changed: 43 additions & 96 deletions

File tree

src/test/java/tech/smartboot/redisun/bench/RedissonBenchmark.java

Lines changed: 24 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

src/test/java/tech/smartboot/redisun/bench/RedisunBenchmark.java

Lines changed: 19 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,30 @@ public void before() {
2222
});
2323
// 在每次测试前清空所有数据
2424
redisun.flushAll();
25+
26+
// 预热逻辑
27+
warmup();
28+
}
29+
30+
private void warmup() {
31+
try {
32+
// 预热:执行一些基本操作让系统进入稳定状态
33+
CountDownLatch warmupLatch = new CountDownLatch(WARMUP_COUNT);
34+
for (int i = 0; i < WARMUP_COUNT; i++) {
35+
redisun.asyncSet("warmup" + i, "warmup" + i).thenRun(warmupLatch::countDown);
36+
}
37+
warmupLatch.await();
38+
39+
// 清除预热数据
40+
redisun.flushAll();
41+
} catch (InterruptedException e) {
42+
Thread.currentThread().interrupt();
43+
}
2544
}
2645

2746

2847
@Test
2948
public void asyncSet() throws InterruptedException {
30-
// 预热
31-
CountDownLatch warmupLatch = new CountDownLatch(WARMUP_COUNT);
32-
for (int i = 0; i < WARMUP_COUNT; i++) {
33-
redisun.asyncSet("warmup" + i, "warmup" + i).thenRun(warmupLatch::countDown);
34-
}
35-
warmupLatch.await();
36-
redisun.flushAll(); // 清除预热数据
37-
3849
// 先获取初始键数量
3950
long initialSize = redisun.dbsize();
4051

@@ -61,21 +72,6 @@ public void asyncSet() throws InterruptedException {
6172

6273
@Test
6374
public void asyncGet() throws InterruptedException {
64-
// 预热:先设置预热数据
65-
for (int i = 0; i < WARMUP_COUNT; i++) {
66-
redisun.set("warmup" + i, "warmup" + i);
67-
}
68-
69-
// 执行预热访问
70-
CountDownLatch warmupLatch = new CountDownLatch(WARMUP_COUNT);
71-
for (int i = 0; i < WARMUP_COUNT; i++) {
72-
redisun.asyncGet("warmup" + (i % WARMUP_COUNT)).thenRun(warmupLatch::countDown);
73-
}
74-
warmupLatch.await();
75-
76-
// 清除预热数据
77-
redisun.flushAll();
78-
7975
// 先设置数据
8076
for (int i = 0; i < SET_COUNT; i++) {
8177
redisun.set("test" + i, "test" + i);
@@ -117,14 +113,6 @@ public void asyncGet() throws InterruptedException {
117113

118114
@Test
119115
public void concurrentSet() throws InterruptedException {
120-
// 预热
121-
CountDownLatch warmupLatch = new CountDownLatch(WARMUP_COUNT);
122-
for (int i = 0; i < WARMUP_COUNT; i++) {
123-
redisun.asyncSet("warmup" + i, "warmup" + i).thenRun(warmupLatch::countDown);
124-
}
125-
warmupLatch.await();
126-
redisun.flushAll(); // 清除预热数据
127-
128116
AtomicInteger successCount = new AtomicInteger(0);
129117

130118
Thread[] threads = new Thread[CONCURRENT_CLIENT_COUNT];
@@ -161,21 +149,6 @@ public void concurrentSet() throws InterruptedException {
161149

162150
@Test
163151
public void concurrentGet() throws InterruptedException {
164-
// 预热:先设置预热数据
165-
for (int i = 0; i < WARMUP_COUNT; i++) {
166-
redisun.set("warmup" + i, "warmup" + i);
167-
}
168-
169-
// 执行预热访问
170-
CountDownLatch warmupLatch = new CountDownLatch(WARMUP_COUNT);
171-
for (int i = 0; i < WARMUP_COUNT; i++) {
172-
redisun.asyncGet("warmup" + (i % WARMUP_COUNT)).thenRun(warmupLatch::countDown);
173-
}
174-
warmupLatch.await();
175-
176-
// 清除预热数据
177-
redisun.flushAll();
178-
179152
// 先设置数据
180153
for (int i = 0; i < SET_COUNT; i++) {
181154
redisun.set("test" + i, "test" + i);

0 commit comments

Comments
 (0)