Uploaded image for project: 'JBRULES'
  1. JBRULES
  2. JBRULES-1056

Performance of RuleBase().addPackage(.) goes down on sub sequent instances of RuleBase with large number of packages.

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed (View Workflow)
    • Priority: Major
    • Resolution: Done
    • Affects Version/s: 4.0.0.GA
    • Fix Version/s: 4.0.1
    • Component/s: drools-compiler
    • Labels:
      None
    • Environment:

      Windows

      Description

      I encountered a strange behavior when trying to test the performance of rule registration, meaning to add Package's to a new instance of RuleBase. I ran the same test 5 times in a sequence, and the RuleBase instance was discarded right away. The test ran with different different number of packages, 20, 50, 100, and 200. The results are as the followings:

      Added 20 packages to rulebase in 0:00:0.15
      Added 20 packages to rulebase in 0:00:0.21
      Added 20 packages to rulebase in 0:00:0.21
      Added 20 packages to rulebase in 0:00:0.301
      Added 20 packages to rulebase in 0:00:0.18
      Added 20 packages to rulebase 5 times in 0:00:1.051

      Added 50 packages to rulebase in 0:00:0.17
      Added 50 packages to rulebase in 0:00:1.422
      Added 50 packages to rulebase in 0:00:1.252
      Added 50 packages to rulebase in 0:00:1.322
      Added 50 packages to rulebase in 0:00:1.331
      Added 50 packages to rulebase 5 times in 0:00:5.497

      Added 100 packages to rulebase in 0:00:0.19
      Added 100 packages to rulebase in 0:00:5.278
      Added 100 packages to rulebase in 0:00:5.297
      Added 100 packages to rulebase in 0:00:5.228
      Added 100 packages to rulebase in 0:00:5.327
      Added 100 packages to rulebase 5 times in 0:00:21.32

      Added 200 packages to rulebase in 0:00:0.22
      Added 200 packages to rulebase in 0:00:21.691
      Added 200 packages to rulebase in 0:00:21.481
      Added 200 packages to rulebase in 0:00:21.642
      Added 200 packages to rulebase in 0:00:21.341
      Added 200 packages to rulebase 5 times in 0:01:26.385

      As you can see, the timing of creating first RuleBase instance is always good, while the subsequent one's were very bad. Is there an attribute I could adjust in configuration to improve the performance for this scenario? Please advise if there is other alternatives. I would be glad to provide details if needed.

      The following test program would demonstrate the problem:
      import java.util.List;
      import java.util.ArrayList;
      import java.util.Random;
      import java.io.StringReader;
      import java.io.IOException;

      import org.drools.rule.Package;
      import org.drools.compiler.PackageBuilder;
      import org.drools.compiler.DroolsParserException;
      import org.drools.RuleBase;
      import org.drools.RuleBaseFactory;

      /**

      • Created by IntelliJ IDEA. User: SG0521861 Date: Aug 2, 2007 Time: 2:53:50 PM To change this template use File |
      • Settings | File Templates.
        */
        public class TestDrools {
        private static final String ALPHABET= "abcdefghijklmnopqrstuvwxyz";
        private static final String DRL_TEMPLATE =
        "package testpackage\n" +
        "import test.TestObject\n"+
        "rule \"testrule_$1\"\n" +
        " when\n" +
        " h:TestObject(value1==\"$2\", value2 in (\"$3\"))\n" +
        " then\n" +
        " h.setResult(\"$4\");\n" +
        "end";
        private int count;

      public TestDrools(int count)

      { this.count = count; }

      public void run() throws Exception {
      List<String> drls = generateDrls();
      List<Package> pkgs = generatePackages(drls);

      for (int i = 0; i < 3; i++)

      { addPackages(pkgs, i+1); }

      }

      private static RuleBase addPackages(List<Package> pkgs, int id) throws Exception

      { long time = System.currentTimeMillis(); RuleBase rb = RuleBaseFactory.newRuleBase(); for (Package pkg : pkgs) rb.addPackage(pkg); System.out.println("Added "+pkgs.size()+" packages to rulebase"+id+" in "+ (System.currentTimeMillis()-time)/1000.0+" seconds"); return rb; }

      private List<String> generateDrls() {
      List<String> list = new ArrayList<String>(count);
      for (int i = 0; i < count; i++)

      { list.add(getDrl(i)); }

      return list;
      }

      private static List<Package> generatePackages(List<String> drls) throws DroolsParserException, IOException {
      List<Package> list = new ArrayList<Package>(drls.size());
      for (String drl : drls)

      { PackageBuilder builder = new PackageBuilder(); builder.addPackageFromDrl(new StringReader(drl)); list.add(builder.getPackage()); }

      return list;
      }

      private String getDrl(int seed)

      { String drl = DRL_TEMPLATE.replace("$1", Integer.toString(seed)); drl = drl.replace("$2", getRandomString()); drl = drl.replace("$3", getRandomString()); return drl.replace("$4", getRandomString()); }

      private Random rand = new Random();

      private String getRandomString()

      { StringBuilder sb = new StringBuilder(); for (int i = Math.abs(rand.nextInt() % 16) + 1; i-- > 0; ) sb.append(ALPHABET.charAt(Math.abs(rand.nextInt() % ALPHABET.length()))); return sb.toString(); }

      public static void main(String[] argv) {
      if (argv.length > 0) {
      try

      { new TestDrools(Integer.parseInt(argv[0])).run(); }

      catch (Exception ex)

      { ex.printStackTrace(); }

      }
      else
      System.out.println("Usage: TestDrool # of packages");
      }
      }

      package test;
      /**

      • Created by IntelliJ IDEA. User: SG0521861 Date: Aug 2, 2007 Time: 3:38:13 PM To change this template use File |
      • Settings | File Templates.
        */
        public class TestObject {
        private String value1;
        private String value2;
        private String result;

      public String getValue1()

      { return value1; }

      public void setValue1(String value1)

      { this.value1 = value1; }

      public String getValue2()

      { return value2; }

      public void setValue2(String value2)

      { this.value2 = value2; }

      public String getResult()

      { return result; }

      public void setResult(String result)

      { this.result = result; }

      }

        Gliffy Diagrams

          Attachments

          1. TestDrools.java
            4 kB
          2. testdrools.zip
            3 kB
          3. TestObject.java
            0.5 kB

            Activity

              People

              • Assignee:
                mark.proctor Mark Proctor
                Reporter:
                mingjin Ming Jin
              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: