📄 test2.java
字号:
package net.betterjava.design.refactor;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
/**
* test Set.add().
*/
public class Test2 {
private static String data[][];
private static int rpts[][] = { { 1000, 100 }, {
100, 2000 }, {
100, 4000 }, {
100, 10000 }, {
100, 20000 }
}; //the number of outer and inner loop
/**
* record one round test data
*/
private static void record(int currentTurn, int currentRound, String result) {
data[currentTurn][currentRound] = result;
}
/**
* a warming method act as running for JIT warm,
* the time of warming can be used to filter peak test
*/
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));
}
/**
* generate a csv file conatains each round's test data
* @param fw the csv file
* @throws IOException
*/
private static void genCSV(FileWriter fw) throws IOException {
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[i].length; j++) {
fw.write(data[i][j] + ",");
}
fw.write("\n");
}
fw.flush();
}
/**
* force garbage collector to run.
* @param mb the memory amount that prepare for next test
*/
private static 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);
}
}
private static void log(String msg) {
System.out.println(msg);
}
private static void log(Throwable e) {
e.printStackTrace(System.out);
}
/**
* contains the code to test.
* @param rpts the loop counter of test
* @param innerSet the test object
* @return long the test code running time
*/
private static long runTestBody(int rpts, Set innerSet) {
long elapse = System.currentTimeMillis();
for (int j = 0; j < rpts; j++) {
innerSet.add(String.valueOf(j));
}
return System.currentTimeMillis() - elapse;
}
public static void main(String[] args) {
Set hashSet = new HashSet();
Set treeSet = new TreeSet();
int turn = 5;
int round = 1;
int indexOfRoundRpts = 1;
data = new String[turn][round];
warming();
for (int currentTurn = 0; currentTurn < turn; currentTurn++) {
for (int currentRound = 0; currentRound < round; currentRound++) {
Set innerSet = new HashSet();
long elapse = 0;
for (int k = 0; k < rpts[currentTurn][currentRound]; k++) {
elapse += runTestBody(rpts[currentTurn][indexOfRoundRpts], innerSet);
}
String result =
String.valueOf(
elapse
* 1.0
/ (rpts[currentTurn][currentRound]
* rpts[currentTurn][indexOfRoundRpts]));
record(currentTurn, currentRound, result);
}
}
FileWriter fw = null;
try {
fw = new FileWriter("test.csv");
genCSV(fw);
} catch (IOException e) {
log(e);
} finally {
try {
fw.close();
} catch (IOException e) {
log(e);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -