Skip to content

Commit f30a6f9

Browse files
author
吴就业
committed
fixes #5 #6
1 parent df12ce2 commit f30a6f9

File tree

5 files changed

+26
-16
lines changed

5 files changed

+26
-16
lines changed

src/main/java/com/wujiuye/flow/BaseFlower.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.wujiuye.flow.common.*;
44

5+
import java.math.BigDecimal;
56
import java.util.ArrayList;
67
import java.util.List;
78

@@ -62,8 +63,10 @@ public long exceptionAvg() {
6263
* @return
6364
*/
6465
@Override
65-
public long successAvg() {
66-
return metric.success() / getWindowInterval(metric.getWindowInterval());
66+
public float successAvg() {
67+
float total = metric.success();
68+
float windows = getWindowInterval(metric.getWindowInterval());
69+
return total / windows;
6770
}
6871

6972
/**

src/main/java/com/wujiuye/flow/Flower.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ default long total() {
6161
*
6262
* @return
6363
*/
64-
long successAvg();
64+
float successAvg();
6565

6666
/**
6767
* 平均耗时

src/main/java/com/wujiuye/flow/common/ArrayMetric.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,15 @@ public long rt() {
5656
public long minRt() {
5757
// 确保当前时间的bucket不为空
5858
data.currentWindow();
59-
long rt = 0;
59+
long rt = Long.MAX_VALUE;
6060
List<MetricBucket> list = data.values();
6161
for (MetricBucket window : list) {
62-
if (window.minRt() < rt || rt == 0) {
63-
rt = window.minRt();
62+
long wrt = window.minRt();
63+
if (wrt > 0 && wrt < rt) {
64+
rt = wrt;
6465
}
6566
}
66-
return Math.max(1, rt);
67+
return rt == Long.MAX_VALUE ? 0 : rt;
6768
}
6869

6970
@Override
@@ -77,7 +78,7 @@ public long maxRt() {
7778
rt = window.maxRt();
7879
}
7980
}
80-
return Math.max(1, rt);
81+
return rt;
8182
}
8283

8384
@Override

src/main/java/com/wujiuye/flow/common/MetricBucket.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ private void add(MetricEvent event, long n) {
4040
}
4141

4242
public void reset() {
43+
minRt = Long.MAX_VALUE;
44+
maxRt = Long.MIN_VALUE;
4345
for (MetricEvent event : MetricEvent.values()) {
4446
counters[event.ordinal()].reset();
4547
}

src/test/java/com/wujiuye/flow/FlowTest.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ public static void main(String[] args) throws InterruptedException {
1717
// 注册需要收集指标数据的Flower
1818
MetricPersistencer.registerFlower("test-resource", flowHelper.getFlow(FlowType.Minute));
1919
new Thread(() -> {
20+
int i = 0;
2021
while (!Thread.interrupted()) {
22+
if (i++ >= 100) {
23+
break;
24+
}
2125
try {
2226
Thread.sleep(1);
2327
flowHelper.incrSuccess(1);
@@ -38,23 +42,23 @@ public static void main(String[] args) throws InterruptedException {
3842
System.out.println("最小请求耗时:" + flower.minRt());
3943
System.out.println("平均请求成功数:" + flower.successAvg());
4044
System.out.println("平均请求异常数:" + flower.exceptionAvg());
41-
try {
42-
Thread.sleep(10000);
43-
} catch (InterruptedException e) {
44-
e.printStackTrace();
45-
}
4645
System.out.println();
4746
List<WindowWrap<MetricBucket>> buckets = flower.windows();
4847
for (WindowWrap<MetricBucket> bucket : buckets) {
4948
System.out.print("开始时间戳:" + bucket.windowStart() + "\t");
5049
System.out.print("成功数:" + bucket.value().success() + "\t");
5150
System.out.print("失败数:" + bucket.value().exception() + "\t");
52-
System.out.print("平均耗时:" + (bucket.value().rt() / bucket.value().success()) + "\t");
53-
System.out.print("最大数:" + bucket.value().maxRt() + "\t");
54-
System.out.print("最小数:" + bucket.value().minRt() + "\t");
51+
System.out.print("平均耗时:" + (bucket.value().success() > 0 ? (bucket.value().rt() / bucket.value().success()) : 0) + "\t");
52+
System.out.print("最大耗时:" + bucket.value().maxRt() + "\t");
53+
System.out.print("最小耗时:" + bucket.value().minRt() + "\t");
5554
System.out.println();
5655
}
5756
System.out.println("======================================================");
57+
try {
58+
Thread.sleep(1000 * 60);
59+
} catch (InterruptedException e) {
60+
e.printStackTrace();
61+
}
5862
}
5963
}).start();
6064
Thread.sleep(Integer.MAX_VALUE);

0 commit comments

Comments
 (0)