Can anybody help me to explain the performance between Stateful and Stateless
session?
I write a very simple test code to test the performance:
Just a message,if the id is odd then set a attribute to "odd"
if it is even then set to "even".
insert message one by one, and insert 1,000,000 times.
Rule:
rule "even"
when
message: Message()
eval((message.getId() % 2) == 0)
then
message.setOddOrEven("odd");
end
rule "odd"
when
message: Message()
eval((message.getId() % 2) == 1)
then
message.setOddOrEven("even");
end
message class just id and oddOrEven
public class Message {
private int id;
private String oddOrEven;
public Message(int id){
this.id = id;
}
// getter and setter
}
My test code:
// create statefulSession and statelessfulSession out of KnowledgeBase
StatefulKnowledgeSession statefulSession =
kbase.newStatefulKnowledgeSession();
StatelessKnowledgeSession statelessfulSession =
kbase.newStatelessKnowledgeSession();
int messageNumber = 1000000; // total message number
Random random = new Random(); // to generate id
long begin = System.currentTimeMillis();
for(int i = 0; i < messageNumber; i++){
FactHandle handle = statefulSession.insert(new Message(random.nextInt()));
statefulSession.retract(handle);
}
statefulSession.dispose();
long end = System.currentTimeMillis();
long statefulTime = end - begin;
begin = System.currentTimeMillis();
for(int i = 0; i < messageNumber; i++){
statelessfulSession.equals(new Message(random.nextInt()));
}
end = System.currentTimeMillis();
long statelessTime = end - begin;
System.out.println("Stateful session time: " + statefulTime + " Stateless
session time: " + statelessTime);
The result: "Stateful session time: 12158 Stateless session time: 46"
oh my god...
Is there any place is wrong in my code ?
--
View this message in context:
http://drools.46999.n3.nabble.com/Performance-Stateful-vs-Stateless-Sessi...
Sent from the Drools: User forum mailing list archive at
Nabble.com.