forked from radargun/radargun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LocalBenchmark.java
182 lines (154 loc) · 6.52 KB
/
LocalBenchmark.java
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package org.radargun.local;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.radargun.CacheWrapper;
import org.radargun.CacheWrapperStressor;
import org.radargun.ShutDownHook;
import org.radargun.utils.TypedProperties;
import org.radargun.utils.Utils;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
/**
* @author Mircea.Markus@jboss.com
*/
public class LocalBenchmark {
private static Log log = LogFactory.getLog(LocalBenchmark.class);
private static final String LOCAL_BENCHMARK_CSV = "local_benchmark.csv";
private static final String REPORTS_DIR = "reports";
private List<CacheWrapperStressor> stressors = new ArrayList<CacheWrapperStressor>();
private LinkedHashMap<String, List<Properties>> product2Config = new LinkedHashMap<String, List<Properties>>();
private boolean headerGenerted = false;
private List<ReportDesc> reportDescs = new ArrayList<ReportDesc>();
private long initialFreeMemory = freeMememory();
private StringBuilder reportCsvContent = new StringBuilder();
public LocalBenchmark() {
Runtime.getRuntime().addShutdownHook(new ShutDownHook("Local benchmark process"));
}
public void benchmark() throws Exception {
log.info("Starting benchmark with " + Utils.kb(initialFreeMemory) + " kb initial free memory.");
for (Map.Entry<String, List<Properties>> product : product2Config.entrySet()) {
for (Properties configProps : product.getValue()) {
final String config = configProps.getProperty("name");
log.info("Processing " + product.getKey() + "-" + config);
CacheWrapper wrapper = getCacheWrapper(product.getKey());
try {
wrapper.setUp(config, true, -1, new TypedProperties(configProps));
Map<String, String> results = null;
for (CacheWrapperStressor stressor : stressors) {
results = stressor.stress(wrapper);
stressor.destroy();
}
generateReport(results, product.getKey(), config);
wrapper.tearDown();
wrapper = null;
gc();
} catch (Exception e) {
log.error("Error processing " + product.getKey() + "-" + config, e);
wrapper.tearDown();
}
}
createOutputFile();
generateChart();
}
}
private void generateChart() throws Exception {
for (ReportDesc reportDesc : reportDescs) {
LocalChartGenerator chartGenerator = new LocalChartGenerator(reportDesc);
chartGenerator.generateChart(REPORTS_DIR);
}
}
private void gc() throws Exception {
log.info(Utils.printMemoryFootprint(true));
for (int i = 0; i < 30; i++) {
System.gc();
if (!freeMemoryOkay()) {
log.info("GC didn't finish the work. Initial free memory was: " + Utils.kb(initialFreeMemory) + "kb, now we have: " + Utils.kb(freeMememory()) + "kb");
Thread.sleep(1000);
} else {
break;
}
}
if (!freeMemoryOkay()) {
log.error("The amount of free memory is more than 10% smaller than original one, benchmarks might be affected. Exiting... ");
ShutDownHook.exit(1);
}
log.info(Utils.printMemoryFootprint(false));
}
private boolean freeMemoryOkay() {
return freeMememory() + 0.1 * freeMememory() >= initialFreeMemory;
}
private void createOutputFile() throws IOException {
File parentDir = new File(REPORTS_DIR);
if (!parentDir.exists()) {
if (!parentDir.mkdirs())
throw new RuntimeException(parentDir.getAbsolutePath() + " does not exist and could not be created!");
}
File reportFile = Utils.createOrReplaceFile(parentDir, LOCAL_BENCHMARK_CSV);
if (!reportFile.exists()) {
throw new IllegalStateException(reportFile.getAbsolutePath() + " was deleted? Not allowed to delete report file during test run!");
}
PrintWriter writer = null;
try {
writer = new PrintWriter(reportFile);
writer.append(reportCsvContent.toString());
} finally {
if (writer != null) writer.close();
}
}
private void generateReport(Map<String, String> results, String product, String config) throws IOException {
generateHeader(results);
StringBuilder line = new StringBuilder();
line.append(product).append(",").append(config);
for (String key : results.keySet()) {
line.append(",").append(results.get(key));
}
writeLine(line.toString());
for (ReportDesc reportDesc : reportDescs) {
long readsPerSec = (long) Double.parseDouble(results.get("READS_PER_SEC"));
long noReads = Long.parseLong(results.get("READ_COUNT"));
long writesPerSec = (long) Double.parseDouble(results.get("WRITES_PER_SEC"));
long noWrites = Long.parseLong(results.get("WRITE_COUNT"));
reportDesc.updateData(product, config, readsPerSec, noReads, writesPerSec, noWrites);
}
}
private void generateHeader(Map<String, String> results) throws FileNotFoundException {
if (!headerGenerted) {
StringBuilder header = new StringBuilder();
header.append("PRODUCT, CONFIG");
for (String key : results.keySet()) {
header.append(",").append(key);
}
writeLine(header.toString());
headerGenerted = true;
}
}
private void writeLine(String line) throws FileNotFoundException {
reportCsvContent.append('\n').append(line);
}
private CacheWrapper getCacheWrapper(String product) throws Exception {
String fqnClass = Utils.getCacheWrapperFqnClass(product);
URLClassLoader loader = Utils.buildProductSpecificClassLoader(product, getClass().getClassLoader());
Thread.currentThread().setContextClassLoader(loader);
return (CacheWrapper) loader.loadClass(fqnClass).newInstance();
}
public void addStressor(CacheWrapperStressor stressor) {
stressors.add(stressor);
}
public void addProductConfig(String productName, List<Properties> configs) {
product2Config.put(productName, configs);
}
public void addReportDesc(ReportDesc reportDesc) {
reportDescs.add(reportDesc);
}
private long freeMememory() {
return Runtime.getRuntime().freeMemory();
}
}