1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.scalabench.plugins.dacapo;
19
20 import java.io.Closeable;
21 import java.io.File;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.OutputStreamWriter;
25 import java.io.Writer;
26 import java.util.Deque;
27 import java.util.LinkedList;
28 import java.util.List;
29
30 import org.scalabench.plugins.dacapo.config.BenchmarkConfiguration;
31 import org.scalabench.plugins.dacapo.config.Description;
32 import org.scalabench.plugins.dacapo.config.OutputFile;
33 import org.scalabench.plugins.dacapo.config.Size;
34
35 public class ConfigrationWriter {
36
37 private final String name;
38
39 private final BenchmarkConfiguration configuration;
40
41 private final List<String> libraryJars;
42
43 private final Deque<String> entrySeparators = new LinkedList<String>();
44
45 private final Deque<Integer> entryCounts = new LinkedList<Integer>();
46
47 private Writer out;
48
49 public ConfigrationWriter(String name, BenchmarkConfiguration configuration, List<String> libraryJars) {
50 this.name = name;
51 this.configuration = configuration;
52 this.libraryJars = libraryJars;
53 }
54
55 public synchronized void writeConfiguration(File file) throws IOException {
56 FileOutputStream fos = null;
57 out = null;
58
59 try {
60 fos = new FileOutputStream(file);
61 out = new OutputStreamWriter(fos, "US-ASCII");
62
63 writeContent();
64 } finally {
65 close(out);
66 close(fos);
67 }
68 }
69
70 private void writeContent() throws IOException {
71 startSection(";\n");
72 writeHeader();
73 for (Size size : configuration.getSizes())
74 writeSize(size);
75 writeDescription();
76 endSection(";\n");
77 }
78
79 private void writeHeader() throws IOException {
80 writeEntry("benchmark", name);
81 startSection();
82 writeEntryIfPresent("class", configuration.getHarnessClass());
83 writeEntryIfPresent("thread-model", configuration.getThreadModel());
84 writeLibsSection();
85 endSection();
86 }
87
88 private void writeLibsSection() throws IOException {
89 if (libraryJars.isEmpty())
90 return;
91
92 writeEntry("libs");
93 startSection(",");
94 for (String libraryJar : libraryJars)
95 writeQuotedEntry(libraryJar);
96 endSection();
97 }
98
99 private void writeSize(Size size) throws IOException {
100 writeEntry("size", size.getName());
101 startSection();
102 writeArgs(size.getArgs());
103 writeEntryIfNotDefault("threads", size.getThreads(), 1);
104 writeEntryIfNotDefault("thread-limit", size.getThreadLimit(), 0);
105 writeOutputFiles(size.getOutputs());
106 writeQuotedEntryIfPresent("description", size.getDescription());
107 endSection();
108 }
109
110 private void writeArgs(List<String> args) throws IOException {
111 writeEntry("args");
112 startSection(",");
113 for (String arg : args)
114 writeQuotedEntry(arg);
115 endSection();
116 }
117
118 private void writeOutputFiles(List<OutputFile> outputs) throws IOException {
119 if (outputs.isEmpty())
120 return;
121
122 writeEntry("output");
123 startSection(",");
124 for (OutputFile output : outputs)
125 writeOutputFile(output);
126 endSection();
127 }
128
129 private void writeOutputFile(OutputFile output) throws IOException {
130 final String textOption = output.isText() ? "text" : "binary";
131 final String filterOption = output.isFilter() ? "filter" : "raw";
132
133 writeQuotedEntry(output.getName());
134 startSection();
135 writeEntryIfPresent("digest", output.getChecksum(), textOption, filterOption);
136 writeEntryIfNotDefault("lines", output.getLines(), -1);
137 writeEntryIfNotDefault("bytes", output.getBytes(), -1);
138 if (output.isExists()) writeEntry("exists");
139 if (output.isKeep()) writeEntry("keep");
140 endSection();
141 }
142
143 private void writeDescription() throws IOException {
144 final Description description = configuration.getDescription();
145
146 writeEntry("description");
147 startSection(",");
148 writeQuotedEntryIfPresent("short", description.getShort());
149 writeQuotedEntryIfPresent("long", description.getLong());
150 writeQuotedEntryIfPresent("author", description.getAuthor());
151 writeQuotedEntryIfPresent("copyright", description.getCopyright());
152 writeQuotedEntryIfPresent("license", description.getLicense());
153 writeQuotedEntryIfPresent("url", description.getUrl());
154 writeQuotedEntryIfPresent("version", description.getVersion());
155 writeQuotedEntryIfPresent("threads", description.getThreads());
156 writeQuotedEntryIfPresent("repeats", description.getRepeats());
157 endSection();
158 }
159
160 private void startSection() throws IOException {
161 startSection("");
162 }
163
164 private void startSection(String entrySeparator) throws IOException {
165 if (entryCounts.size() != 0)
166 out.append("\n");
167 entrySeparators.push(entrySeparator);
168 entryCounts.push(0);
169 }
170
171 private void endSection() throws IOException {
172 endSection("");
173 }
174
175 private void endSection(String sectionTerminator) throws IOException {
176 out.append(sectionTerminator);
177 entrySeparators.pop();
178 entryCounts.pop();
179 }
180
181 private void writeEntry(String value) throws IOException {
182 separate();
183 out.append(value);
184 incrementEntryCount();
185 }
186
187 private void writeQuotedEntry(String value) throws IOException {
188 separate();
189 out.append('"').append(value).append('"');
190 incrementEntryCount();
191 }
192
193 private void writeEntry(String name, String value, String... options) throws IOException {
194 separate();
195 out.append(name);
196 if (options.length != 0) {
197 out.append('(').append(options[0]);
198 for (int i = 1; i < options.length; i++)
199 out.append(',').append(' ').append(options[i]);
200 out.append(')');
201 }
202 out.append(' ').append(value);
203 incrementEntryCount();
204 }
205
206 private void writeQuotedEntry(String name, String value) throws IOException {
207 separate();
208 out.append(name).append(' ').append('"').append(value).append('"');
209 incrementEntryCount();
210 }
211
212 private void writeEntryIfPresent(String name, String value, String... options) throws IOException {
213 if (value != null) writeEntry(name, value, options);
214 }
215
216 private void writeQuotedEntryIfPresent(String name, String value) throws IOException {
217 if (value != null) writeQuotedEntry(name, value);
218 }
219
220 private void writeEntryIfNotDefault(String name, int value, int defaultValue) throws IOException {
221 if (value != defaultValue) writeEntry(name, Integer.toString(value));
222 }
223
224 private void separate() throws IOException {
225 if (entryCounts.peek() > 0)
226 out.append(entrySeparators.peek()).append('\n');
227 indent();
228 }
229
230 private void indent() throws IOException {
231 for (int i = 0; i < entryCounts.size() - 1; i++)
232 out.append(" ");
233 }
234
235 private void incrementEntryCount() {
236 entryCounts.push(entryCounts.pop() + 1);
237 }
238
239 private static void close(Closeable closeable) throws IOException {
240 if (closeable != null)
241 closeable.close();
242 }
243 }