View Javadoc

1   /*
2    * Copyright (c) 2004, RV Test Team
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    *
8    * Redistributions of source code must retain the above copyright notice, this
9    * list of conditions and the following disclaimer.
10   *
11   * Redistributions in binary form must reproduce the above copyright notice,
12   * this list of conditions and the following disclaimer in the documentation
13   * and/or other materials provided with the distribution.
14   *
15   * Neither the name of the "RV Test Team" nor the names of its contributors may
16   * be used to endorse or promote products derived from this software without
17   * specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29   * THE POSSIBILITY OF SUCH DAMAGE.
30   *
31   */
32  /*
33   * Copyright (c) 2002, Reuters
34   * All rights reserved.
35   *
36   * Redistribution and use in source and binary forms, with or without
37   * modification, are permitted provided that the following conditions are met:
38   *
39   * Redistributions of source code must retain the above copyright notice, this
40   * list of conditions and the following disclaimer.
41   * Redistributions in binary form must reproduce the above copyright notice,
42   * this list of conditions and the following disclaimer in the documentation
43   * and/or other materials provided with the distribution.
44   * Neither the name of the Reuters nor the names of its contributors may be
45   * used to endorse or promote products derived from this software without
46   * specific prior written permission.
47   *
48   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
49   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
52   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
53   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
54   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
55   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
56   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
57   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
58   * THE POSSIBILITY OF SUCH DAMAGE.
59   *
60   */
61  package com.reuters.msgtest.load.output;
62  
63  import com.reuters.msgtest.load.config.LoadTestConfig;
64  import org.apache.commons.logging.Log;
65  import org.apache.commons.logging.LogFactory;
66  import java.io.BufferedOutputStream;
67  import java.io.FileOutputStream;
68  import java.io.PrintWriter;
69  import java.util.Date;
70  import java.util.Hashtable;
71  import java.util.Iterator;
72  import java.util.Vector;
73  
74  
75  // TODO: remove the compilation unit after splitting a logic into different classes.
76  public class LoadTestPrinter {
77      private Log log = LogFactory.getLog(LoadTestPrinter.class);
78      private LoadTestConfig loadTestConfig;
79      private String outputFileName;
80      private String timeKeyName;
81      private Vector messageNames;
82      private Vector timeKeys;
83      private Hashtable timeMatrix;
84      private PrintWriter printWriter = null;
85  
86      public LoadTestPrinter() {
87      }
88  
89      private final void createFileWithDir(String fileName) {
90          int index = fileName.lastIndexOf('/');
91  
92          if (index != -1) {
93              String dirName = fileName.substring(0, index);
94              java.io.File dir = new java.io.File(dirName);
95  
96              if (!dir.exists()) {
97                  dir.mkdirs();
98              }
99          }
100 
101         java.io.File file = new java.io.File(fileName);
102 
103         try {
104             file.createNewFile();
105         } catch (java.io.IOException ioe) {
106             ioe.printStackTrace();
107         }
108     }
109 
110     public void genOutput() {
111         try {
112             // first make sure that the full path to the output file is created
113             createFileWithDir(getOutputFileName());
114 
115             // TODO: provide more fine grained interface that would accept printwriter 
116             // and data to process. 
117             FileOutputStream fout = new FileOutputStream(getOutputFileName(),
118                     true);
119             printWriter = new PrintWriter(new BufferedOutputStream(fout));
120 
121             //Write Test Header
122             printWriter.println("# BEGIN ------------------");
123             printWriter.println("# Date: " + new Date());
124             printWriter.println("# TestName = " +
125                 getLoadTestConfig().getName());
126             printWriter.println("# Total # of Messages: " + timeMatrix.size());
127             printWriter.print("#  " + getTimeKeyName() + ", ");
128 
129             for (Iterator iter = getMessageNames().iterator(); iter.hasNext();) {
130                 printWriter.print((String) iter.next() + ", ");
131             }
132 
133             //Output the values.
134             Vector mintime = new Vector();
135             Vector maxtime = new Vector();
136             Vector avgtime = new Vector();
137             int rowsize = 0;
138 
139             for (int k = 0; k < timeKeys.size(); k++) {
140                 String aKey = (String) timeKeys.elementAt(timeKeys.size() - k -
141                         1);
142 
143                 Vector timeVec = (Vector) timeMatrix.get(aKey);
144                 printWriter.print(aKey);
145                 rowsize = timeVec.size();
146 
147                 for (int i = 0; i < timeVec.size(); i++) {
148                     Long recordedTime = (Long) timeVec.elementAt(i);
149                     Long tStart = (Long) timeVec.elementAt(0);
150                     long tInterval = recordedTime.longValue() -
151                         tStart.longValue();
152 
153                     //use absolute time.
154                     printWriter.print(", " + recordedTime.longValue());
155 
156                     //m_PrintWriter.print(" ,  "+tInterval);
157                     if ((k == 0) || (mintime.size() <= i)) {
158                         mintime.addElement(new Long(tInterval));
159                         maxtime.addElement(new Long(tInterval));
160                         avgtime.addElement(new Long(tInterval));
161                     } else {
162                         Long oldmintime = (Long) mintime.elementAt(i);
163                         Long oldmaxtime = (Long) maxtime.elementAt(i);
164                         Long oldavgtime = (Long) avgtime.elementAt(i);
165 
166                         if (tInterval < oldmintime.longValue()) {
167                             mintime.setElementAt(new Long(tInterval), i);
168                         }
169 
170                         if (tInterval > oldmaxtime.longValue()) {
171                             maxtime.setElementAt(new Long(tInterval), i);
172                         }
173 
174                         avgtime.setElementAt(new Long(tInterval +
175                                 oldavgtime.longValue()), i);
176                     }
177                 }
178 
179                 printWriter.println();
180 
181                 //end
182             }
183 
184             //Print out some summary statistics.
185             printWriter.print("# min ");
186 
187             for (int i = 0; i < rowsize; i++) {
188                 if (i == (rowsize - 1)) {
189                     printWriter.println(mintime.elementAt(i));
190                 } else {
191                     printWriter.print(mintime.elementAt(i) + ", ");
192                 }
193             }
194 
195             printWriter.print("# max ");
196 
197             for (int i = 0; i < rowsize; i++) {
198                 if (i == (rowsize - 1)) {
199                     printWriter.println(maxtime.elementAt(i));
200                 } else {
201                     printWriter.print(maxtime.elementAt(i) + ", ");
202                 }
203             }
204 
205             printWriter.print("# avg ");
206 
207             for (int i = 0; i < rowsize; i++) {
208                 Long curtotal = (Long) avgtime.elementAt(i);
209 
210                 //       double curavg = (curtotal.longValue()*1.0)/timeKeyList.size();
211                 double curavg = (curtotal.longValue() * 1.0) / timeMatrix.size();
212 
213                 if (i == (rowsize - 1)) {
214                     printWriter.println(curavg);
215                 } else {
216                     printWriter.print(curavg + ", ");
217                 }
218             }
219 
220             printWriter.println("# END   ------------------");
221             printWriter.close();
222             fout.close();
223         } catch (java.io.FileNotFoundException e) {
224             log.error("could not open outputfile: " + outputFileName, e);
225         } catch (java.io.IOException e2) {
226             log.error("could not close outputfile", e2);
227         }
228     }
229 
230     public void genUnitTestOutput() {
231         try {
232             // first make sure that the full path to the output file is created
233             createFileWithDir(outputFileName);
234 
235             FileOutputStream fout = new FileOutputStream(outputFileName, true);
236             printWriter = new PrintWriter(fout);
237 
238             //mlp Loadtestconfig config = getLoadTestInfo().getLoadTestConfig();
239             //Write Test Header
240             printWriter.println("# BEGIN ------------------");
241             printWriter.println("# Date: " + new Date());
242 
243             String loadTestName = loadTestConfig.getName();
244             printWriter.println("# TestName = " + loadTestName);
245             printWriter.println("# Total # of Messages: " + timeKeys.size());
246 
247             printWriter.print("#  " + timeKeyName + ", ");
248 
249             int namelistsize = messageNames.size();
250 
251             for (int i = 0; i < namelistsize; i++) {
252                 if (i > 0) {
253                     printWriter.print(", ");
254                 }
255 
256                 printWriter.print(messageNames.elementAt(i));
257             }
258 
259             printWriter.println();
260 
261             Vector mintime = new Vector();
262             Vector maxtime = new Vector();
263             Vector sumtime = new Vector();
264 
265             //Used for actual throughput
266             Vector minLegTimes = new Vector();
267             Vector maxLegTimes = new Vector();
268 
269             // initialize the statistics
270             int vecSize = ((Vector) timeMatrix.get(timeKeys.elementAt(0))).size();
271 
272             for (int i = 0; i < vecSize; i++) {
273                 mintime.addElement(new Long(Long.MAX_VALUE));
274                 maxtime.addElement(new Long(0));
275                 sumtime.addElement(new Long(0));
276                 minLegTimes.addElement(new Long(Long.MAX_VALUE));
277                 maxLegTimes.addElement(new Long(0));
278             }
279 
280             for (int k = 0; k < timeKeys.size(); k++) {
281                 String aKey = (String) timeKeys.elementAt(timeKeys.size() - k -
282                         1);
283                 Vector timeVec = (Vector) timeMatrix.get(aKey);
284 
285                 Long tStart = (Long) timeVec.elementAt(0);
286 
287                 for (int i = 0; i < vecSize; i++) {
288                     Long recordedTime = (Long) timeVec.elementAt(i);
289 
290                     long tInterval = recordedTime.longValue() -
291                         tStart.longValue();
292 
293                     if (i > 1) {
294                         printWriter.print(" ,  ");
295                     }
296 
297                     if (i > 0) {
298                         printWriter.print(tInterval);
299                     }
300 
301                     long min = Math.min(((Long) mintime.elementAt(i)).longValue(),
302                             tInterval);
303                     long max = Math.max(((Long) maxtime.elementAt(i)).longValue(),
304                             tInterval);
305                     long sum = ((Long) sumtime.elementAt(i)).longValue() +
306                         tInterval;
307 
308                     long maxLegTime = Math.max(((Long) maxLegTimes.elementAt(i)).longValue(),
309                             recordedTime.longValue());
310                     long minLegTime = Math.min(((Long) minLegTimes.elementAt(i)).longValue(),
311                             recordedTime.longValue());
312 
313                     mintime.setElementAt(new Long(min), i);
314                     maxtime.setElementAt(new Long(max), i);
315                     sumtime.setElementAt(new Long(sum), i);
316                     maxLegTimes.setElementAt(new Long(maxLegTime), i);
317                     minLegTimes.setElementAt(new Long(minLegTime), i);
318                 }
319 
320                 printWriter.println();
321             }
322 
323             printWriter.println("\nTotal # of Messages: " + timeKeys.size());
324 
325             long burstTime = ((Long) maxLegTimes.elementAt(0)).longValue() -
326                 ((Long) minLegTimes.elementAt(0)).longValue();
327             printWriter.print("Stimulus Burst Time:" + burstTime);
328 
329             printWriter.print("\nTotal Processing Time: ");
330 
331             for (int i = 1; i < vecSize; i++) {
332                 if (i > 1) {
333                     printWriter.print(" ,  ");
334                 }
335 
336                 long time = ((Long) maxLegTimes.elementAt(i)).longValue() -
337                     ((Long) minLegTimes.elementAt(0)).longValue();
338                 printWriter.print(time);
339             }
340 
341             printWriter.print("\nAverage Processing Time: ");
342 
343             for (int i = 1; i < vecSize; i++) {
344                 if (i > 1) {
345                     printWriter.print(" ,  ");
346                 }
347 
348                 long time = ((Long) maxLegTimes.elementAt(i)).longValue() -
349                     ((Long) minLegTimes.elementAt(0)).longValue();
350                 printWriter.print((time * 1.0) / timeKeys.size());
351             }
352 
353             //Print out some summary statistics.
354             printWriter.print("\n\nMin (perceived): ");
355 
356             for (int i = 1; i < vecSize; i++) {
357                 if (i > 1) {
358                     printWriter.print(" ,  ");
359                 }
360 
361                 printWriter.print(mintime.elementAt(i));
362             }
363 
364             printWriter.print("\nMax (perceived): ");
365 
366             for (int i = 1; i < vecSize; i++) {
367                 if (i > 1) {
368                     printWriter.print(" ,  ");
369                 }
370 
371                 printWriter.print(maxtime.elementAt(i));
372             }
373 
374             printWriter.print("\nAvg (perceived): ");
375 
376             for (int i = 1; i < vecSize; i++) {
377                 if (i > 1) {
378                     printWriter.print(" ,  ");
379                 }
380 
381                 Long curtotal = (Long) sumtime.elementAt(i);
382                 double curavg = (curtotal.longValue() * 1.0) / timeKeys.size();
383                 printWriter.print(curavg);
384             }
385 
386             printWriter.println("\n# END   ------------------");
387             printWriter.close();
388             fout.close();
389         } catch (java.io.FileNotFoundException e) {
390             log.error("could not open outputfile: " + outputFileName, e);
391         } catch (java.io.IOException e2) {
392             log.error("could not close outputfile", e2);
393         }
394     }
395 
396     public String getOutputFileName() {
397         return outputFileName;
398     }
399 
400     public void setOutputFileName(String outputFileName) {
401         this.outputFileName = outputFileName;
402     }
403 
404     public String getTimeKeyName() {
405         return timeKeyName;
406     }
407 
408     public void setTimeKeyName(String timeKeyName) {
409         this.timeKeyName = timeKeyName;
410     }
411 
412     public Vector getTimeKeys() {
413         return timeKeys;
414     }
415 
416     public void setTimeKeys(Vector timeKeys) {
417         this.timeKeys = timeKeys;
418     }
419 
420     public Hashtable getTimeMatrix() {
421         return timeMatrix;
422     }
423 
424     public void setTimeMatrix(Hashtable timeMatrix) {
425         this.timeMatrix = timeMatrix;
426     }
427 
428     public LoadTestConfig getLoadTestConfig() {
429         return loadTestConfig;
430     }
431 
432     public void setLoadTestConfig(LoadTestConfig loadTestConfig) {
433         this.loadTestConfig = loadTestConfig;
434     }
435 
436     public Vector getMessageNames() {
437         return messageNames;
438     }
439 
440     public void setMessageNames(Vector messageNames) {
441         this.messageNames = messageNames;
442     }
443 }