The comparison might not be done with the required fairness. Consider that the stateless session&#39;s execute method implies a dispose() call, which you appear to be skipping in the stateful test code.<br><br>If your rules do not insert secondary facts and if you are able to clean up properly after processing one message, you won&#39;t see much difference. Also, major gains are only to be expected if you can run a stateless in sequential mode.<br>
<br>-W<br><br><div class="gmail_quote">On 29 July 2011 00:08, Ryan R. <span dir="ltr">&lt;<a href="mailto:ryanrolland@gmail.com">ryanrolland@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I have a usecase where I want to apply rules to messages that are received<br>
and processed one message at a time.<br>
<br>
I am thinking the stateless session matches this usecase. I was surprised<br>
though to notice that the stateless session seemed to perform upwards of 10x<br>
slower!<br>
<br>
I am including the below source which illustrates my usage. The DRL file<br>
used simply has one rule that does a simple modification on two fields.<br>
There is some test code above this stuff that just pushes messages into the<br>
plugin.<br>
<br>
I am also including VisualVM profiling results. The top results are for the<br>
stateful while the bottom are for the stateless. It looks like the stateless<br>
performance is dominated by calls to ReflectionInstantiator.newInstance()?<br>
<br>
StatelessKnowledgeSession Code:<br>
<br>
public class DataConditionPlugin implements Plugin {<br>
<br>
  final KnowledgeBuilder kbuilder;<br>
  final StatelessKnowledgeSession ksession;<br>
  public DataConditionPlugin(String drlFileName) {<br>
    kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();<br>
    // this will parse and compile in one step<br>
    kbuilder.add(<br>
        ResourceFactory.newClassPathResource(drlFileName,<br>
DataConditionPlugin.class),<br>
        ResourceType.DRL);<br>
<br>
    // Check the builder for errors<br>
    if (kbuilder.hasErrors()) {<br>
      System.out.println(kbuilder.getErrors().toString());<br>
      throw new RuntimeException(&quot;Unable to compile \&quot;&quot;+drlFileName+&quot;\&quot;.&quot;);<br>
    }<br>
<br>
    // get the compiled packages (which are serializable)<br>
    final Collection&lt;KnowledgePackage&gt; pkgs =<br>
kbuilder.getKnowledgePackages();<br>
<br>
    // add the packages to a knowledgebase (deploy the knowledge packages).<br>
    final KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();<br>
    kbase.addKnowledgePackages(pkgs);<br>
<br>
    ksession = kbase.newStatelessKnowledgeSession();<br>
  }<br>
<br>
  @Override<br>
  public Object execute(Object message) {<br>
    ksession.execute(message);<br>
    return message;<br>
  }<br>
}<br>
<br>
StatefulKnowledgeSession Code:<br>
<br>
public class DataConditionPlugin implements Plugin {<br>
<br>
  final KnowledgeBuilder kbuilder;<br>
  final StatefulKnowledgeSession ksession;<br>
  public DataConditionPlugin(String drlFileName) {<br>
    kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();<br>
    // this will parse and compile in one step<br>
    kbuilder.add(<br>
        ResourceFactory.newClassPathResource(drlFileName,<br>
DataConditionPlugin.class),<br>
        ResourceType.DRL);<br>
<br>
    // Check the builder for errors<br>
    if (kbuilder.hasErrors()) {<br>
      System.out.println(kbuilder.getErrors().toString());<br>
      throw new RuntimeException(&quot;Unable to compile \&quot;&quot;+drlFileName+&quot;\&quot;.&quot;);<br>
    }<br>
<br>
    // get the compiled packages (which are serializable)<br>
    final Collection&lt;KnowledgePackage&gt; pkgs =<br>
kbuilder.getKnowledgePackages();<br>
<br>
    // add the packages to a knowledgebase (deploy the knowledge packages).<br>
    final KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();<br>
    kbase.addKnowledgePackages(pkgs);<br>
<br>
    ksession = kbase.newStatefulKnowledgeSession();<br>
  }<br>
<br>
  protected void finalize() throws Throwable {<br>
    ksession.dispose();<br>
  };<br>
<br>
  @Override<br>
  public Object execute(Object message) {<br>
    FactHandle factHandler = ksession.insert(message);<br>
    ksession.fireAllRules();<br>
    Object o = ksession.getObject(factHandler);<br>
    ksession.retract(factHandler);<br>
<br>
    return o;<br>
  }<br>
}<br>
<br>
<a href="http://drools.46999.n3.nabble.com/file/n3208057/Screenshot-Java_VisualVM.png" target="_blank">http://drools.46999.n3.nabble.com/file/n3208057/Screenshot-Java_VisualVM.png</a><br>
<font color="#888888"><br>
--<br>
View this message in context: <a href="http://drools.46999.n3.nabble.com/Stateful-vs-Stateless-Session-Performance-tp3208057p3208057.html" target="_blank">http://drools.46999.n3.nabble.com/Stateful-vs-Stateless-Session-Performance-tp3208057p3208057.html</a><br>

Sent from the Drools: User forum mailing list archive at Nabble.com.<br>
_______________________________________________<br>
rules-users mailing list<br>
<a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
</font></blockquote></div><br>