<html><body><div style="color:#000; background-color:#fff; font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:12pt"><div>Hi,</div><div><br></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: transparent; font-style: normal;">I have been trying to measure Drools' &nbsp;5.5.0 performance for quite some time. I see that Drools' perform inconsistently in terms of CPU time.</div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: transparent; font-style: normal;"><br></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: transparent; font-style: normal;">Please see the below code
 (modification of DroolsTest example):</div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: transparent; font-style: normal;"><br></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: transparent; font-style: normal;">/*Code start*/</div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: transparent; font-style: normal;"><br></div><div style="background-color: transparent;">public class DroolsTest {</div><div style="background-color: transparent;"><br></div><div style="background-color: transparent;">&nbsp; &nbsp; public static final void main(String[] args) {</div><div style="background-color: transparent;">&nbsp; &nbsp;
 &nbsp; &nbsp; try {</div><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // load up the knowledge base</div><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; KnowledgeBase kbase = readKnowledgeBase();</div><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // go !</div><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i &lt; 20; i++) {</div><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre">        </span>Message message = new Message();</div><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre">        </span>message.setMessage("Hello World");</div><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
 class="Apple-tab-span" style="white-space:pre">        </span>message.setStatus(Message.HELLO);</div><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre">        </span>long startTime = System.nanoTime();</div><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre">        </span>StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();</div><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre">        </span>FactHandle handle = ksession.insert(message);</div><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre">        </span>ksession.fireAllRules();</div><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
 <span class="Apple-tab-span" style="white-space:pre">        </span>ksession.retract(handle);</div><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre">        </span>System.out.println("Time taken is: " + (System.nanoTime()-startTime)/1000);</div><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre">        </span>ksession.dispose();</div><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; } catch (Throwable t) {</div><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t.printStackTrace();</div><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="background-color: transparent;">&nbsp; &nbsp; }</div><div
 style="background-color: transparent;"><br></div><div style="background-color: transparent;">&nbsp; &nbsp; private static KnowledgeBase readKnowledgeBase() throws Exception {</div><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();</div><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; kbuilder.add(ResourceFactory.newClassPathResource("Sample.drl"), ResourceType.DRL);</div><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; KnowledgeBuilderErrors errors = kbuilder.getErrors();</div><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; if (errors.size() &gt; 0) {</div><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (KnowledgeBuilderError error: errors) {</div><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
 System.err.println(error);</div><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new IllegalArgumentException("Could not parse knowledge.");</div><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();</div><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());</div><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; return kbase;</div><div style="background-color: transparent;">&nbsp; &nbsp; }</div><div style="background-color: transparent;"><br></div><div style="background-color: transparent;">&nbsp; &nbsp; public static class Message {</div><div
 style="background-color: transparent;"><br></div><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; public static final int HELLO = 0;</div><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; public static final int GOODBYE = 1;</div><div style="background-color: transparent;"><br></div><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; private String message;</div><div style="background-color: transparent;"><br></div><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; private int status;</div><div style="background-color: transparent;"><br></div><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; public String getMessage() {</div><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return this.message;</div><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="background-color:
 transparent;"><br></div><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; public void setMessage(String message) {</div><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.message = message;</div><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="background-color: transparent;"><br></div><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; public int getStatus() {</div><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return this.status;</div><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="background-color: transparent;"><br></div><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; public void setStatus(int status) {</div><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.status = status;</div><div
 style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="background-color: transparent;"><br></div><div style="background-color: transparent;">&nbsp; &nbsp; }</div><div style="background-color: transparent;">}</div><div>/** Code end<span style="font-size: 12pt;">**/</span></div><div><span style="font-size: 12pt;"><br></span></div><div>I have just added timing code to the DroolsTest.java program (provided by JBoss) and I am using Sample.drl file (provided by JBoss). The rules get executed correctly but the timings that I see are:</div><div><br></div><div>Iteration 1:&nbsp;103753 (we can ignore this as the classes get loaded during the first iteration)</div><div>Iteration 2: 861</div><div>Iteration 3: 744</div><div>Iteration 4: 1350<br></div><div>Iteration 5: 718<br></div><div>Iteration 6: 651<br></div><div>Iteration 7: 661<br></div><div>Iteration 8: 668<br></div><div>Iteration 9: 648<br></div><div>Iteration 10:
 3564<br></div><div>Iteration 11: 911<br></div><div><span class="Apple-tab-span" style="white-space:pre">                        </span>Iteration 12: 689</div><div>Iteration 13: 682<br></div><div>Iteration 14: 796<br></div><div>Iteration 15: 4236<br></div><div>Iteration 16: 774<br></div><div>Iteration 17: 772<br></div><div>Iteration 18: 722<br></div><div>Iteration 19: 713<br></div><div>Iteration 20: &nbsp;697</div><div><br></div><div>I profiled it with Yourkit Profiler and the increase in time does not correspond to garbage collection. I also timed individual calls and found out that:</div><div>- ksession.insert: takes consistent time</div><div>- ksession.fireAllRules: inconsistent</div><div>- kbase.newStatefulKnowledgeSession: inconsistent</div><div><br></div><div>I see same inconsistency in performance when I integrate Drools with original project (which I can't share here). I also found that this has nothing to do with compiler
 optimizations.</div><div><br></div><div><span style="font-size: 12pt;">Why is there inconsistent performance?&nbsp;</span>Is there anything that I am missing?<br></div><div><br></div><div>Thank you.</div><div><br></div><div><br></div><div><br></div></div></body></html>