1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package org.perfidix.ouput;
28
29 import java.io.File;
30 import java.io.FileNotFoundException;
31 import java.io.FileOutputStream;
32 import java.io.PrintStream;
33 import java.lang.reflect.Method;
34 import java.util.Hashtable;
35 import java.util.Map;
36
37 import org.perfidix.exceptions.AbstractPerfidixMethodException;
38 import org.perfidix.meter.AbstractMeter;
39 import org.perfidix.result.BenchmarkResult;
40 import org.perfidix.result.ClassResult;
41 import org.perfidix.result.MethodResult;
42
43
44
45
46
47
48 public final class CSVOutput extends AbstractOutput {
49
50
51
52
53 private static final String SEPARATOR = "-";
54
55
56 private transient final File folder;
57
58
59
60
61 private transient boolean firstResult;
62
63
64
65
66 private transient boolean firstException;
67
68
69
70
71 private transient final Map<File, PrintStream> usedFiles;
72
73
74
75
76
77
78
79 public CSVOutput(final File paramFolder) {
80 super();
81 folder = paramFolder;
82 if (folder != null && !folder.isDirectory()) {
83 throw new IllegalStateException(new StringBuilder(paramFolder.toString()).append(
84 " has to be a folder!").toString());
85 }
86 firstResult = true;
87 firstException = true;
88 usedFiles = new Hashtable<File, PrintStream>();
89 }
90
91
92
93
94 public CSVOutput() {
95 this(null);
96 }
97
98
99
100
101 @Override
102 public boolean listenToResultSet(final Method meth, final AbstractMeter meter, final double data) {
103 final PrintStream stream =
104 setUpNewPrintStream(false, meth.getDeclaringClass().getSimpleName(), meth.getName(), meter
105 .getName());
106 if (!firstResult) {
107 stream.append(",");
108 }
109 stream.append(Double.toString(data));
110 stream.flush();
111 firstResult = false;
112 return true;
113
114 }
115
116
117 @Override
118 public boolean listenToException(final AbstractPerfidixMethodException exec) {
119 final PrintStream currentWriter = setUpNewPrintStream(false, "Exceptions");
120 if (!firstException) {
121 currentWriter.append("\n");
122 }
123 currentWriter.append(exec.getRelatedAnno().getSimpleName());
124 currentWriter.append(",");
125 if (exec.getMethod() != null) {
126 currentWriter.append(exec.getMethod().getDeclaringClass().getSimpleName());
127 currentWriter.append("#");
128 currentWriter.append(exec.getMethod().getName());
129 currentWriter.append(",");
130 }
131 exec.getExec().printStackTrace(currentWriter);
132 currentWriter.flush();
133 firstException = false;
134 return true;
135
136 }
137
138
139 @Override
140 public void visitBenchmark(final BenchmarkResult res) {
141
142 for (final ClassResult classRes : res.getIncludedResults()) {
143 for (final MethodResult methRes : classRes.getIncludedResults()) {
144 for (final AbstractMeter meter : methRes.getRegisteredMeters()) {
145 final PrintStream currentWriter =
146 setUpNewPrintStream(true, classRes.getElementName(), methRes.getElementName(), meter
147 .getName());
148 boolean first = true;
149 for (final Double d : methRes.getResultSet(meter)) {
150 if (first) {
151 currentWriter.append(d.toString());
152 first = false;
153 } else {
154 currentWriter.append(new StringBuilder(",").append(d.toString()).toString());
155 }
156 }
157
158 currentWriter.flush();
159 }
160 }
161 }
162
163 final PrintStream currentWriter = setUpNewPrintStream(true, "Exceptions");
164
165 for (final AbstractPerfidixMethodException exec : res.getExceptions()) {
166 currentWriter.append(exec.getRelatedAnno().getSimpleName());
167 if (exec.getMethod() != null) {
168 currentWriter.append(":");
169 currentWriter.append(exec.getMethod().getDeclaringClass().getSimpleName());
170 currentWriter.append("#");
171 currentWriter.append(exec.getMethod().getName());
172 }
173 currentWriter.append("\n");
174 exec.getExec().printStackTrace(currentWriter);
175 }
176
177 currentWriter.flush();
178 tearDownAllStreams();
179 }
180
181 private void tearDownAllStreams() {
182 for (final PrintStream stream : usedFiles.values()) {
183 stream.close();
184 }
185 }
186
187
188
189
190
191
192
193
194
195
196
197
198
199 private PrintStream setUpNewPrintStream(final boolean visitorStream, final String... names) {
200
201 PrintStream out = System.out;
202
203 if (folder == null) {
204 if (visitorStream) {
205 out.println();
206 }
207 } else {
208 final File toWriteTo = new File(folder, buildFileName(names));
209 try {
210 if (usedFiles.containsKey(toWriteTo)) {
211 out = usedFiles.get(toWriteTo);
212 } else {
213 toWriteTo.delete();
214 out = new PrintStream(new FileOutputStream(toWriteTo, !visitorStream));
215 usedFiles.put(toWriteTo, out);
216 firstResult = true;
217 }
218 } catch (final FileNotFoundException e) {
219 throw new IllegalStateException(e);
220 }
221
222 }
223 return out;
224
225 }
226
227
228
229
230
231
232
233
234 private String buildFileName(final String... names) {
235 final StringBuilder builder = new StringBuilder();
236 for (int i = 0; i < names.length; i++) {
237 builder.append(names[i]);
238 if (i < names.length - 1) {
239 builder.append(SEPARATOR);
240 }
241 }
242 builder.append(".").append("csv");
243 return builder.toString();
244 }
245
246 }