performancetestsuite.java

来自「Mandarax是一个规则引擎的纯Java实现。它支持多类型的事实和基于反映的规」· Java 代码 · 共 138 行

JAVA
138
字号
/*
 * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package test.org.mandarax.performance;


import java.io.FileOutputStream;
import java.io.PrintStream;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Category;
import org.apache.log4j.Level;
import org.mandarax.kernel.InferenceEngine;
import org.mandarax.reference.ResolutionInferenceEngine;
import org.mandarax.reference.ResolutionInferenceEngine2;
import org.mandarax.reference.ResolutionInferenceEngine3;
import org.mandarax.reference.ResolutionInferenceEngine4;

/**
 * Executable performance test suite that writes data in a CSV file.
 * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
 * @version 3.4 <7 March 05>
 * @since 2.1
 */
public class PerformanceTestSuite {
	public static final String SEP = ",";
	public static final int REPEAT_TESTS = 5;
	/**
	 * Get the inference engines to be tested.
	 * @return an array of inference engines.
	 */
	public static InferenceEngine[] getIEs() {
		return new InferenceEngine[] {
			new ResolutionInferenceEngine(),
			new ResolutionInferenceEngine2(),
			new ResolutionInferenceEngine3(),
			new ResolutionInferenceEngine4()
		};
	}
	/**
	 * Get the output stream where to write results.
	 * @return an output stream
	 */
	public static PrintStream getOut4Statistics() {

		try {
			return new PrintStream(new FileOutputStream("performance.csv"));
		}
		catch (Exception x) {
			x.printStackTrace();
			return System.out;
		}
	}
	/**
	 * Get the performance tests to run.
	 * @return an output stream
	 */
	public static PerformanceTest[] getTests() {
		return new PerformanceTest[] {
			new PerformanceTest1(4), 
			new PerformanceTest1(8)
		};
	}
	/**
     * Run a performance test.
     */
    public static void main(String args[] ) {
    	BasicConfigurator.configure();
    	Category.getRoot ().setLevel (Level.WARN);
    	InferenceEngine[] IEs = getIEs();
    	PrintStream out = getOut4Statistics();
    	out.println("IE CLASS"+SEP+"test"+SEP+"cardinality"+SEP+"log"+SEP+"kb depth"+SEP+"kb size"+SEP+"results"+SEP+"time");
    	// one result
    	for (int i=0;i<IEs.length;i++) {
    		for (int k=0;k<REPEAT_TESTS;k++) {
	    		PerformanceTest[] tests = getTests();
	    		for (int j=0;j<tests.length;j++) {
	    			//runAndLog(tests[j],IEs[i],true,true,out);
	    			runAndLog(tests[j],IEs[i],true,false,out);
	    		}
    		}
    	}
    	// all results
    	for (int i=0;i<IEs.length;i++) {
    		for (int k=0;k<REPEAT_TESTS;k++) {
	    		PerformanceTest[] tests = getTests();
	    		if (!(IEs[i] instanceof ResolutionInferenceEngine)) {
		    		for (int j=0;j<tests.length;j++) {
		    			//runAndLog(tests[j],IEs[i],false,true,out);
		    			runAndLog(tests[j],IEs[i],false,false,out);
		    		}
	    		}
    		}
    	}
    	out.close();
    	System.out.println("performance test suite done");
    	System.exit(0);
    }
    /**
     * Run the performance test and add entry to csv.
     */
    private static void runAndLog(PerformanceTest test,InferenceEngine ie,boolean one,boolean log,PrintStream out) {
    	test.run(ie,one,log,System.out);
    	out.print(ie.getClass().getName());
    	out.print(SEP);
    	out.print(test);
    	out.print(SEP);
    	out.print(one?"one result":"all results");
    	out.print(SEP);
    	out.print(log?"on":"off");
    	out.print(SEP);
    	out.print(test.getDepth());
    	out.print(SEP);
    	out.print(test.getKBSize());
    	out.print(SEP);
    	out.print(test.getNumberOfResults());
    	out.print(SEP);
    	out.print(test.getTime());
    	out.println();
    	test.release();
    	
    }
}

⌨️ 快捷键说明

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