<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' 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;"> public static final void main(String[] args) {</div><div style="background-color: transparent;">
try {</div><div style="background-color: transparent;"> // load up the knowledge base</div><div style="background-color: transparent;"> KnowledgeBase kbase = readKnowledgeBase();</div><div style="background-color: transparent;"> // go !</div><div style="background-color: transparent;"> for (int i = 0; i < 20; i++) {</div><div style="background-color: transparent;"> <span class="Apple-tab-span" style="white-space:pre">        </span>Message message = new Message();</div><div style="background-color: transparent;"> <span class="Apple-tab-span" style="white-space:pre">        </span>message.setMessage("Hello World");</div><div style="background-color: transparent;"> <span
class="Apple-tab-span" style="white-space:pre">        </span>message.setStatus(Message.HELLO);</div><div style="background-color: transparent;"> <span class="Apple-tab-span" style="white-space:pre">        </span>long startTime = System.nanoTime();</div><div style="background-color: transparent;"> <span class="Apple-tab-span" style="white-space:pre">        </span>StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();</div><div style="background-color: transparent;"> <span class="Apple-tab-span" style="white-space:pre">        </span>FactHandle handle = ksession.insert(message);</div><div style="background-color: transparent;"> <span class="Apple-tab-span" style="white-space:pre">        </span>ksession.fireAllRules();</div><div style="background-color: transparent;">
<span class="Apple-tab-span" style="white-space:pre">        </span>ksession.retract(handle);</div><div style="background-color: transparent;"> <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;"> <span class="Apple-tab-span" style="white-space:pre">        </span>ksession.dispose();</div><div style="background-color: transparent;"> }</div><div style="background-color: transparent;"> } catch (Throwable t) {</div><div style="background-color: transparent;"> t.printStackTrace();</div><div style="background-color: transparent;"> }</div><div style="background-color: transparent;"> }</div><div
style="background-color: transparent;"><br></div><div style="background-color: transparent;"> private static KnowledgeBase readKnowledgeBase() throws Exception {</div><div style="background-color: transparent;"> KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();</div><div style="background-color: transparent;"> kbuilder.add(ResourceFactory.newClassPathResource("Sample.drl"), ResourceType.DRL);</div><div style="background-color: transparent;"> KnowledgeBuilderErrors errors = kbuilder.getErrors();</div><div style="background-color: transparent;"> if (errors.size() > 0) {</div><div style="background-color: transparent;"> for (KnowledgeBuilderError error: errors) {</div><div style="background-color: transparent;">
System.err.println(error);</div><div style="background-color: transparent;"> }</div><div style="background-color: transparent;"> throw new IllegalArgumentException("Could not parse knowledge.");</div><div style="background-color: transparent;"> }</div><div style="background-color: transparent;"> KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();</div><div style="background-color: transparent;"> kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());</div><div style="background-color: transparent;"> return kbase;</div><div style="background-color: transparent;"> }</div><div style="background-color: transparent;"><br></div><div style="background-color: transparent;"> public static class Message {</div><div
style="background-color: transparent;"><br></div><div style="background-color: transparent;"> public static final int HELLO = 0;</div><div style="background-color: transparent;"> public static final int GOODBYE = 1;</div><div style="background-color: transparent;"><br></div><div style="background-color: transparent;"> private String message;</div><div style="background-color: transparent;"><br></div><div style="background-color: transparent;"> private int status;</div><div style="background-color: transparent;"><br></div><div style="background-color: transparent;"> public String getMessage() {</div><div style="background-color: transparent;"> return this.message;</div><div style="background-color: transparent;"> }</div><div style="background-color:
transparent;"><br></div><div style="background-color: transparent;"> public void setMessage(String message) {</div><div style="background-color: transparent;"> this.message = message;</div><div style="background-color: transparent;"> }</div><div style="background-color: transparent;"><br></div><div style="background-color: transparent;"> public int getStatus() {</div><div style="background-color: transparent;"> return this.status;</div><div style="background-color: transparent;"> }</div><div style="background-color: transparent;"><br></div><div style="background-color: transparent;"> public void setStatus(int status) {</div><div style="background-color: transparent;"> this.status = status;</div><div
style="background-color: transparent;"> }</div><div style="background-color: transparent;"><br></div><div style="background-color: transparent;"> }</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: 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: 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? </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>