1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.util.counters.impl; |
8 | |
|
9 | |
import org.mule.util.counters.CounterFactory.Type; |
10 | |
|
11 | |
import java.security.InvalidParameterException; |
12 | |
import java.util.Iterator; |
13 | |
import java.util.LinkedList; |
14 | |
|
15 | |
public class RatePerUnit extends AggregateCounter |
16 | |
{ |
17 | |
|
18 | 0 | private static class Sample |
19 | |
{ |
20 | |
private double value; |
21 | |
private long time; |
22 | |
|
23 | |
public Sample(double value, long time) |
24 | 0 | { |
25 | 0 | this.value = value; |
26 | 0 | this.time = time; |
27 | 0 | } |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
public long getTime() |
33 | |
{ |
34 | 0 | return time; |
35 | |
} |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
public double getValue() |
41 | |
{ |
42 | 0 | return value; |
43 | |
} |
44 | |
} |
45 | |
|
46 | |
private final LinkedList samples; |
47 | |
private final long unit; |
48 | |
private final long length; |
49 | |
private final long baseTime; |
50 | |
|
51 | |
public RatePerUnit(String name, String p, Type type, AbstractCounter base) |
52 | |
{ |
53 | 0 | super(name, type, base); |
54 | |
|
55 | 0 | if (type == Type.RATE_PER_SECOND) |
56 | |
{ |
57 | 0 | unit = 1000; |
58 | |
} |
59 | 0 | else if (type == Type.RATE_PER_MINUTE) |
60 | |
{ |
61 | 0 | unit = 60 * 1000; |
62 | |
} |
63 | 0 | else if (type == Type.RATE_PER_HOUR) |
64 | |
{ |
65 | 0 | unit = 60 * 60 * 1000; |
66 | |
} |
67 | |
else |
68 | |
{ |
69 | 0 | throw new InvalidParameterException(); |
70 | |
} |
71 | |
|
72 | 0 | long newLength = 0; |
73 | |
|
74 | |
try |
75 | |
{ |
76 | 0 | newLength = Long.parseLong(p); |
77 | |
} |
78 | 0 | catch (Exception e) |
79 | |
{ |
80 | 0 | newLength = 0; |
81 | |
} |
82 | |
finally |
83 | |
{ |
84 | 0 | if (newLength <= 0) |
85 | |
{ |
86 | 0 | newLength = 128; |
87 | |
} |
88 | |
|
89 | 0 | length = newLength; |
90 | 0 | } |
91 | |
|
92 | 0 | samples = new LinkedList(); |
93 | 0 | baseTime = System.currentTimeMillis(); |
94 | 0 | } |
95 | |
|
96 | |
public double nextValue() |
97 | |
{ |
98 | 0 | if (samples.isEmpty()) |
99 | |
{ |
100 | 0 | return 0.0; |
101 | |
} |
102 | |
else |
103 | |
{ |
104 | 0 | double total = 0.0; |
105 | 0 | long current = getTime(); |
106 | 0 | Iterator it = samples.iterator(); |
107 | 0 | Sample sample = null; |
108 | 0 | while (it.hasNext()) |
109 | |
{ |
110 | 0 | sample = (Sample) it.next(); |
111 | 0 | if (current - sample.time > length) |
112 | |
{ |
113 | 0 | break; |
114 | |
} |
115 | 0 | total += sample.value; |
116 | |
} |
117 | 0 | return total / (1 + current - (sample != null ? sample.time : 0)); |
118 | |
} |
119 | |
} |
120 | |
|
121 | |
public void doCompute() |
122 | |
{ |
123 | 0 | Sample l = samples.isEmpty() ? null : (Sample) samples.getFirst(); |
124 | 0 | long t = getTime(); |
125 | 0 | if (l == null || t > l.time) |
126 | |
{ |
127 | 0 | Sample s = new Sample(this.getBase().nextValue(), t); |
128 | 0 | samples.addFirst(s); |
129 | 0 | } |
130 | |
else |
131 | |
{ |
132 | 0 | l.value += getBase().nextValue(); |
133 | |
} |
134 | 0 | while (samples.size() > length) |
135 | |
{ |
136 | 0 | samples.removeLast(); |
137 | |
} |
138 | 0 | } |
139 | |
|
140 | |
protected long getTime() |
141 | |
{ |
142 | 0 | return (System.currentTimeMillis() - baseTime) / unit; |
143 | |
} |
144 | |
|
145 | |
} |