TransactionStatisticsComputer.java
3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package com.dianping.cat.consumer.transaction;
import java.util.Comparator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import com.dianping.cat.consumer.transaction.model.entity.AllDuration;
import com.dianping.cat.consumer.transaction.model.entity.Range;
import com.dianping.cat.consumer.transaction.model.entity.TransactionName;
import com.dianping.cat.consumer.transaction.model.entity.TransactionType;
import com.dianping.cat.consumer.transaction.model.transform.BaseVisitor;
public class TransactionStatisticsComputer extends BaseVisitor {
public double m_duration = 3600;
private double computeLineValue(Map<Integer, AllDuration> durations, double percent) {
int totalCount = 0;
Map<Integer, AllDuration> sorted = new TreeMap<Integer, AllDuration>(TransactionComparator.DESC);
sorted.putAll(durations);
for (AllDuration duration : durations.values()) {
totalCount += duration.getCount();
}
int remaining = (int) (totalCount * (100 - percent) / 100);
for (Entry<Integer, AllDuration> entry : sorted.entrySet()) {
remaining -= entry.getValue().getCount();
if (remaining <= 0) {
return entry.getKey();
}
}
return 0.0;
}
public TransactionStatisticsComputer setDuration(double duration) {
m_duration = duration;
return this;
}
double std(long count, double avg, double sum2, double max) {
double value = sum2 / count - avg * avg;
if (value <= 0 || count <= 1) {
return 0;
} else if (count == 2) {
return max - avg;
} else {
return Math.sqrt(value);
}
}
@Override
public void visitName(TransactionName name) {
super.visitName(name);
long count = name.getTotalCount();
if (count > 0) {
long failCount = name.getFailCount();
double avg = name.getSum() / count;
double std = std(count, avg, name.getSum2(), name.getMax());
double failPercent = 100.0 * failCount / count;
name.setFailPercent(failPercent);
name.setAvg(avg);
name.setStd(std);
double line95 = computeLineValue(name.getAllDurations(), 95);
double line999 = computeLineValue(name.getAllDurations(), 99.9);
name.setLine95Value(line95);
name.setLine99Value(line999);
}
if (m_duration > 0) {
name.setTps(name.getTotalCount() * 1.0 / m_duration);
}
}
@Override
public void visitRange(Range range) {
if (range.getCount() > 0) {
range.setAvg(range.getSum() / range.getCount());
}
}
@Override
public void visitType(TransactionType type) {
super.visitType(type);
long count = type.getTotalCount();
if (count > 0) {
long failCount = type.getFailCount();
double avg = type.getSum() / count;
double std = std(count, avg, type.getSum2(), type.getMax());
double failPercent = 100.0 * failCount / count;
type.setFailPercent(failPercent);
type.setAvg(avg);
type.setStd(std);
double line95 = computeLineValue(type.getAllDurations(), 95);
double line999 = computeLineValue(type.getAllDurations(), 99.9);
type.setLine95Value(line95);
type.setLine99Value(line999);
if (m_duration > 0) {
type.setTps(type.getTotalCount() * 1.0 / m_duration);
}
}
}
private static enum TransactionComparator implements Comparator<Integer> {
DESC {
@Override
public int compare(Integer o1, Integer o2) {
return o2 - o1;
}
}
}
}