/* Reattore HTTP Server
Copyright (C) 2002 Michael Hope <michaelh@juju.net.nz>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
$Id: CSVReporter.java,v 1.3 2003/03/05 04:31:56 michaelh Exp $
*/
package juju.reattore.perfcap.reporter.impl;
import java.util.*;
import java.io.*;
import java.net.*;
import org.apache.commons.beanutils.BeanUtils;
import juju.reattore.perfcap.reporter.Reporter;
import juju.reattore.perfcap.var.Variable;
import juju.reattore.perfcap.tester.Results;
import juju.reattore.util.Base64;
/** Writes a report to a CSV file. The first line is a header
containing the names of all of the independant variables, followed
by the name of the result point. The following lines are the
samples in order.
@tag csvreporter
@group Reporter
*/
public class CSVReporter
implements Reporter {
private String filter;
private String out;
private PrintStream to;
/** The property from the test results. If unset, serialises the
whole result object as a Base64 string.
@param filter The Bean name of the property.
*/
public void setFilter(String filter) {
this.filter = filter;
}
/** Name of the file to write to. If unset, writes to stdout.
@param out Name of the file.
*/
public void setOut(String out) {
this.out = out;
}
private void setup()
throws IOException {
if (out == null) {
to = System.out;
}
else {
to = new PrintStream(new FileOutputStream(out));
}
}
private void writeHeader(List ind, Results res)
throws IOException {
StringBuffer line = new StringBuffer();
for (Iterator i = ind.iterator(); i.hasNext();) {
Variable var = (Variable)i.next();
line.append(var.getName());
line.append(",");
}
line.append(filter != null ? filter : "Data");
to.println(line.toString());
}
private String encode(Object obj)
throws IOException {
ByteArrayOutputStream buf = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(buf);
out.writeObject(obj);
out.flush();
return new String(Base64.encode(buf.toByteArray()));
}
/** @see Reporter */
public void add(List ind, Results res)
throws Exception {
if (to == null) {
setup();
writeHeader(ind, res);
}
StringBuffer line = new StringBuffer();
for (Iterator i = ind.iterator(); i.hasNext();) {
Variable var = (Variable)i.next();
line.append(var.getValue());
line.append(",");
}
if (filter != null) {
line.append(BeanUtils.getProperty(res, filter));
}
else {
line.append(encode(res));
}
to.println(line.toString());
to.flush();
}
/** @see Reporter */
public void end()
throws Exception {
if (to != System.out) {
to.close();
}
}
}