Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: juju/reattore/perfcap/reporter/impl/CSVReporter.java


1   /*  Reattore HTTP Server
2   
3       Copyright (C) 2002 Michael Hope <michaelh@juju.net.nz>
4   
5       This program is free software; you can redistribute it and/or modify
6       it under the terms of the GNU General Public License as published by
7       the Free Software Foundation; either version 2 of the License, or
8       (at your option) any later version.
9   
10      This program 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
13      GNU General Public License for more details.
14  
15      You should have received a copy of the GNU General Public License
16      along with this program; if not, write to the Free Software
17      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  
19      $Id: CSVReporter.java,v 1.3 2003/03/05 04:31:56 michaelh Exp $
20  */
21  
22  package juju.reattore.perfcap.reporter.impl;
23  
24  import java.util.*;
25  import java.io.*;
26  import java.net.*;
27  
28  import org.apache.commons.beanutils.BeanUtils;
29  
30  import juju.reattore.perfcap.reporter.Reporter;
31  import juju.reattore.perfcap.var.Variable;
32  import juju.reattore.perfcap.tester.Results;
33  import juju.reattore.util.Base64;
34  
35  /** Writes a report to a CSV file.  The first line is a header
36      containing the names of all of the independant variables, followed
37      by the name of the result point.  The following lines are the
38      samples in order.
39  
40      @tag csvreporter
41      @group Reporter
42  */
43  public class CSVReporter
44      implements Reporter {
45  
46      private String filter;
47      private String out;
48  
49      private PrintStream to;
50  
51      /** The property from the test results.  If unset, serialises the
52          whole result object as a Base64 string.
53  
54          @param filter  The Bean name of the property.
55       */
56      public void setFilter(String filter) {
57          this.filter = filter;
58      }
59  
60      /** Name of the file to write to.  If unset, writes to stdout.
61  
62          @param out  Name of the file.
63      */
64      public void setOut(String out) {
65          this.out = out;
66      }
67  
68      private void setup()
69          throws IOException {
70  
71          if (out == null) {
72              to = System.out;
73          }
74          else {
75              to = new PrintStream(new FileOutputStream(out));
76          }
77      }
78  
79      private void writeHeader(List ind, Results res)
80          throws IOException {
81  
82          StringBuffer line = new StringBuffer();
83  
84          for (Iterator i = ind.iterator(); i.hasNext();) {
85              Variable var = (Variable)i.next();
86              line.append(var.getName());
87              line.append(",");
88          }
89  
90          line.append(filter != null ? filter : "Data");
91  
92          to.println(line.toString());
93      }
94  
95      private String encode(Object obj)
96          throws IOException {
97  
98          ByteArrayOutputStream buf = new ByteArrayOutputStream();
99          ObjectOutputStream out = new ObjectOutputStream(buf);
100 
101         out.writeObject(obj);
102         out.flush();
103 
104         return new String(Base64.encode(buf.toByteArray()));
105     }
106 
107     /** @see Reporter */
108     public void add(List ind, Results res)
109         throws Exception {
110 
111         if (to == null) {
112             setup();
113             writeHeader(ind, res);
114         }
115 
116         StringBuffer line = new StringBuffer();
117 
118         for (Iterator i = ind.iterator(); i.hasNext();) {
119             Variable var = (Variable)i.next();
120 
121             line.append(var.getValue());
122             line.append(",");
123         }
124 
125         if (filter != null) {
126             line.append(BeanUtils.getProperty(res, filter));
127         }
128         else {
129             line.append(encode(res));
130         }
131 
132         to.println(line.toString());
133         to.flush();
134     }
135 
136     /** @see Reporter */
137     public void end()
138         throws Exception {
139 
140         if (to != System.out) {
141             to.close();
142         }
143     }
144 }