20 package com.mysql.cluster.benchmark.tws;
22 import java.io.PrintWriter;
23 import java.io.FileInputStream;
24 import java.io.InputStream;
25 import java.io.IOException;
26 import java.io.FileWriter;
28 import java.util.Properties;
29 import java.util.List;
30 import java.util.ArrayList;
31 import java.util.Date;
32 import java.text.SimpleDateFormat;
38 static protected final PrintWriter out =
new PrintWriter(System.out,
true);
39 static protected final PrintWriter err =
new PrintWriter(System.err,
true);
42 static protected final String endl = System.getProperty(
"line.separator");
43 static protected final Runtime rt = Runtime.getRuntime();
46 static private final List<String> propFileNames =
new ArrayList<String>();
47 static private String logFileName;
50 protected final Properties props =
new Properties();
51 protected boolean logRealTime;
52 protected boolean logMemUsage;
53 protected boolean includeFullGC;
54 protected int warmupRuns;
57 protected PrintWriter log;
58 protected boolean logHeader;
59 protected StringBuilder header;
60 protected StringBuilder rtimes;
61 protected StringBuilder musage;
62 protected long t0 = 0, t1 = 0, ta = 0;
63 protected long m0 = 0, m1 = 0, ma = 0;
73 out.println(
"usage: [options]");
74 out.println(
" [-p <file name>]... a properties file name");
75 out.println(
" [-l <file name>] log file name for data output");
76 out.println(
" [-h|--help] print usage message and exit");
85 for (
int i = 0;
i < args.length;
i++) {
87 if (arg.equals(
"-p")) {
88 if (
i >= args.length) {
91 propFileNames.add(args[++
i]);
92 }
else if (arg.equals(
"-l")) {
93 if (
i >= args.length) {
96 logFileName = args[++
i];
97 }
else if (arg.equals(
"-h") || arg.equals(
"--help")) {
100 out.println(
"unknown option: " + arg);
105 if (propFileNames.size() == 0) {
106 propFileNames.add(
"run.properties");
109 if (logFileName == null) {
110 SimpleDateFormat sdf =
new SimpleDateFormat(
"yyyyMMdd_HHMMss");
111 logFileName = (
"log_" + sdf.format(
new Date()) +
".txt");
127 if (warmupRuns > 0) {
129 out.println(
"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
130 out.println(
"warmup runs ...");
131 out.println(
"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
133 for (
int i = 0;
i < warmupRuns;
i++) {
144 out.println(
"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
145 out.println(
"hot runs ...");
146 out.println(
"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
150 out.println(
"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
152 }
catch (Exception ex) {
154 out.println(
"caught " + ex);
155 ex.printStackTrace();
165 static protected void loadSystemLibrary(
String name) {
166 out.print(
"loading libary ...");
169 System.loadLibrary(name);
170 }
catch (UnsatisfiedLinkError e) {
173 path = System.getProperty(
"java.library.path");
174 }
catch (Exception ex) {
175 path =
"<exception caught: " + ex.getMessage() +
">";
177 err.println(
"NdbBase: failed loading library '"
178 + name +
"'; java.library.path='" + path +
"'");
180 }
catch (SecurityException e) {
181 err.println(
"NdbBase: failed loading library '"
182 + name +
"'; caught exception: " + e);
185 out.println(
" [" + name +
"]");
189 static private void gc() {
193 final int nFullGCs = 10;
194 for (
int i = 0;
i < nFullGCs;
i++) {
197 long newfree = rt.freeMemory();
200 rt.runFinalization();
202 newfree = rt.freeMemory();
204 }
while (newfree > oldfree);
210 protected void init() throws Exception {
219 protected void close() throws Exception {
233 private void loadProperties() throws IOException {
235 for (
String fn : propFileNames) {
236 out.println(
"reading properties file: " + fn);
249 protected boolean parseBoolean(
String k,
boolean vdefault) {
250 final String v = props.getProperty(k);
251 return (v == null ? vdefault : Boolean.parseBoolean(v));
255 protected int parseInt(
String k,
int vdefault) {
256 final String v = props.getProperty(k);
258 return (v == null ? vdefault : Integer.parseInt(v));
259 }
catch (NumberFormatException e) {
260 final NumberFormatException nfe =
new NumberFormatException(
261 "invalid value of benchmark property ('" + k +
"', '"
269 protected void initProperties() {
271 out.print(
"setting driver properties ...");
274 final StringBuilder
msg =
new StringBuilder();
275 final String eol = System.getProperty(
"line.separator");
277 logRealTime = parseBoolean(
"logRealTime",
true);
278 logMemUsage = parseBoolean(
"logMemUsage",
false);
279 includeFullGC = parseBoolean(
"includeFullGC",
false);
281 warmupRuns = parseInt(
"warmupRuns", 0);
282 if (warmupRuns < 0) {
283 msg.append(
"[ignored] warmupRuns: " + warmupRuns + eol);
287 if (msg.length() == 0) {
288 out.println(
" [ok]");
291 out.print(msg.toString());
296 protected void printProperties() {
298 out.println(
"driver settings ...");
299 out.println(
"logRealTime: " + logRealTime);
300 out.println(
"logMemUsage: " + logMemUsage);
301 out.println(
"includeFullGC: " + includeFullGC);
302 out.println(
"warmupRuns: " + warmupRuns);
306 private void openLogFile() throws IOException {
308 out.println(
"writing results to file: " + logFileName);
309 log =
new PrintWriter(
new FileWriter(logFileName,
false));
313 private void closeLogFile() throws IOException {
314 out.print(
"closing files ...");
320 out.println(
" [ok]");
327 abstract protected void runTests() throws Exception;
329 protected
void clearLogBuffers() {
331 header =
new StringBuilder();
333 rtimes =
new StringBuilder();
336 musage =
new StringBuilder();
342 log.println(descr +
", rtime[ms]"
343 + header.toString() + endl
344 + rtimes.toString() + endl);
347 log.println(descr +
", net musage[KiB]"
348 + header.toString() + endl
349 + musage.toString() + endl);
353 protected void begin(
String name) {
361 m0 = rt.totalMemory() - rt.freeMemory();
366 t0 = System.nanoTime() / 1000000;
370 protected void finish(
String name) {
378 t1 = System.nanoTime() / 1000000;
379 final long t = t1 - t0;
380 out.println(
"tx real time " + t
383 rtimes.append(
"\t" + t);
390 m1 = rt.totalMemory() - rt.freeMemory();
391 final long m0K = (m0 / 1024);
392 final long m1K = (m1 / 1024);
393 final long mK = m1K - m0K;
394 out.println(
"net mem usage "
395 + (mK >= 0 ?
"+" :
"") + mK
396 +
"\tKiB [" + m0K +
"K->" + m1K +
"K]");
397 musage.append(
"\t" + mK);
402 header.append(
"\t" + name);