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  //Created on Oct 6, 2004
33  package com.reuters.msgtest.load.output;
34  
35  import com.reuters.msgtest.RvTestException;
36  import com.reuters.msgtest.load.LoadTestResult;
37  import com.reuters.msgtest.load.LoadTestResults;
38  import com.reuters.msgtest.load.output.csv.ExcelCSVPrinter;
39  import java.io.IOException;
40  import java.io.Writer;
41  import java.util.Date;
42  import java.util.Iterator;
43  
44  
45  /***
46   * @author <a href="mailto:Kurman.Karabukaev@thoughtworks.com">Kurman</a>
47   *
48   */
49  public class LoadTestCSVWriter implements LoadTestOutput {
50      public void outputResults(LoadTestResults loadTestResults, Writer writer)
51          throws RvTestException {
52          ExcelCSVPrinter ecsvp = new ExcelCSVPrinter(writer);
53  
54          try {
55              createHeader(loadTestResults.getLoadFixtureName(), ecsvp);
56  
57              if (loadTestResults.size() < 1) {
58                  ecsvp.writeln("No test results available");
59              } else {
60                  ecsvp.writeln(new String[] {
61                          "Number of runs :",
62                          Integer.toString(loadTestResults.size())
63                      });
64                  ecsvp.writeln(new String[] {
65                          "Test start time :",
66                          new Date(loadTestResults.getTestStartTime()).toString()
67                      });
68                  ecsvp.writeln(new String[] {
69                          "Last response received :",
70                          new Date(loadTestResults.getLastResponseTime()).toString()
71                      });
72                  ecsvp.writeln(new String[] {
73                          "Sum of all executions (mills.): ",
74                          Long.toString((loadTestResults.getTotalExecutionTime()))
75                      });
76                  ecsvp.writeln(new String[] {
77                          "Average execution time (mills.): ",
78                          Long.toString(
79                              (loadTestResults.getAverageExecutionTime()))
80                      });
81              }
82  
83              for (Iterator iter = loadTestResults.iterator(); iter.hasNext();) {
84                  LoadTestResult loadTestResult = (LoadTestResult) iter.next();
85                  ecsvp.writeln(new String[] {
86                          Long.toString(loadTestResult.getSentTime()),
87                          Long.toString(loadTestResult.getReceiveTime())
88                      });
89              }
90  
91              createFooter(ecsvp);
92          } catch (IOException e) {
93              throw new RvTestException("Could not write load test results", e);
94          } finally {
95              try {
96                  ecsvp.close(); // closes the stream as well.
97              } catch (IOException e) {
98              }
99          }
100     }
101 
102     private void createFooter(ExcelCSVPrinter ecsvp) throws IOException {
103         ecsvp.writeln();
104         ecsvp.writeln();
105     }
106 
107     private void createHeader(String loadFixtureName, ExcelCSVPrinter ecsvp)
108         throws IOException {
109         ecsvp.writeln(new String[] { "Load Test Fixture : ", loadFixtureName });
110         ecsvp.writeln((new Date()).toString());
111     }
112 }