/
RequestExecutionTimeGaugeImpl.java
301 lines (268 loc) · 8.59 KB
/
RequestExecutionTimeGaugeImpl.java
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package org.dcache.commons.stats;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;
import java.lang.management.ManagementFactory;
import java.util.Formatter;
import java.util.concurrent.TimeUnit;
/**
* this class stores an average of the execution time of the request
* if the num is the num of updates that took place before this update
* then the next average is caclulated using the formula:
* newaverage =
* (previousaverage*num +nextmeasurment) /(num+1);
* there is a utility method to read an average(mean), max, mean, RMS, standard deviation
* and error on mean.
* Separate average is kept for feeding into the rrd database.
* This average is reset to the value of the last measurment
* when it is read and the new average is calculated when new updates come
*
* @author timur
*/
public class RequestExecutionTimeGaugeImpl implements RequestExecutionTimeGaugeMXBean {
private static final Logger LOG = LoggerFactory.getLogger(RequestExecutionTimeGaugeImpl.class);
private final String name;
// These are the variables that keep the
// the average for the duration of the existance of the
// gauge
/**
* average
*/
private long averageExecutionTime=0;
/**
* Mininum
*/
private long minExecutionTime=0;
/**
* Maximum
*/
private long maxExecutionTime=0;
/**
* Square of the RMS (Root Mean Square)
* sum(value_i)/n
* RMSS(i+1)=(RMSS(i)+value(i+1)**2)/(i+1)
*/
private long executionTimeRMSS=0;
/**
* number of updates
*/
private int updateNum=0;
/**
* last value fed to the gauge
*/
private long lastExecutionTime=0;
private final long startTime;
// These are the variables that are reset every time
// getAndResetAverageExecutionTime is called
// for feeding into the RRD Database
/**
* TimeStamp of the beginning of the measurment period
*/
private long periodStartTime;
private long periodAverageExecutionTime=0;
private long periodUpdateNum=0;
/**
*
* @param name
*/
public RequestExecutionTimeGaugeImpl(String name, String family) {
this.name = name;
String mxName = String.format("%s:type=RequestExecutionTimeGauge,family=%s,name=%s",
this.getClass().getPackage().getName(), family, this.name);
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
try {
ObjectName mxBeanName = new ObjectName(mxName);
if (!server.isRegistered(mxBeanName)) {
server.registerMBean(this, mxBeanName);
}
} catch (MalformedObjectNameException ex) {
LOG.warn("Failed to create a MXBean with name: {} : {}" , mxName, ex.toString());
} catch (InstanceAlreadyExistsException | MBeanRegistrationException ex) {
LOG.warn("Failed to register a MXBean: {}", ex.toString());
} catch (NotCompliantMBeanException ex) {
LOG.warn("Failed to create a MXBean: {}", ex.toString());
}
startTime = System.currentTimeMillis();
periodStartTime = startTime;
}
/**
*
* @param nextExecTime
*/
@Override
public synchronized void update(long nextExecTime) {
if(nextExecTime <0) {
LOG.info("possible backwards timeshift detected; discarding invalid data ({})",
nextExecTime);
return;
}
// long term averages calculations
if( updateNum==0 ) {
averageExecutionTime = nextExecTime;
minExecutionTime = nextExecTime;
maxExecutionTime = nextExecTime;
executionTimeRMSS=nextExecTime*nextExecTime;
} else {
averageExecutionTime =
(averageExecutionTime*updateNum +nextExecTime) /(updateNum+1);
minExecutionTime = getMinExecutionTime() <nextExecTime?
getMinExecutionTime():nextExecTime;
maxExecutionTime = getMaxExecutionTime()>nextExecTime?
getMaxExecutionTime():nextExecTime;
executionTimeRMSS=
(executionTimeRMSS*updateNum+nextExecTime*nextExecTime)/
(updateNum+1);
}
updateNum++;
// period averages caclucations
periodAverageExecutionTime =
(periodAverageExecutionTime*periodUpdateNum +nextExecTime) /
(periodUpdateNum+1);
periodUpdateNum++;
lastExecutionTime = nextExecTime;
}
/**
* return average over the lifetime of the gauge
* @return
*/
@Override
public synchronized long getAverageExecutionTime() {
return averageExecutionTime;
}
/**
* return average over the last period, and start new period
* @return
*/
@Override
public synchronized long resetAndGetAverageExecutionTime() {
long periodAverageExecutionTime = this.periodAverageExecutionTime;
periodUpdateNum = 0;
this.periodAverageExecutionTime = lastExecutionTime;
periodStartTime = System.currentTimeMillis();
return periodAverageExecutionTime;
}
/**
*
* @return String representation of this RequestExecutionTimeGauge
* Only long term statistics is printed
*/
@Override
public synchronized String toString() {
String aName = name;
if(name.length() >34) {
aName = aName.substring(0,34);
}
long updatePeriod= System.currentTimeMillis() -
startTime;
StringBuilder sb = new StringBuilder();
try (Formatter formatter = new Formatter(sb)) {
formatter.format("%-34s %12d\u00B1%10f %,12d %,12d %,12d %,12d %,12d",
aName, averageExecutionTime,getStandardError(),
minExecutionTime,maxExecutionTime,
getStandardDeviation(), updateNum,
TimeUnit.MILLISECONDS.toSeconds(updatePeriod));
}
return sb.toString();
}
/**
* @return the minExecutionTime
*/
@Override
public synchronized long getMinExecutionTime() {
return minExecutionTime;
}
/**
* @return the maxExecutionTime
*/
@Override
public synchronized long getMaxExecutionTime() {
return maxExecutionTime;
}
/**
* @return the name
*/
@Override
public String getName() {
return name;
}
/**
* @return the RMS of executionTime
*/
@Override
public synchronized double getExecutionTimeRMS() {
return Math.sqrt(executionTimeRMSS);
}
@Override
public synchronized long getStandardDeviation() {
long deviationSquare = executionTimeRMSS - averageExecutionTime*averageExecutionTime;
assert (deviationSquare >=0);
return (long) Math.sqrt(executionTimeRMSS - averageExecutionTime*averageExecutionTime);
}
/**
*
* @return standard error of the mean
*/
@Override
public synchronized double getStandardError() {
return getStandardDeviation() / Math.sqrt(updateNum);
}
/**
* @return the updateNum
*/
@Override
public synchronized int getUpdateNum() {
return updateNum;
}
/**
* @return the lastExecutionTime
*/
@Override
public synchronized long getLastExecutionTime() {
return lastExecutionTime;
}
/**
* @return the startTime
*/
@Override
public synchronized long getStartTime() {
return startTime;
}
/**
* @return the periodStartTime
*/
@Override
public synchronized long getPeriodStartTime() {
return periodStartTime;
}
/**
* @return the periodAverageExecutionTime
*/
@Override
public synchronized long getPeriodAverageExecutionTime() {
return periodAverageExecutionTime;
}
/**
* @return the periodUpdateNum
*/
@Override
public synchronized long getPeriodUpdateNum() {
return periodUpdateNum;
}
@Override
public synchronized void reset() {
periodStartTime = System.currentTimeMillis();
periodAverageExecutionTime = 0;
periodUpdateNum = 0;
averageExecutionTime=0;
minExecutionTime=0;
maxExecutionTime=0;
executionTimeRMSS=0;
lastExecutionTime = 0;
updateNum = 0;
}
}