public static void main(final String[] args) throws Exception {
try {
PackageBuilder builder = new PackageBuilder();
builder.addPackageFromDrl( new InputStreamReader( WaltzBenchmark.class.getResourceAsStream( "xxx.drl" ) ) );
Package pkg = builder.getPackage();
final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
ruleBase.addPackage( pkg );
WorkingMemory workingMemory = ruleBase.newWorkingMemory();
final PrintStream out = new PrintStream(new BufferedOutputStream( new FileOutputStream( "stats.log" ) ) );
workingMemory.addEventListener( new DefaultAgendaEventListener() {
public void activationCancelled(ActivationCancelledEvent activation) {
String name = activation.getActivation().getRule().getName();
Integer integer = ( Integer ) cancelled.get( name );
if ( integer == null ) {
integer = new Integer( 0 );
}
integer = new Integer( integer.intValue() + 1 );
cancelled.put( name, integer );
}
public void activationCreated(ActivationCreatedEvent activation) {
String name = activation.getActivation().getRule().getName();
Integer integer = ( Integer ) created.get( name );
if ( integer == null ) {
integer = new Integer( 0 );
}
integer = new Integer( integer.intValue() + 1 );
created.put( name, integer );
}
public void afterActivationFired(AfterActivationFiredEvent activation) {
String name = activation.getActivation().getRule().getName();
Integer integer = ( Integer ) fired.get( name );
if ( integer == null ) {
integer = new Integer( 0 );
}
integer = new Integer( integer.intValue() + 1 );
fired.put( name, integer );
out.println( timings.getRule() + " : " + ( System.currentTimeMillis() - timings.getTime() ) );
timings.setRule( name );
timings.setTime( System.currentTimeMillis() );
}
} );
long start = System.currentTimeMillis();
workingMemory.fireAllRules();
System.out.println( (System.currentTimeMillis() - start) / 1000 );
out.println("-------------------");
out.println( "created" );
int total = 0;
for ( Iterator it = created.entrySet().iterator(); it.hasNext(); ) {
Entry entry = ( Entry ) it.next();
out.println( entry.getKey() + " : " + entry.getValue() );
total = total + ( ( Integer )entry.getValue() ).intValue();
}
out.println( "total : " + total );
out.println("-------------------");
out.println( "cancelled" );
total = 0;
for ( Iterator it = cancelled.entrySet().iterator(); it.hasNext(); ) {
Entry entry = ( Entry ) it.next();
out.println( entry.getKey() + " : " + entry.getValue() );
total = total + ( ( Integer )entry.getValue() ).intValue();
}
out.println( "total : " + total );
out.println("-------------------");
out.println( "fired" );
total = 0;
for ( Iterator it = fired.entrySet().iterator(); it.hasNext(); ) {
Entry entry = ( Entry ) it.next();
out.println( entry.getKey() + " : " + entry.getValue() );
total = total + ( ( Integer )entry.getValue() ).intValue();
}
out.println( "total : " + total );
out.close();
} catch (Throwable t) {
t.printStackTrace();
}
}
static class RuleTimings {
String rule;
long time;
public String getRule() {
return this.rule;
}
public void setRule(String rule) {
this.rule = rule;
}
public long getTime() {
return this.time;
}
public void setTime(long time) {
this.time = time;
}
}
Referenced by:
Comments