Skip to content

Instantly share code, notes, and snippets.

@fmbenhassine
Last active May 24, 2018 20:53
Show Gist options
  • Save fmbenhassine/0eb38fc58b7364af1b74 to your computer and use it in GitHub Desktop.
Save fmbenhassine/0eb38fc58b7364af1b74 to your computer and use it in GitHub Desktop.
easy rules benchmark #EasyRules
package org.easyrules.samples.bench;
import org.easyrules.core.AnnotatedRulesEngine;
import org.easyrules.core.DefaultRulesEngine;
import java.util.Random;
public class Benchmark {
public static final int NB_ITERATION = 20000; //should be even
public static void main(String[] args) {
/*
* generate random numbers
*/
Random random = new Random();
int[] numbers = new int[NB_ITERATION];
for (int i = 0; i < NB_ITERATION; i++) {
numbers[i] = random.nextInt();
}
/*
* Test default rules engine (no annotations)
*/
SimpleRule simpleRule = new SimpleRule();
DefaultRulesEngine defaultRulesEngine = new DefaultRulesEngine();
defaultRulesEngine.registerRule(simpleRule);
long startTime = System.currentTimeMillis();
for (int i = 0; i < numbers.length; i += 2) {
simpleRule.setA(numbers[i]);
simpleRule.setB(numbers[i + 1]);
defaultRulesEngine.fireRules();
}
long endTime = System.currentTimeMillis() - startTime;
System.out.printf("Execution time of DefaultRulesEngine for %s rules took %sms", NB_ITERATION / 2, endTime );
/*
* Test annotated rules engine
*/
TestRule rule = new TestRule();
AnnotatedRulesEngine annotatedRulesEngine = new AnnotatedRulesEngine();
annotatedRulesEngine.registerRule(rule);
startTime = System.currentTimeMillis();
for (int i = 0; i < numbers.length; i += 2) {
rule.setA(numbers[i]);
rule.setB(numbers[i + 1]);
annotatedRulesEngine.fireRules();
}
endTime = System.currentTimeMillis() - startTime;
System.out.printf("Execution time of AnnotatedRulesEngine for %s rules took %sms", NB_ITERATION / 2, endTime );
}
}
package org.easyrules.samples.bench;
import org.easyrules.core.BasicRule;
public class SimpleRule extends BasicRule {
private int a, b;
@Override
public boolean evaluateConditions() {
return a > b;
}
@Override
public void performActions() throws Exception {
//no op
}
public void setA(int a) {
this.a = a;
}
public void setB(int b) {
this.b = b;
}
}
package org.easyrules.samples.bench;
import org.easyrules.annotation.Action;
import org.easyrules.annotation.Condition;
import org.easyrules.annotation.Rule;
@Rule(name = "test rule", description = "simple rule used for benchmark")
public class TestRule {
private int a, b;
@Condition
public boolean when() {
return a > b;
}
@Action
public void then() {
//no op
}
public void setA(int a) {
this.a = a;
}
public void setB(int b) {
this.b = b;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment