📄 abstractmemorytest.java
字号:
/*
* 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 org.mandarax.examples.family.performance;
// import org.apache.log4j.*;
import java.util.*;
import org.mandarax.kernel.*;
import org.mandarax.reference.AbstractResolutionInferenceEngine;
import org.mandarax.reference.AdvancedKnowledgeBase;
import org.mandarax.reference.ResolutionInferenceEngine3;
import org.mandarax.util.LogicFactorySupport;
/**
* Abstract stress test class used to check for OutOfMemory errors.
* It appears that the OutOfMemory error only occurs when logging is set to DEBUG !!
* (version : 3.2)
* @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
* @version 3.4 <7 March 05>
* @since 3.2.1
*/
public abstract class AbstractMemoryTest {
public static final String FAMILY_ROOT = "root";
public static final int NUMBER_OF_SONS = 50;
public static final int NUMBER_OF_GRANDCHILDREN_PER_SON = 3;
/**
* Run the test.
*/
public void test() throws Exception {
// disable deep level logging
// BasicConfigurator.configure();
// replaceLevel.DEBUG by Level.INFO and the OutOfMemoryError will disappear
// Category.getRoot().setLevel(Level.INFO);
configureLogging();
System.out.println("Run memory profiling test " + this);
// get kb
KnowledgeBase kb = createKB();
// issue query
AbstractResolutionInferenceEngine ie = new ResolutionInferenceEngine3();
ie.setMaxNumberOfProofSteps(100000);
Query query = kb.getQuery("query");
long freeMem = Runtime.getRuntime().freeMemory();
List values = new ArrayList();
long old = freeMem;
long minValue = -1;
long maxValue = 1;
for (int i=0;i<100;i++) {
try {
run(ie,query,kb,false);
old = freeMem;
freeMem = Runtime.getRuntime().freeMemory();
values.add(new Long(freeMem));
// compute average, max und min
if (minValue==-1) minValue=freeMem;
if (maxValue==-1) maxValue=freeMem;
minValue = Math.min(minValue,freeMem);
maxValue = Math.max(maxValue,freeMem);
long avg = 0;
for (int j=0;j<values.size();j++) avg = avg + ((Long)values.get(j)).longValue();
avg = avg / values.size();
// print report
this.logMemSnapshot(i,freeMem,old,minValue,maxValue,avg);
}
catch (Throwable t) {
t.printStackTrace();
}
}
System.out.println("Computation finished !");
System.exit(0);
}
/**
* Log a memory snapshot.
* @param i the computation step
* @param freeMem the free memory
* @param oldValue the last free memory value
* @param min the min value from all snapshots in this series
* @param max the max value from all snapshots in this series
* @param avgValue the average value from all snapshots in this series
*/
private void logMemSnapshot (int i,long freeMem, long oldValue, long minValue,long maxValue,long avgValue) {
// print report
System.out.println("run no " + i);
System.out.println("free memory " + freeMem);
System.out.println("prev free memory was " + oldValue);
System.out.println("min free memory was " + minValue);
System.out.println("max free memory was " + maxValue);
System.out.println("avg free memory was " + avgValue);
}
/**
* Configure logging - logging is critical!!
*/
public abstract void configureLogging();
/**
* Run and time the computation.
*/
private static void run(InferenceEngine ie,Query q,KnowledgeBase kb,boolean logResults) {
try {
long before = System.currentTimeMillis();
ResultSet rs = ie.query(q,kb,InferenceEngine.ALL,InferenceEngine.BUBBLE_EXCEPTIONS);
long after = System.currentTimeMillis();
// count results
int solutions = 0;
while (rs.next()) {
solutions = solutions+1;
if (logResults) {
System.out.print(rs.getResult(String.class,"x"));
System.out.print(" is brother of ");
System.out.println(rs.getResult(String.class,"y"));
}
}
// report
System.out.println("solutions found: " + solutions);
System.out.println("time (in millis): " + (after-before));
}
catch (Exception x) {
x.printStackTrace();
}
}
/**
* Set up the knowledge base.
* @return the knowledge base.
*/
private static KnowledgeBase createKB() {
LogicFactorySupport lfs = new LogicFactorySupport();
// init predicates
Class[] struct = { String.class, String.class };
Predicate predicate_is_father = new SimplePredicate("father", struct);
Predicate predicate_is_brother = new SimplePredicate("brother", struct);
// add knowledge - rule
KnowledgeBase kb = new AdvancedKnowledgeBase();
Rule rule =
lfs.rule(
lfs.prereq(predicate_is_father, lfs.variable("s"), lfs.variable("f")),
lfs.prereq(predicate_is_father, lfs.variable("t"), lfs.variable("f")),
lfs.prereq(org.mandarax.lib.text.StringArithmetic.NOT_EQUAL, lfs.variable("s"), lfs.variable("t")),
lfs.fact(predicate_is_brother, lfs.variable("s"), lfs.variable("t")));
kb.add(rule);
// add facts for family tree
for (int i=0;i<NUMBER_OF_SONS;i++) {
kb.add(
lfs.fact(
predicate_is_father,
lfs.cons(""+i,String.class),
lfs.cons(FAMILY_ROOT,String.class)
)
);
for (int j=0;j<NUMBER_OF_GRANDCHILDREN_PER_SON;j++) {
kb.add(
lfs.fact(
predicate_is_father,
lfs.cons(""+i+"."+j,String.class),
lfs.cons(""+i,String.class)
)
);
}
}
// add query
kb.addQuery(
lfs.query(
lfs.fact(
predicate_is_brother,
lfs.variable("x",String.class),
lfs.variable("y",String.class)
),
"query"
)
);
// return
return kb;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -