19 package com.mysql.clusterj.jpatest;
21 import java.util.Properties;
22 import java.util.List;
24 import java.util.HashSet;
25 import java.util.ArrayList;
26 import java.util.Date;
27 import java.text.SimpleDateFormat;
29 import java.io.FileInputStream;
30 import java.io.FileWriter;
31 import java.io.IOException;
32 import java.io.PrintWriter;
33 import java.io.InputStream;
60 static protected final PrintWriter
out =
new PrintWriter(System.out,
true);
65 static protected final PrintWriter
err =
new PrintWriter(System.err,
true);
70 static protected final String endl = System.getProperty(
"line.separator");
75 static private final Runtime rt = Runtime.getRuntime();
78 static private final List<String> propFileNames =
new ArrayList<String>();
79 static private String logFileName
81 +
new SimpleDateFormat(
"yyyyMMdd_HHMMss").format(
new Date())
85 private PrintWriter log;
88 protected final Properties props =
new Properties();
89 protected String descr =
"";
90 protected boolean logRealTime =
false;
91 protected boolean logMemUsage =
false;
92 protected boolean includeFullGC =
false;
93 protected boolean logSumOfOps =
false;
94 protected boolean renewOperations =
false;
95 protected boolean renewConnection =
false;
96 protected boolean allowExtendedPC =
false;
97 protected int aStart = (1 << 8), aEnd = (1 << 12), aIncr = (1 << 2);
98 protected int bStart = (1 << 8), bEnd = (1 << 12), bIncr = (1 << 2);
99 protected int maxStringLength = 100;
100 protected int warmupRuns = 0;
101 protected int hotRuns = 0;
102 protected final Set<String> exclude =
new HashSet<String>();
109 protected abstract class Op {
110 final protected String name;
120 public abstract void run(
int countA,
int countB)
throws Exception;
128 protected final List<Op>
ops =
new ArrayList<Op>();
132 private StringBuilder header;
133 private StringBuilder rtimes;
134 private StringBuilder musage;
137 private long t0 = 0, t1 = 0, ta = 0;
138 private long m0 = 0, m1 = 0, ma = 0;
141 abstract protected void initConnection() throws Exception;
142 abstract protected
void closeConnection() throws Exception;
143 abstract protected
void initOperations() throws Exception;
144 abstract protected
void closeOperations() throws Exception;
145 abstract protected
void clearPersistenceContext() throws Exception;
146 abstract protected
void clearData() throws Exception;
147 abstract protected
void beginTransaction() throws Exception;
148 abstract protected
void commitTransaction() throws Exception;
149 abstract protected
void rollbackTransaction() throws Exception;
157 static protected final
void verify(
boolean cond) {
160 throw new RuntimeException(
"wrong data; verification failed");
167 out.print(
"loading libary ...");
170 System.loadLibrary(name);
171 }
catch (UnsatisfiedLinkError e) {
174 path = System.getProperty(
"java.library.path");
175 }
catch (Exception ex) {
176 path =
"<exception caught: " + ex.getMessage() +
">";
178 err.println(
"NdbBase: failed loading library '"
179 + name +
"'; java.library.path='" + path +
"'");
181 }
catch (SecurityException e) {
182 err.println(
"NdbBase: failed loading library '"
183 + name +
"'; caught exception: " + e);
186 out.println(
" [" + name +
"]");
199 for (
int i = 0;
i < warmupRuns;
i++)
204 out.println(
"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
205 out.println(
"start logging results ...");
206 out.println(
"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
208 header =
new StringBuilder();
209 rtimes =
new StringBuilder();
210 musage =
new StringBuilder();
215 for (
int i = 0;
i < hotRuns;
i++)
220 log.println(descr +
", rtime[ms]"
221 + header.toString() +
endl
225 log.println(descr +
", net musage[KiB]"
226 + header.toString() +
endl
231 }
catch (Exception ex) {
233 out.println(
"caught " + ex);
234 ex.printStackTrace();
242 protected void init() throws Exception {
250 header =
new StringBuilder();
251 rtimes =
new StringBuilder();
252 musage =
new StringBuilder();
258 protected void close() throws Exception {
273 private void loadProperties() throws IOException {
274 if (propFileNames.size() == 0) {
275 propFileNames.add(
"crund.properties");
279 for (
String fn : propFileNames) {
280 out.println(
"reading properties file: " + fn);
281 InputStream is = null;
285 }
catch (Exception e) {
286 out.println(
"error reading file.");
298 return Boolean.parseBoolean(props.getProperty(k));
306 final String v = props.getProperty(k);
308 return (v == null ? vdefault : Integer.parseInt(v));
309 }
catch (NumberFormatException e) {
310 final NumberFormatException nfe =
new NumberFormatException(
311 "invalid value of benchmark property ('" + k +
"', '"
330 aStart =
parseInt(
"aStart", 1 << 8);
333 bStart =
parseInt(
"bStart", 1 << 8);
336 maxStringLength =
parseInt(
"maxStringLength", 100);
337 warmupRuns =
parseInt(
"warmupRuns", 0);
341 final String[] e = props.getProperty(
"exclude",
"").split(
",");
342 for (
int i = 0;
i < e.length;
i++) {
353 out.println(
"main settings:");
354 out.println(
"logRealTime: " + logRealTime);
355 out.println(
"logMemUsage: " + logMemUsage);
356 out.println(
"includeFullGC: " + includeFullGC);
357 out.println(
"logSumOfOps: " + logSumOfOps);
358 out.println(
"renewOperations: " + renewOperations);
359 out.println(
"renewConnection: " + renewConnection);
360 out.println(
"allowExtendedPC: " + allowExtendedPC);
361 out.println(
"aStart: " + aStart);
362 out.println(
"aEnd: " + aEnd);
363 out.println(
"aIncr: " + aIncr);
364 out.println(
"bStart: " + bStart);
365 out.println(
"bEnd: " + bEnd);
366 out.println(
"bIncr: " + bIncr);
367 out.println(
"maxStringLength: " + maxStringLength);
368 out.println(
"warmupRuns: " + warmupRuns);
369 out.println(
"hotRuns: " + hotRuns);
370 out.println(
"exclude: " + exclude);
376 private void openLogFile() throws IOException {
378 out.println(
"writing results to file: " + logFileName);
379 log =
new PrintWriter(
new FileWriter(logFileName,
false));
385 private void closeLogFile() throws IOException {
386 out.print(
"closing files ... ");
392 out.println(
" [ok]");
404 for (
int i = aStart;
i <= aEnd;
i *= aIncr) {
406 for (
int j = (
i > bStart ?
i : bStart); j <= bEnd; j *= bIncr) {
409 }
catch (Exception ex) {
418 out.println(
"------------------------------------------------------------");
431 out.println(
"------------------------------------------------------------");
432 out.println(
"countA = " + countA +
", countB = " + countB);
437 rtimes.append(
"A=" + countA +
", B=" + countB);
441 musage.append(
"A=" + countA +
", B=" + countB);
446 if (renewConnection) {
451 }
else if (renewOperations) {
460 if (!allowExtendedPC) {
463 clearPersistenceContext();
465 runOp(op, countA, countB);
469 header.append(
"\ttotal");
476 rtimes.append(
"\t" + ta);
478 out.println(
"total");
479 out.println(
"tx real time = " + ta +
"\tms [begin..commit]");
485 musage.append(
"\t" + ma);
487 out.println(
"total");
488 out.println(
"net mem usage = " + (ma >= 0 ?
"+" :
"") + ma
498 protected void runOp(
Op op,
int countA,
int countB)
throws Exception {
500 if (!exclude.contains(name)) {
502 op.run(countA, countB);
518 m0 = rt.totalMemory() - rt.freeMemory();
523 t0 = System.nanoTime() / 1000000;
542 t1 = System.nanoTime() / 1000000;
543 final long t = t1 - t0;
544 out.println(
"tx real time = " + t +
"\tms [begin..commit]");
546 rtimes.append(
"\t" + t);
553 m1 = rt.totalMemory() - rt.freeMemory();
554 final long m0K = (m0 / 1024);
555 final long m1K = (m1 / 1024);
556 final long mK = m1K - m0K;
557 out.println(
"net mem usage = " + (mK >= 0 ?
"+" :
"") + mK
558 +
"\tKiB [" + m0K +
"K->" + m1K +
"K]");
567 musage.append(
"\t" + mK);
572 header.append(
"\t" +
name);
578 static private void gc() {
582 final int nFullGCs = 10;
583 for (
int i = 0;
i < nFullGCs;
i++) {
586 long newfree = rt.freeMemory();
589 rt.runFinalization();
591 newfree = rt.freeMemory();
593 }
while (newfree > oldfree);
603 static private void exitUsage() {
604 out.println(
"usage: [options]");
605 out.println(
" [-p <file name>]... a properties file name");
606 out.println(
" [-l <file name>] log file name for data output");
607 out.println(
" [-h|--help] print usage message and exit");
616 for (
int i = 0;
i < args.length;
i++) {
618 if (arg.equals(
"-p")) {
619 if (
i >= args.length) {
622 propFileNames.add(args[++
i]);
623 }
else if (arg.equals(
"-l")) {
624 if (
i >= args.length) {
627 logFileName = args[++
i];
628 }
else if (arg.equals(
"-h") || arg.equals(
"--help")) {
631 out.println(
"unknown option: " + arg);
641 propFileNames.clear();