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 java.io.IOException;
39  import java.io.Writer;
40  import java.util.Date;
41  import java.util.Iterator;
42  
43  
44  /***
45   * @author <a href="mailto:Kurman.Karabukaev@thoughtworks.com">Kurman</a>
46   *
47   */
48  public class LoadTestWriter implements LoadTestOutput {
49      public void outputResults(LoadTestResults loadTestResults, Writer writer)
50          throws RvTestException {
51          try {
52              StringBuffer content = new StringBuffer();
53              createHeader(loadTestResults.getLoadFixtureName(), content);
54  
55              if (loadTestResults.size() < 1) {
56                  content.append("No test results available\n");
57              } else {
58                  content.append("Number of runs :" + loadTestResults.size());
59                  content.append("\n");
60                  content.append("Test start time :" +
61                      new Date(loadTestResults.getTestStartTime()));
62                  content.append("\n");
63                  content.append("Last response received :" +
64                      new Date(loadTestResults.getLastResponseTime()));
65                  content.append("\n");
66                  content.append("Sum of all executions : " +
67                      (loadTestResults.getTotalExecutionTime() / 1000) + " sec.");
68                  content.append("\n");
69                  content.append("Average execution time : " +
70                      (loadTestResults.getAverageExecutionTime() / 1000) +
71                      " sec.");
72                  content.append("\n");
73              }
74  
75              for (Iterator iter = loadTestResults.iterator(); iter.hasNext();) {
76                  LoadTestResult loadTestResult = (LoadTestResult) iter.next();
77                  content.append(loadTestResult.getSentTime() + ", " +
78                      loadTestResult.getReceiveTime());
79                  content.append("\n");
80              }
81  
82              createFooter(content);
83              writer.write(content.toString());
84          } catch (IOException e) {
85              throw new RvTestException("Could not write load test results", e);
86          } finally {
87              try {
88                  writer.close();
89              } catch (IOException e) {
90              }
91          }
92      }
93  
94      private void createFooter(StringBuffer content) {
95          content.append("###########################\n");
96      }
97  
98      private StringBuffer createHeader(String loadFixtureName,
99          StringBuffer content) {
100         content.append("###########################");
101         content.append("\n");
102         content.append("Load Test Fixture : " + loadFixtureName);
103         content.append("\n");
104         content.append(new Date());
105         content.append("\n");
106 
107         return content;
108     }
109 }