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