001: package org.drools.integrationtests.sequential;
002:
003: import java.io.IOException;
004: import java.io.InputStreamReader;
005: import java.util.ArrayList;
006: import java.util.List;
007: import java.util.Properties;
008:
009: import junit.framework.TestCase;
010:
011: import org.drools.Cheese;
012: import org.drools.Person;
013: import org.drools.RuleBase;
014: import org.drools.RuleBaseConfiguration;
015: import org.drools.RuleBaseFactory;
016: import org.drools.StatelessSession;
017: import org.drools.compiler.DroolsParserException;
018: import org.drools.compiler.PackageBuilder;
019: import org.drools.rule.Package;
020:
021: public class SequentialTest extends TestCase {
022: public void testBasicOperation() throws Exception {
023:
024: // postponed while I sort out KnowledgeHelperFixer
025: final PackageBuilder builder = new PackageBuilder();
026: builder.addPackageFromDrl(new InputStreamReader(getClass()
027: .getResourceAsStream("simpleSequential.drl")));
028: final Package pkg = builder.getPackage();
029:
030: RuleBaseConfiguration conf = new RuleBaseConfiguration();
031: conf.setSequential(true);
032: final RuleBase ruleBase = getRuleBase(conf);
033: ruleBase.addPackage(pkg);
034: final StatelessSession session = ruleBase.newStatelessSession();
035:
036: final List list = new ArrayList();
037: session.setGlobal("list", list);
038:
039: final Person p1 = new Person("p1", "stilton");
040: final Person p2 = new Person("p2", "cheddar");
041: final Person p3 = new Person("p3", "stilton");
042:
043: final Cheese stilton = new Cheese("stilton", 15);
044: final Cheese cheddar = new Cheese("cheddar", 15);
045:
046: session.execute(new Object[] { p1, stilton, p2, cheddar, p3 });
047:
048: assertEquals(3, list.size());
049:
050: }
051:
052: public void XXtestProfileSequential() throws Exception {
053:
054: runTestProfileManyRulesAndFacts(true, "Sequential mode", 0,
055: "sequentialProfile.drl");
056: runTestProfileManyRulesAndFacts(true, "Sequential mode", 0,
057: "sequentialProfile.drl");
058:
059: System.gc();
060: Thread.sleep(100);
061: }
062:
063: public void XXtestProfileRETE() throws Exception {
064: runTestProfileManyRulesAndFacts(false, "Normal RETE mode", 0,
065: "sequentialProfile.drl");
066: runTestProfileManyRulesAndFacts(false, "Normal RETE mode", 0,
067: "sequentialProfile.drl");
068:
069: System.gc();
070: Thread.sleep(100);
071: }
072:
073: public void testNumberofIterationsSeq() throws Exception {
074: //test throughput
075: runTestProfileManyRulesAndFacts(true, "SEQUENTIAL", 2000,
076: "sequentialProfile.drl");
077: }
078:
079: public void testNumberofIterationsRETE() throws Exception {
080: //test throughput
081: runTestProfileManyRulesAndFacts(false, "RETE", 2000,
082: "sequentialProfile.drl");
083:
084: }
085:
086: public void XXtestPerfJDT() throws Exception {
087: runTestProfileManyRulesAndFacts(true, "JDT", 2000,
088: "sequentialProfile.drl");
089:
090: }
091:
092: public void XXtestPerfMVEL() throws Exception {
093: runTestProfileManyRulesAndFacts(true, "MVEL", 2000,
094: "sequentialProfileMVEL.drl");
095:
096: }
097:
098: private void runTestProfileManyRulesAndFacts(
099: boolean sequentialMode, String message,
100: int timetoMeasureIterations, String file)
101: throws DroolsParserException, IOException, Exception {
102: // postponed while I sort out KnowledgeHelperFixer
103: final PackageBuilder builder = new PackageBuilder();
104: builder.addPackageFromDrl(new InputStreamReader(getClass()
105: .getResourceAsStream(file)));
106: final Package pkg = builder.getPackage();
107:
108: Properties properties = new Properties();
109: properties.setProperty("drools.shadowProxyExcludes",
110: "org.drools.*");
111:
112: RuleBaseConfiguration conf = new RuleBaseConfiguration(
113: properties);
114: conf.setSequential(sequentialMode);
115:
116: final RuleBase ruleBase = getRuleBase(conf);
117: ruleBase.addPackage(pkg);
118: final StatelessSession session = ruleBase.newStatelessSession();
119:
120: final List list = new ArrayList();
121: session.setGlobal("list", list);
122:
123: Object[] data = new Object[50000];
124: for (int i = 0; i < data.length; i++) {
125:
126: if (i % 2 == 0) {
127: final Person p = new Person("p" + i, "stilton");
128: data[i] = p;
129: } else {
130: data[i] = new Cheese("cheddar", i);
131: }
132: }
133:
134: if (timetoMeasureIterations == 0) {
135: //one shot measure
136: long start = System.currentTimeMillis();
137: session.execute(data);
138: System.out.println("Time for " + message + ":"
139: + (System.currentTimeMillis() - start));
140: assertTrue(list.size() > 0);
141:
142: } else {
143: //lots of shots
144: //test throughput
145: long start = System.currentTimeMillis();
146: long end = start + timetoMeasureIterations;
147: int count = 0;
148: while (System.currentTimeMillis() < end) {
149: StatelessSession sess2 = ruleBase.newStatelessSession();
150: List list2 = new ArrayList();
151: sess2.setGlobal("list", list2);
152:
153: sess2.execute(data);
154: //session.execute( data );
155: count++;
156: }
157: System.out.println("Iterations in for " + message + " : "
158: + count);
159:
160: }
161:
162: }
163:
164: protected RuleBase getRuleBase() throws Exception {
165:
166: return RuleBaseFactory.newRuleBase(RuleBase.RETEOO, null);
167: }
168:
169: protected RuleBase getRuleBase(final RuleBaseConfiguration config)
170: throws Exception {
171:
172: return RuleBaseFactory.newRuleBase(RuleBase.RETEOO, config);
173: }
174: }
|