KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > clif > datacollector > lib > InjectorDataCollector


1 /*
2 * CLIF is a Load Injection Framework
3 * Copyright (C) 2004 France Telecom R&D
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * CLIF $Name: $
20 *
21 * Contact: clif@objectweb.org
22 */

23
24 package org.objectweb.clif.datacollector.lib;
25
26
27 import org.objectweb.clif.storage.api.ActionEvent;
28 import java.io.Serializable JavaDoc;
29
30
31 /**
32  * Data collector for load-injectors generating ActionEvent events.
33  * statistics:
34  * - time frame, i.e. ellapsed time since previous getStat() call
35  * - average reponse time during this time frame
36  * - max response time during this time frame
37  * - min response time during this time frame
38  * - number of actions during this time frame
39  * - throughput (number of actions/sec) during this time frame
40  * - number of error occurrences during this time frame
41  * - error rate (number of error/sec) during this time frame
42  *
43  * @author Bruno Dillenseger
44  */

45 public class InjectorDataCollector extends AbstractDataCollector
46 {
47     static public final String JavaDoc[] LABELS = {
48         "time frame (ms)",
49         "average response time (ms)",
50         "min response time (ms)",
51         "max response time (ms)",
52         "number of actions",
53         "action throughput (actions/s)",
54         "number of errors",
55         "error throughput (errors/s)",
56         "error rate (%)" };
57
58
59     /** absolute date of previous call to getStat() */
60     protected long statStartTime;
61     /** number of successfull actions performed since the previous call to getStat() */
62     protected long successfullActions;
63     /** number of error occurrences since the previous call to getStat() */
64     protected long errors;
65     /** duration of the quickest action since the previous call to getStat() */
66     protected long actionMinDuration;
67     /** duration of the longest action since the previous call to getStat() */
68     protected long actionMaxDuration;
69     /** total amount of time spent by actions since the previous call to getStat() */
70     protected long actionTotalDuration;
71     /** total number of actions performed since current test initialization */
72     protected long cumulativeSuccessfullActions;
73     /** total number of error occurrences since current test initialization */
74     protected long cumulativeErrors;
75
76
77     public InjectorDataCollector()
78     {
79         super();
80     }
81
82
83     /**
84      * resets all statistics but the cumulative ones
85      */

86     private void resetStats()
87     {
88         statStartTime = System.currentTimeMillis();
89         successfullActions = 0;
90         errors = 0;
91         actionMinDuration = Long.MAX_VALUE;
92         actionMaxDuration = Long.MIN_VALUE;
93         actionTotalDuration = 0;
94     }
95
96
97     /**
98      * new test initialization => call super class' init() method and resets all statistics,
99      * including the cumulative ones
100      */

101     public void init(Serializable JavaDoc testId, String JavaDoc scenarioId)
102     {
103         super.init(testId, scenarioId);
104         resetStats();
105         cumulativeSuccessfullActions = 0;
106         cumulativeErrors = 0;
107     }
108
109
110     /**
111      * new measure available => call super class' add() method and compute statistics
112      */

113     public void add(ActionEvent event)
114     {
115         super.add(event);
116         if (event.isSuccessful())
117         {
118             ++successfullActions;
119             actionTotalDuration += event.duration;
120             if (event.duration > actionMaxDuration)
121             {
122                 actionMaxDuration = event.duration;
123             }
124             if (event.duration < actionMinDuration)
125             {
126                 actionMinDuration = event.duration;
127             }
128         }
129         else
130         {
131             ++errors;
132         }
133     }
134
135
136     //////////////////////////////////
137
// interface DataCollectorAdmin //
138
//////////////////////////////////
139

140
141     /**
142      * Get statistics computed since previous call (or initialization for 1st call).
143      * @return injector action statistics
144      */

145     public long[] getStat()
146     {
147         long[] stats = new long[LABELS.length];
148         // ellapsed time since previous call
149
stats[0] = System.currentTimeMillis() - statStartTime;
150         // min, max and average response times, error rate
151
if (successfullActions == 0)
152         {
153             // when no action has been performed, these statistics are set to zero
154
stats[1] = 0; // average response time
155
stats[2] = 0; // min response time
156
stats[3] = 0; // max response time
157
if (errors == 0) // error rate special case
158
{
159                 stats[8] = 0;
160             }
161             else
162             {
163                 stats[8] = 100;
164             }
165         }
166         else
167         {
168             stats[1] = actionTotalDuration / successfullActions; // average response time
169
stats[2] = actionMinDuration; // min response time
170
stats[3] = actionMaxDuration; // max response time
171
stats[8] = (errors * 100) / (successfullActions + errors); // error rate
172
}
173         cumulativeSuccessfullActions += successfullActions;
174         stats[4] = cumulativeSuccessfullActions;
175         // action throughput /s
176
stats[5] = (successfullActions * 1000) / stats[0];
177         cumulativeErrors += errors;
178         stats[6] = cumulativeErrors;
179         // error throughput /s
180
stats[7] = (errors * 1000) / stats[0];
181         resetStats();
182         return stats;
183     }
184
185
186     public String JavaDoc[] getLabels()
187     {
188         return LABELS;
189     }
190 }
191
Popular Tags