[rules-users] Stateful vs. Stateless Session Performance

Wolfgang Laun wolfgang.laun at gmail.com
Fri Jul 29 04:08:43 EDT 2011


The comparison might not be done with the required fairness. Consider that
the stateless session's execute method implies a dispose() call, which you
appear to be skipping in the stateful test code.

If your rules do not insert secondary facts and if you are able to clean up
properly after processing one message, you won't see much difference. Also,
major gains are only to be expected if you can run a stateless in sequential
mode.

-W

On 29 July 2011 00:08, Ryan R. <ryanrolland at gmail.com> wrote:

> 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.
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20110729/48db6e46/attachment.html 


More information about the rules-users mailing list