parsec2.1基准测试程序统计结果抽取及导入excel

问题来源:由于parsec的每个基准测试程序运行后,都会生成stats.txt文件,为了便于查看和对比sim_seconds,host_seconds等信息,最终将结果导入到excel中,在excel中便可通过图表等形式查看各个指标的对比结果。


参考:Excel生成的方法


数据抽取的代码如下:


package datatools;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

import jxl.write.WriteException;

public class DataExtract {

	public static void main(String[] args) throws IOException {
		String filepath = "C:/Users/fandroid/Desktop/testdata"; //测试集统计结果目录
		String targetfile = "stats.txt"; //待抽取数据的文件名
		String[] keywords = {"sim_seconds", "host_seconds"}; //统计包含此参数的数据,可统计多行数据
		int region = 1; //statistics region eg: Begin/End pair
		String outputpath = "F:/sram.xls"; //output excel path
		String sheetname = "sram";
		
		DataExtract de = new DataExtract();
		double[][] datas = de.extract(keywords, filepath, targetfile, region);
		
		for(int i=0;i<datas.length;i++) {
			for(int j=0;j<datas[i].length;j++) {
				System.out.print(datas[i][j] + "   ");
			}
			System.out.println();
		}
		
		//output data into excel
		File filewrite = new File(outputpath);
		filewrite.createNewFile();
		OutputStream os = new FileOutputStream(filewrite);
		ExcelData me = new ExcelData();
		try {
			me.createExcel(os, sheetname, keywords, datas);
		} catch (WriteException e) {
			e.printStackTrace();
		}
	}
	
	/*
	 * Used to extract the specific statistics of the keywords
	 * @param keywords
	 * the specific info you want to check
	 * @param filepath
	 * where the whole benchmark is
	 * @param targetfile
	 * the specific file you want to extract. eg: stats.txt
	 * @param region
	 * which region you want to extraxt? eg: Begin/End pair
	 */
	public double[][] extract(String keywords[], String filepath, String targetfile, int region) throws IOException {
		BufferedReader br = null;
		String s = null;
		int j = 0;
		DataExtract de = new DataExtract();
		Map<String, String> map = de.getFile(filepath, targetfile);
		Iterator iter = map.keySet().iterator();
		double[][] darray = new double[keywords.length][map.size()];

		
		while(iter.hasNext()) {  //遍历单个基准程序,分别统计每个基准程序中相应的字段keywords
			int b = 0; //region counter
			Object key = iter.next();
			String dir = map.get(key);
			br = new BufferedReader(new FileReader(new File(dir)));
			
			while((s = br.readLine()) != null) {
				if(s.contains("Begin Simulation Statistics")) {
					b++;
				}
				if(b == region) {                  //have found the statistic region
					for(int i = 0; i<keywords.length; i++) {
						if(s.contains(keywords[i])) {
							String t = s.substring(s.indexOf(0x0020)).trim(); //abandon the start info of the line
							double data = Double.parseDouble(t.substring(0, t.indexOf(0x0020))); //get the double data
							darray[i][j] = data;
							//System.out.println(data);
						}
					}
				}
			}
			
			j++;
			br.close();
		}
		return darray;
	}
	
	/*
	 * 用于读取某个路径下的所有测试集,然后返回测试集与对应统计数据的路径 like <"01","d:/01black/stats.txt">
	 */
	public Map<String, String> getFile(String filepath, String targetfile) {
		Map<String, String> map = new LinkedHashMap<String, String>();
		
		File dir = new File(filepath);
		File[] files = dir.listFiles();
		for (int i = 0; i < files.length; i++) {
			map.put(files[i].getName().substring(0, 2), files[i].getAbsolutePath() + "\\" + targetfile);
		}
		
		return map;
	}

}

生成excel的代码如下:


package datatools;

import java.io.*;
import java.util.Calendar;
import java.util.Date;

import jxl.Workbook;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.write.Boolean;
import jxl.write.DateFormats;
import jxl.write.DateTime;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

public class ExcelData {
	//根据keywords按行抽取数据,生成每行keywords对应的数据
	public void createExcelByRow(OutputStream os, String sheetname, String keywords[], double[][] datas) throws WriteException, IOException {
		//创建工作薄
        WritableWorkbook workbook = Workbook.createWorkbook(os);
        //创建新的一页
        WritableSheet sheet = workbook.createSheet(sheetname, 0);
        //创建parsec2.1测试用例集
        Label formate = new Label(0,0,"TestCase");
        sheet.addCell(formate);
        Label black01 = new Label(1,0,"blackscholes");
        sheet.addCell(black01);
        Label body02 = new Label(2,0,"bodytrack");
        sheet.addCell(body02);
        Label canneal03 = new Label(3,0,"canneal");
        sheet.addCell(canneal03);
        Label dedup04 = new Label(4,0,"dedup");
        sheet.addCell(dedup04);
        Label facesim05 = new Label(5,0,"facesim");
        sheet.addCell(facesim05);
        Label ferret06 = new Label(6,0,"ferret");
        sheet.addCell(ferret06);
        Label fluid07 = new Label(7,0,"fluidanimate");
        sheet.addCell(fluid07);
        Label freq08 = new Label(8,0,"freqmine");
        sheet.addCell(freq08);
        Label stream09 = new Label(9,0,"streamcluster");
        sheet.addCell(stream09);
        Label swap10 = new Label(10,0,"swaptions");
        sheet.addCell(swap10);
        Label vips11 = new Label(11,0,"vips");
        sheet.addCell(vips11);
        Label x26412 = new Label(12,0,"x264");
        sheet.addCell(x26412);
        
        for(int i=0; i<keywords.length; i++) {
        	Label word = new Label(0, i+1, keywords[i]);
            sheet.addCell(word);
        }
        
        for(int i=0;i<datas.length;i++) {
			for(int j=0;j<datas[i].length;j++) {
				Number number = new Number(j+1, i+1, datas[i][j]);
				sheet.addCell(number);
			}
		}
        
        //把创建的内容写入到输出流中,并关闭输出流
        workbook.write();
        workbook.close();
        os.close();
	}
	
	//根据keywords按列抽取数据,生成每行keywords对应的数据
	public void createExcelByColumn(OutputStream os, String sheetname, String keywords[], double[][] datas) throws WriteException, IOException {
		//创建工作薄
        WritableWorkbook workbook = Workbook.createWorkbook(os);
        //创建新的一页
        WritableSheet sheet = workbook.createSheet(sheetname, 0);
        //创建parsec2.1测试用例集
        Label formate = new Label(0,0,"TestCase");
        sheet.addCell(formate);
        Label black01 = new Label(0,1,"blackscholes");
        sheet.addCell(black01);
        Label body02 = new Label(0,2,"bodytrack");
        sheet.addCell(body02);
        Label canneal03 = new Label(0,3,"canneal");
        sheet.addCell(canneal03);
        Label dedup04 = new Label(0,4,"dedup");
        sheet.addCell(dedup04);
        Label facesim05 = new Label(0,5,"facesim");
        sheet.addCell(facesim05);
        Label ferret06 = new Label(0,6,"ferret");
        sheet.addCell(ferret06);
        Label fluid07 = new Label(0,7,"fluidanimate");
        sheet.addCell(fluid07);
        Label freq08 = new Label(0,8,"freqmine");
        sheet.addCell(freq08);
        Label stream09 = new Label(0,9,"streamcluster");
        sheet.addCell(stream09);
        Label swap10 = new Label(0,10,"swaptions");
        sheet.addCell(swap10);
        Label vips11 = new Label(0,11,"vips");
        sheet.addCell(vips11);
        Label x26412 = new Label(0,12,"x264");
        sheet.addCell(x26412);
        
        for(int i=0; i<keywords.length; i++) {
        	Label word = new Label(i+1, 0, keywords[i]);
            sheet.addCell(word);
        }
        
        for(int i=0;i<datas.length;i++) {
			for(int j=0;j<datas[i].length;j++) {
				Number number = new Number(i+1, j+1, datas[i][j]);
				sheet.addCell(number);
			}
		}
        
        //把创建的内容写入到输出流中,并关闭输出流
        workbook.write();
        workbook.close();
        os.close();
	}
	
}



抽取数据后的运行效果如下:








你可能感兴趣的:(java,stats,GEM5)