⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 batchtest3.java

📁 高质量Java程序设计 源代码
💻 JAVA
字号:
package net.betterjava.design.refactor;

import java.io.FileWriter;
import java.io.IOException;

import net.betterjava.util.DataUtil;

public abstract class BatchTest3 {

	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

	protected abstract String round(int turn, int round);

	public BatchTest3(int turn, int round, String file) {
		data = new String[turn][round];
		this.turn = turn;
		this.round = round;
		this.file = file;
		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();
		for (int i = 0; i < turn; i++)
			for (int j = 0; j < round; j++) {
				round(i, j);
			}

		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);
			}
		}

	}

	private 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) {
		System.out.println(msg);
	}

	protected void log(Throwable e) {
		e.printStackTrace(System.out);
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -