Skip to content

Commit 898d53b

Browse files
yanhom1314claude
andcommitted
test: add unit tests for PerformanceProvider
Co-Authored-By: Claude <noreply@anthropic.com>
1 parent c804e4f commit 898d53b

1 file changed

Lines changed: 126 additions & 0 deletions

File tree

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.dromara.dynamictp.test.core.monitor;
19+
20+
import org.dromara.dynamictp.core.monitor.PerformanceProvider;
21+
import org.junit.jupiter.api.Test;
22+
23+
import static org.junit.jupiter.api.Assertions.assertEquals;
24+
import static org.junit.jupiter.api.Assertions.assertTrue;
25+
26+
/**
27+
* PerformanceProvider test
28+
*
29+
* @author yanhom
30+
* @since 1.2.2
31+
*/
32+
class PerformanceProviderTest {
33+
34+
@Test
35+
void testEmptySnapshot() {
36+
PerformanceProvider provider = new PerformanceProvider();
37+
PerformanceProvider.PerformanceSnapshot snapshot = provider.getSnapshotAndReset();
38+
39+
assertEquals(0.0, snapshot.getAvg(), 0.001);
40+
assertEquals(0, snapshot.getMaxRt());
41+
assertEquals(0, snapshot.getMinRt());
42+
}
43+
44+
@Test
45+
void testCompleteTaskUpdatesMetrics() {
46+
PerformanceProvider provider = new PerformanceProvider();
47+
48+
provider.completeTask(100);
49+
provider.completeTask(200);
50+
provider.completeTask(300);
51+
52+
PerformanceProvider.PerformanceSnapshot snapshot = provider.getSnapshotAndReset();
53+
54+
assertEquals(300, snapshot.getMaxRt());
55+
assertEquals(100, snapshot.getMinRt());
56+
assertEquals(200.0, snapshot.getAvg(), 0.001);
57+
}
58+
59+
@Test
60+
void testTpsCalculation() throws InterruptedException {
61+
PerformanceProvider provider = new PerformanceProvider();
62+
63+
// wait a bit to ensure non-zero interval
64+
Thread.sleep(1100);
65+
66+
provider.completeTask(50);
67+
provider.completeTask(50);
68+
provider.completeTask(50);
69+
70+
PerformanceProvider.PerformanceSnapshot snapshot = provider.getSnapshotAndReset();
71+
72+
// 3 tasks over ~1 second interval, tps should be around 3
73+
assertTrue(snapshot.getTps() >= 1.0, "TPS should be >= 1, was: " + snapshot.getTps());
74+
assertTrue(snapshot.getTps() <= 5.0, "TPS should be <= 5, was: " + snapshot.getTps());
75+
}
76+
77+
@Test
78+
void testResetAfterSnapshot() throws InterruptedException {
79+
PerformanceProvider provider = new PerformanceProvider();
80+
81+
provider.completeTask(100);
82+
provider.completeTask(200);
83+
84+
PerformanceProvider.PerformanceSnapshot first = provider.getSnapshotAndReset();
85+
assertEquals(200, first.getMaxRt());
86+
87+
// After reset, new snapshot should be empty
88+
Thread.sleep(100);
89+
PerformanceProvider.PerformanceSnapshot second = provider.getSnapshotAndReset();
90+
assertEquals(0, second.getMaxRt());
91+
assertEquals(0.0, second.getAvg(), 0.001);
92+
}
93+
94+
@Test
95+
void testPercentiles() {
96+
PerformanceProvider provider = new PerformanceProvider();
97+
98+
// Add enough samples for meaningful percentiles
99+
for (int i = 1; i <= 100; i++) {
100+
provider.completeTask(i);
101+
}
102+
103+
PerformanceProvider.PerformanceSnapshot snapshot = provider.getSnapshotAndReset();
104+
105+
assertEquals(100, snapshot.getMaxRt());
106+
assertEquals(1, snapshot.getMinRt());
107+
assertTrue(snapshot.getTp50() >= 40 && snapshot.getTp50() <= 60,
108+
"tp50 should be around 50, was: " + snapshot.getTp50());
109+
assertTrue(snapshot.getTp90() >= 80 && snapshot.getTp90() <= 100,
110+
"tp90 should be around 90, was: " + snapshot.getTp90());
111+
assertTrue(snapshot.getTp99() >= 95,
112+
"tp99 should be >= 95, was: " + snapshot.getTp99());
113+
}
114+
115+
@Test
116+
void testSingleTaskSnapshot() {
117+
PerformanceProvider provider = new PerformanceProvider();
118+
provider.completeTask(42);
119+
120+
PerformanceProvider.PerformanceSnapshot snapshot = provider.getSnapshotAndReset();
121+
122+
assertEquals(42, snapshot.getMaxRt());
123+
assertEquals(42, snapshot.getMinRt());
124+
assertEquals(42.0, snapshot.getAvg(), 0.001);
125+
}
126+
}

0 commit comments

Comments
 (0)