Source code: juju/reattore/perfcap/tester/impl/CSVTester.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: CSVTester.java,v 1.3 2003/03/05 04:31:57 michaelh Exp $
20 */
21
22 package juju.reattore.perfcap.tester.impl;
23
24 import java.util.*;
25 import java.io.*;
26 import java.net.URLDecoder;
27
28 import org.apache.commons.logging.*;
29
30 import juju.reattore.perfcap.PerfCap;
31 import juju.reattore.perfcap.var.Variable;
32 import juju.reattore.perfcap.tester.*;
33 import juju.reattore.util.Base64;
34
35 /** 'Tests' using previously recorded data. This tester can import
36 data previously recorded using the CSVReporter including
37 de-serialising the recorded results.
38
39 @tag csvtester
40 @group Tester
41 */
42 public class CSVTester
43 implements Tester {
44
45 private static Log log = LogFactory.getLog(CSVTester.class);
46
47 private static class Var
48 implements Variable {
49
50 private List values = new ArrayList();
51 private Object cur;
52 private Iterator it;
53
54 private String name;
55
56 private Var(String name) {
57 this.name = name;
58 }
59
60 private void addValue(String val) {
61 if (values.contains(val) == false) {
62 values.add(val);
63 }
64 }
65
66 public String getName() {
67 return name;
68 }
69
70 public Object getValue() {
71 return cur;
72 }
73
74 public boolean hasNext() {
75 return it.hasNext();
76 }
77
78 public void next() {
79 cur = it.next();
80 }
81
82 public void begin() {
83 it = values.iterator();
84 }
85
86 public void end() {
87 cur = null;
88 it = null;
89 }
90 }
91
92 private List vars = new ArrayList();
93 private List res = new ArrayList();
94 private Iterator it;
95
96 private Results decode(String val)
97 throws Exception {
98
99 char[] ch = new char[val.length()];
100 val.getChars(0, val.length(), ch, 0);
101
102 byte[] chars = Base64.decode(ch);
103
104 ByteArrayInputStream buf = new ByteArrayInputStream(chars);
105 ObjectInputStream in = new ObjectInputStream(buf);
106
107 return (Results)in.readObject();
108 }
109
110 private void load(String name)
111 throws Exception {
112
113 BufferedReader in = new BufferedReader(new FileReader(name));
114
115 String[] names = in.readLine().split(",");
116
117 for (int i = 0; i < names.length - 1; i++) {
118 vars.add(new Var(names[i]));
119 }
120
121 String got;
122
123 while ((got = in.readLine()) != null) {
124 String[] vals = got.split(",");
125
126 for (int i = 0; i < vals.length - 1; i++) {
127 ((Var)vars.get(i)).addValue(vals[i]);
128 }
129
130 res.add(decode(vals[vals.length - 1]));
131 }
132
133 in.close();
134 }
135
136 /** The file to read the data from.
137
138 @param name The file name.
139 @throws Exception if the file can't be written to.
140 */
141 public void setSource(String name)
142 throws Exception {
143
144 load(name);
145 it = res.iterator();
146 }
147
148 /** Attach to the top level runner.
149
150 @param top The top level.
151 */
152 public void setPerfCap(PerfCap top) {
153 for (Iterator i = vars.iterator(); i.hasNext();) {
154 top.addVariable((Variable)i.next());
155 }
156 }
157
158 /** @see Tester */
159 public Results go()
160 throws Exception {
161
162 if (it.hasNext()) {
163 return (Results)it.next();
164 }
165 else {
166 return null;
167 }
168 }
169 }