[
http://jira.jboss.com/jira/browse/JBRULES-1056?page=comments#action_12371800 ]
Edson Tirelli commented on JBRULES-1056:
----------------------------------------
Mark,
I'm seeing the behavior both on 4.0GA and trunk. Doing a quick profiling, seems the
reason is the PackageCompilationData.write() method:
public void write(final String resourceName,
final byte[] clazzData) throws RuntimeDroolsException {
if ( this.store.put( resourceName,
clazzData ) != null ) {
// we are updating an existing class so reload();
reload();
} else {
try {
wire( convertResourceToClassName( resourceName ) );
} catch ( final Exception e ) {
e.printStackTrace();
throw new RuntimeDroolsException( e );
}
}
}
The first time a rule base is loaded, the classes are generated and loaded, but from
the second time and on, as the class already exists, the reload() method is called, and it
seems to be quite heavy:
public void reload() throws RuntimeDroolsException {
// drops the classLoader and adds a new one
this.classLoader = new PackageClassLoader( this.parentClassLoader );
// Wire up invokers
try {
for ( final Iterator it = this.invokerLookups.keySet().iterator();
it.hasNext(); ) {
wire( (String) it.next() );
}
} catch ( final ClassNotFoundException e ) {
throw new RuntimeDroolsException( e );
} catch ( final InstantiationError e ) {
throw new RuntimeDroolsException( e );
} catch ( final IllegalAccessException e ) {
throw new RuntimeDroolsException( e );
} catch ( final InstantiationException e ) {
throw new RuntimeDroolsException( e );
}
}
Profiling is charging up to 50% of run time to that method when running that test.
Seems we need to either make this method lighter or at least check and only reload if
the bytecodes actually change....
Performance of RuleBase().addPackage(.) goes down on sub sequent
instances of RuleBase with large number of packages.
---------------------------------------------------------------------------------------------------------------------
Key: JBRULES-1056
URL:
http://jira.jboss.com/jira/browse/JBRULES-1056
Project: JBoss Rules
Issue Type: Bug
Security Level: Public(Everyone can see)
Components: Drl Parser/Builder
Affects Versions: 4.0.0.GA
Environment: Windows
Reporter: Ming Jin
Assigned To: Mark Proctor
Fix For: 4.0.1
Attachments: testdrools.zip
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; }
}
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
http://jira.jboss.com/jira/secure/Administrators.jspa
-
For more information on JIRA, see:
http://www.atlassian.com/software/jira