📄 batchtest.java
字号:
package net.betterjava.util;
import java.io.FileWriter;
import java.io.IOException;
public abstract class BatchTest {
private String data[][]; //test result data holder
private int turn; //total turn of test
private int round; //total round of every turn
private String file; //the test result file path and name
private boolean logOn = true;
protected abstract String round(int turn, int round);
public BatchTest(int turn, int round, String file) {
data = new String[turn][round];
this.turn = turn;
this.round = round;
this.file = file;
log("test " + file);
}
public BatchTest(int turn, int round) {
data = new String[turn][round];
this.turn = turn;
this.round = round;
String s = this.getClass().getName();
s = s.substring(s.lastIndexOf(".") + 1);
this.file = s + System.getProperty("java.vm.version") + ".CSV";
log("test " + file);
}
private void record(int currentTurn, int currentRound, String data) {
if (turn > this.data.length
|| round > this.data[0].length
|| turn < 0
|| round < 0) {
throw new IllegalArgumentException(
"wrong value of turn or round:turn "
+ String.valueOf(turn)
+ " round "
+ String.valueOf(round));
}
this.data[currentTurn][currentRound] = data;
}
private static void warming() {
long time = System.currentTimeMillis();
int temp = 0;
for (int i = 0; i < 8000; i++) {
temp = 0;
for (int j = 0; j < 10000; j++) {
temp++;
}
}
System.out.println("warming time: " + (System.currentTimeMillis() - time));
}
public void run() {
//run to warm JIT
warming();
setLogOff();
for (int i = 0; i < turn; i++)
for (int j = 0; j < round; j++) {
round(i, j);
}
setLogOn();
log(turn + " turn " + round + " round ");
for (int i = 0; i < turn; i++)
for (int j = 0; j < round; j++) {
log("turn " + i + " round " + j);
fullGC(32);
log("free memory " + Runtime.getRuntime().freeMemory());
String result = round(i, j);
log("free memory " + Runtime.getRuntime().freeMemory());
record(i, j, result);
}
FileWriter fw = null;
try {
fw = new FileWriter(file);
genCSV(fw);
} catch (IOException e) {
log(e);
} finally {
try {
fw.close();
} catch (IOException e) {
log(e);
}
}
}
public void genCSV(FileWriter fw) throws IOException {
DataUtil.writeCSV(data, fw);
}
private void fullGC(int mb) {
byte[][] memory = new byte[mb * 2][];
int count = 0;
try {
for (int i = 0; i < mb * 2; i++) {
memory[i] = new byte[1024 * 1024];
}
} catch (OutOfMemoryError e) {
//log("out of memeory at " + new Date(System.currentTimeMillis()));
}
memory = null;
Runtime.getRuntime().gc();
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
log(e);
}
}
protected void log(String msg) {
if (logOn)
System.out.println(msg);
}
protected void log(Throwable e) {
if (logOn)
e.printStackTrace(System.out);
}
protected void setLogOn() {
logOn = true;
}
protected void setLogOff() {
logOn = false;
}
protected void setRound(int round) {
this.round = round;
}
protected void setTurn(int turn) {
this.turn = turn;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -