1 package net.sourceforge.basher.impl;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.List;
6
7 import net.sourceforge.basher.*;
8 import net.sourceforge.basher.events.*;
9 import net.sourceforge.basher.internal.TimeSource;
10 import org.apache.commons.logging.Log;
11 import org.ops4j.gaderian.events.RegistryShutdownListener;
12
13
14
15
16
17 public abstract class AbstractCollector implements Collector, BasherEventListener, RegistryShutdownListener
18 {
19 private long _successes;
20 private long _failures;
21 private long _notRun;
22
23 private long _min = 0;
24 private long _max;
25 private long _total;
26 private long _timeElapsed = System.currentTimeMillis();
27
28 private List<Average> _averages = new ArrayList<Average>();
29 private boolean _collecting;
30
31 protected TimeSource _timeSource;
32 protected Log _log;
33
34 private int _runningThreads;
35
36
37 public void startCollecting()
38 {
39 if (_collecting)
40 {
41 _log.debug("Collecting, will not start collecting again");
42 return;
43 }
44 _log.info("Starting collection");
45 _collecting = true;
46 }
47
48 public void stopCollecting()
49 {
50 if (!_collecting)
51 {
52 _log.debug("Not collecting, will not stop collecting again");
53 return;
54 }
55 _log.info("Stopping collection");
56
57 _collecting = false;
58 try
59 {
60
61 closeOpenResources();
62 _log.info("Collection stopped");
63
64 }
65 catch (Exception e)
66 {
67 _log.error("Error received while closing resources: " + e.getMessage(), e);
68 }
69 }
70
71 protected abstract void initializeCollector(final BasherContext basherContext) throws Exception;
72 protected abstract void closeOpenResources();
73
74 public void success( final TaskExecutionContext taskExecutionContext, final long elapsedTime, final long elapsedTimeNanos )
75 {
76 _successes++;
77 updateStats(elapsedTime);
78 }
79
80 public void notRun( final TaskExecutionContext taskExecutionContext, final long elapsedTime, final long elapsedTimeNanos )
81 {
82 _notRun++;
83
84 }
85
86 public void fail( final TaskExecutionContext taskExecutionContext, final long elapsedTime, final long elapsedTimeNanos, final Throwable throwable )
87 {
88 _failures++;
89 updateStats(elapsedTime);
90 }
91
92 private void updateStats(final long elapsedTime)
93 {
94 _total += elapsedTime;
95 if (elapsedTime < _min)
96 {
97 _min = elapsedTime;
98 }
99 if (elapsedTime > _max)
100 {
101 _max = elapsedTime;
102 }
103 }
104
105 public long getFailures()
106 {
107 return _failures;
108 }
109
110 public long getSuccesses()
111 {
112 return _successes;
113 }
114
115 public long getNotRun()
116 {
117 return _notRun;
118 }
119
120 public long getTotal()
121 {
122 return _failures + _successes;
123 }
124
125 public List<Average> getAverages()
126 {
127 return Collections.unmodifiableList(_averages);
128 }
129
130 public Average markAverage()
131 {
132
133 final Average average = new Average(_min, _max, _total, (System.currentTimeMillis() - _timeElapsed), _successes, _failures,_notRun, _runningThreads);
134 if (isCollecting())
135 {
136 _averages.add(average);
137 }
138 _min = Long.MAX_VALUE;
139 _max = 0;
140 _total = 0;
141 _successes = 0;
142 _failures = 0;
143 _notRun = 0;
144 _timeElapsed = System.currentTimeMillis();
145
146 return average;
147 }
148
149 public void setTimeSource(final TimeSource timeSource)
150 {
151 _timeSource = timeSource;
152 }
153
154 public void setLog(final Log log)
155 {
156 _log = log;
157 }
158
159 public boolean isCollecting()
160 {
161 return _collecting;
162 }
163
164 public void basherEvent(final BasherEvent basherEvent)
165 {
166 if (basherEvent instanceof PhaseTransitionEvent)
167 {
168 final PhaseTransitionEvent phaseTransitionEvent = (PhaseTransitionEvent) basherEvent;
169 final Phase newPhase = phaseTransitionEvent.getNewPhase();
170 switch (newPhase)
171 {
172 case RUN:
173 try
174 {
175 final BasherContext basherContext = phaseTransitionEvent.getBasherContext();
176 initializeCollector(basherContext);
177 _runningThreads = basherContext.getInitialNumberThreads();
178
179 }
180 catch (Exception e)
181 {
182 _log.error("Unable to initialize collector for run: " + e.getMessage(), e);
183 }
184
185 break;
186 case COOLDOWN:
187 case END:
188
189 stopCollecting();
190 break;
191 default:
192 }
193
194 }
195 else if (basherEvent instanceof CollectionStartedEvent)
196 {
197 startCollecting();
198 }
199 else if (basherEvent instanceof CollectionStoppedEvent)
200 {
201 stopCollecting();
202 }
203 else if (basherEvent instanceof ThreadAddedEvent)
204 {
205 _runningThreads++;
206 }
207 else if (basherEvent instanceof ThreadRemovedEvent)
208 {
209 _runningThreads--;
210 }
211 else if (basherEvent instanceof TickEvent)
212 {
213 Average average = markAverage();
214 }
215 }
216
217 public void registryDidShutdown()
218 {
219 stopCollecting();
220 }
221 }