20 package com.mysql.cluster.crund;
22 import java.util.Properties;
23 import java.util.List;
24 import java.util.ArrayList;
25 import java.util.Date;
26 import java.text.SimpleDateFormat;
29 import java.io.FileInputStream;
30 import java.io.FileNotFoundException;
31 import java.io.FileOutputStream;
32 import java.io.FileWriter;
33 import java.io.IOException;
34 import java.io.OutputStream;
35 import java.io.PrintWriter;
36 import java.io.InputStream;
61 static protected final PrintWriter out =
new PrintWriter(System.out,
true);
62 static protected final PrintWriter err =
new PrintWriter(System.err,
true);
65 static protected final String endl = System.getProperty(
"line.separator");
66 static protected final Runtime rt = Runtime.getRuntime();
69 static private final List<String> propFileNames =
new ArrayList<String>();
70 static private String logFileName;
73 protected final Properties props =
new Properties();
74 protected boolean logRealTime;
75 protected boolean logMemUsage;
76 protected boolean includeFullGC;
80 protected PrintWriter log;
81 protected String descr =
"";
82 protected boolean logHeader;
83 protected StringBuilder header;
84 protected StringBuilder rtimes;
85 protected StringBuilder musage;
86 protected long t0 = 0, t1 = 0, ta = 0;
87 protected long m0 = 0, m1 = 0, ma = 0;
97 out.println(
"usage: [options]");
98 out.println(
" [-p <file name>]... a properties file name");
99 out.println(
" [-l <file name>] log file name for data output");
100 out.println(
" [-h|--help] print usage message and exit");
109 for (
int i = 0;
i < args.length;
i++) {
111 if (arg.equals(
"-p")) {
112 if (
i >= args.length) {
115 propFileNames.add(args[++
i]);
116 }
else if (arg.equals(
"-l")) {
117 if (
i >= args.length) {
120 logFileName = args[++
i];
121 }
else if (arg.equals(
"-h") || arg.equals(
"--help")) {
124 out.println(
"unknown option: " + arg);
129 if (propFileNames.size() == 0) {
130 propFileNames.add(
"run.properties");
133 if (logFileName == null) {
134 SimpleDateFormat sdf =
new SimpleDateFormat(
"yyyyMMdd_HHMMss");
135 logFileName = (
"log_" + sdf.format(
new Date()) +
".txt");
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 {
214 writeProperties(
"logging.properties");
216 System.setProperty(
"java.util.logging.config.file",
"logging.properties");
222 protected void close() throws Exception {
234 private void loadProperties() throws IOException {
236 for (
String fn : propFileNames) {
237 out.println(
"reading properties file: " + fn);
250 protected boolean parseBoolean(
String k,
boolean vdefault) {
251 final String v = props.getProperty(k);
252 return (v == null ? vdefault : Boolean.parseBoolean(v));
256 protected int parseInt(
String k,
int vdefault) {
257 final String v = props.getProperty(k);
259 return (v == null ? vdefault : Integer.parseInt(v));
260 }
catch (NumberFormatException e) {
261 final NumberFormatException nfe =
new NumberFormatException(
262 "invalid value of benchmark property ('" + k +
"', '"
270 protected void initProperties() {
272 out.print(
"setting driver properties ...");
275 final StringBuilder
msg =
new StringBuilder();
276 final String eol = System.getProperty(
"line.separator");
278 logRealTime = parseBoolean(
"logRealTime",
true);
279 logMemUsage = parseBoolean(
"logMemUsage",
false);
280 includeFullGC = parseBoolean(
"includeFullGC",
false);
282 nRuns = parseInt(
"nRuns", 1);
284 msg.append(
"[ignored] nRuns: " + nRuns + eol);
288 if (msg.length() == 0) {
289 out.println(
" [ok]");
292 out.print(msg.toString());
297 protected void printProperties() {
299 out.println(
"driver settings ...");
300 out.println(
"logRealTime: " + logRealTime);
301 out.println(
"logMemUsage: " + logMemUsage);
302 out.println(
"includeFullGC: " + includeFullGC);
303 out.println(
"nRuns: " + nRuns);
306 protected void writeProperties(
String fileName) {
307 File logger =
new File(fileName);
310 if (!logger.exists()) {
311 logger.createNewFile();
314 props.store(out,
"**** WARNING: DO NOT EDIT THIS FILE; IT IS GENERATED EACH RUN.");
315 }
catch (FileNotFoundException e) {
316 throw new RuntimeException(
"Unexpected exception opening file logger.properties.", e);
317 }
catch (IOException e) {
318 throw new RuntimeException(
"Unexpected exception writing file logger.properties.", e);
322 private void openLogFile() throws IOException {
324 out.println(
"writing results to file: " + logFileName);
325 log =
new PrintWriter(
new FileWriter(logFileName,
false));
329 private void closeLogFile() throws IOException {
331 out.print(
"closing files ...");
337 out.println(
" [ok]");
344 abstract protected void runTests() throws Exception;
346 protected
void clearLogBuffers() {
348 header =
new StringBuilder();
350 rtimes =
new StringBuilder();
353 musage =
new StringBuilder();
357 protected void writeLogBuffers(
String descr) {
359 log.println(descr +
", rtime[ms]"
360 + header.toString() + endl
361 + rtimes.toString() + endl);
364 log.println(descr +
", net musage[KiB]"
365 + header.toString() + endl
366 + musage.toString() + endl);
370 protected void begin(
String name) {
378 m0 = rt.totalMemory() - rt.freeMemory();
383 t0 = System.nanoTime() / 1000000;
387 protected void finish(
String name) {
395 t1 = System.nanoTime() / 1000000;
396 final long t = t1 - t0;
397 out.println(
"tx real time " + t
400 rtimes.append(
"\t" + t);
407 m1 = rt.totalMemory() - rt.freeMemory();
408 final long m0K = (m0 / 1024);
409 final long m1K = (m1 / 1024);
410 final long mK = m1K - m0K;
411 out.println(
"net mem usage "
412 + (mK >= 0 ?
"+" :
"") + mK
413 +
"\tKiB [" + m0K +
"K->" + m1K +
"K]");
414 musage.append(
"\t" + mK);
419 header.append(
"\t" + name);