1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.util.counters.impl; |
12 | |
|
13 | |
import org.mule.util.counters.CounterFactory.Type; |
14 | |
|
15 | |
public class TimeAverage extends AggregateCounter |
16 | |
{ |
17 | |
|
18 | 0 | private double sum = 0.0; |
19 | 0 | private double lastValue = 0.0; |
20 | 0 | private final long firstTime = System.currentTimeMillis(); |
21 | 0 | private long lastTime = firstTime; |
22 | |
|
23 | |
public TimeAverage(String name, AbstractCounter base) |
24 | |
{ |
25 | 0 | super(name, Type.AVERAGE, base); |
26 | 0 | } |
27 | |
|
28 | |
public double nextValue() |
29 | |
{ |
30 | 0 | long current = System.currentTimeMillis(); |
31 | 0 | return (sum + lastValue * (current - this.lastTime)) / (current - firstTime); |
32 | |
} |
33 | |
|
34 | |
public void doCompute() |
35 | |
{ |
36 | 0 | long current = System.currentTimeMillis(); |
37 | 0 | this.sum += this.lastValue * (current - this.lastTime); |
38 | 0 | this.lastValue = getBase().nextValue(); |
39 | 0 | this.lastTime = current; |
40 | 0 | } |
41 | |
|
42 | |
} |