[rules-users] Stateful vs. Stateless Session Performance

Ryan R. ryanrolland at gmail.com
Thu Jul 28 18:08:08 EDT 2011


I have a usecase where I want to apply rules to messages that are received
and processed one message at a time.

I am thinking the stateless session matches this usecase. I was surprised
though to notice that the stateless session seemed to perform upwards of 10x
slower!

I am including the below source which illustrates my usage. The DRL file
used simply has one rule that does a simple modification on two fields.
There is some test code above this stuff that just pushes messages into the
plugin.

I am also including VisualVM profiling results. The top results are for the
stateful while the bottom are for the stateless. It looks like the stateless
performance is dominated by calls to ReflectionInstantiator.newInstance()?

StatelessKnowledgeSession Code:

public class DataConditionPlugin implements Plugin {

  final KnowledgeBuilder kbuilder;  
  final StatelessKnowledgeSession ksession;
  public DataConditionPlugin(String drlFileName) {
    kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    // this will parse and compile in one step
    kbuilder.add(
        ResourceFactory.newClassPathResource(drlFileName,
DataConditionPlugin.class),
        ResourceType.DRL);

    // Check the builder for errors
    if (kbuilder.hasErrors()) {
      System.out.println(kbuilder.getErrors().toString());
      throw new RuntimeException("Unable to compile \""+drlFileName+"\".");
    }
    
    // get the compiled packages (which are serializable)
    final Collection<KnowledgePackage> pkgs =
kbuilder.getKnowledgePackages();
    
    // add the packages to a knowledgebase (deploy the knowledge packages).
    final KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(pkgs);

    ksession = kbase.newStatelessKnowledgeSession();
  }
    
  @Override
  public Object execute(Object message) {
    ksession.execute(message);
    return message;
  }
}

StatefulKnowledgeSession Code:

public class DataConditionPlugin implements Plugin {

  final KnowledgeBuilder kbuilder;  
  final StatefulKnowledgeSession ksession;
  public DataConditionPlugin(String drlFileName) {
    kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    // this will parse and compile in one step
    kbuilder.add(
        ResourceFactory.newClassPathResource(drlFileName,
DataConditionPlugin.class),
        ResourceType.DRL);

    // Check the builder for errors
    if (kbuilder.hasErrors()) {
      System.out.println(kbuilder.getErrors().toString());
      throw new RuntimeException("Unable to compile \""+drlFileName+"\".");
    }
    
    // get the compiled packages (which are serializable)
    final Collection<KnowledgePackage> pkgs =
kbuilder.getKnowledgePackages();
    
    // add the packages to a knowledgebase (deploy the knowledge packages).
    final KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(pkgs);

    ksession = kbase.newStatefulKnowledgeSession();
  }
  
  protected void finalize() throws Throwable {
    ksession.dispose();
  };
  
  @Override
  public Object execute(Object message) {
    FactHandle factHandler = ksession.insert(message);
    ksession.fireAllRules();
    Object o = ksession.getObject(factHandler);
    ksession.retract(factHandler);
    
    return o;
  }
}

http://drools.46999.n3.nabble.com/file/n3208057/Screenshot-Java_VisualVM.png 

--
View this message in context: http://drools.46999.n3.nabble.com/Stateful-vs-Stateless-Session-Performance-tp3208057p3208057.html
Sent from the Drools: User forum mailing list archive at Nabble.com.



More information about the rules-users mailing list