当前位置:网站首页>Information security - threat detection engine - common rule engine base performance comparison
Information security - threat detection engine - common rule engine base performance comparison
2022-07-06 15:47:00 【Empty one by one】
Recently, we need to use rule engine to do data processing and threat detection engine , Therefore, several technical solutions commonly used by common centralized rule engines in the market have been investigated , I use the same rules ,
Business rules :
The order amount is less than 100, Give points 0;
Order amount (100,500], Give points 100;
Order amount (500,1000], Give points 500;
Order amount (1000,+&], Give points 1000;
Use the following rule engine to execute 100 Ten thousand times 、1000 Ten thousand times 、5000 Ten thousand times , Each scenario executes 3 Take the average value every time to get the results in the following table , From the performance test results ,groovy Win , Best performance .
Number of executions \ Rules engine | QLExpression (ms) | groovy (ms) | drools (ms) | v8 |
---|---|---|---|---|
100w | 1590 | 171 | 364 | NA |
1000w | 12551 | 709 | 3530 | NA |
5000w | 60230 | 3516 | 19414 | NA |
because v8 use js code , Execution performance is low , Not included for comparison ;
Rules engine | QLExpression | groovy | drools |
---|---|---|---|
Dynamic rules | Support | Support | Support |
flexibility | low | high | in |
Complexity | low | high | in |
Get started | fast | slow | slow |
Use breadth | in | in | high |
All three rule engines support dynamic rules , Can be provided by api Dynamically extend rules , In terms of flexibility groovy and drools Support scripting , There are common packaging schemes on the market ; among QL Widely used in small and medium-sized enterprises, familiar with java For the contestants, it is basically out of the box ,groovy and drools As a rule engine, it is widely used in Internet enterprises , There are certain learning and training costs ,drools More followers, wider application area ;
The core test code is as follows
groovy
/**
* @ClassName GroovyApplication
* @Description: TODO
* @Author ikong
* @Date 2022/5/30
* @Version V1.0
**/
public class GroovyOrderScoreApplication {
private static String BASIC_FILE_DIR_PATH = "/Users/ikong/demo/rule-engine-demo/rule-engine-demo-groovy/";
private static String GROOVY_FILE_DIR_PATH = BASIC_FILE_DIR_PATH + "src/main/resources/groovy/";
private static String GROOVY_ORDER01_SCRIPT = GROOVY_FILE_DIR_PATH + "order01_java.groovy";
private static String GROOVY_G_SCRIPT = "test02_script.groovy";
private static String GROOVY_P_SCRIPT = "test03_param.groovy";
public static void main(String[] args) throws Exception {
executeJava();//groovy perform java Written rule code
}
private static void executeJava() throws Exception {
// Method 1 calls groovy file
ClassLoader parent = GroovyOrderScoreApplication.class.getClassLoader();
GroovyClassLoader loader = new GroovyClassLoader(parent);
Class groovyClass = loader.parseClass(new File(GROOVY_ORDER01_SCRIPT));
// obtain groovy object
GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance();
List<Order> orderList = getInitData();
int count = 200000;
long now = System.currentTimeMillis();
for (int j = 0; j < count; j++) {
for (int i = 0; i < orderList.size(); i++) {
Order order = orderList.get(i);
groovyObject.setProperty("order", order);
groovyObject.invokeMethod("apply", null);
}
}
long cost = System.currentTimeMillis() - now;
System.out.println("drools Program execution :" + count + " Time , Time consuming :" + cost + "ms");
}
private static List<Order> getInitData() throws Exception {
List<Order> orderList = new ArrayList<Order>();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
{
Order order = new Order();
order.setAmout(80);
order.setBookingDate(df.parse("2015-07-01"));
User user = new User();
user.setLevel(1);
user.setName("Name1");
order.setUser(user);
order.setScore(111);
orderList.add(order);
}
{
Order order = new Order();
order.setAmout(200);
order.setBookingDate(df.parse("2015-07-02"));
User user = new User();
user.setLevel(2);
user.setName("Name2");
order.setUser(user);
orderList.add(order);
}
{
Order order = new Order();
order.setAmout(800);
order.setBookingDate(df.parse("2015-07-03"));
User user = new User();
user.setLevel(3);
user.setName("Name3");
order.setUser(user);
orderList.add(order);
}
{
Order order = new Order();
order.setAmout(1500);
order.setBookingDate(df.parse("2015-07-04"));
User user = new User();
user.setLevel(4);
user.setName("Name4");
order.setUser(user);
orderList.add(order);
}
return orderList;
}
}
pom.xml
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>3.0.8</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
<version>3.0.8</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-ant</artifactId>
<version>3.0.8</version>
</dependency>
</dependencies>
Drools
/**
* @ClassName DroolsApplication
* @Description: TODO
* @Author ikong
* @Date 2022/5/30
* @Version V1.0
**/
public class DroolsApplication {
/**
* Calculate the amount of extra points The following rules : Original order price amount
* 100 following , No bonus points
* 100-500 Add 100 branch
* 500-1000 Add 500 branch
* 1000 above Add 1000 branch
*
* @param args
* @throws Exception
*/
public static final void main(final String[] args) throws Exception {
// KieServices is the factory for all KIE services
KieServices ks = KieServices.Factory.get();
// From the kie services, a container is created from the classpath
KieContainer kc = ks.getKieClasspathContainer();
execute(kc);
}
public static void execute(KieContainer kc) throws Exception {
// From the container, a session is created based on
// its definition and configuration in the META-INF/kmodule.xml file
KieSession ksession = kc.newKieSession("point-rulesKS");
List<Order> orderList = getInitData();
for (int i = 0; i < orderList.size(); i++) {
Order o = orderList.get(i);
ksession.insert(o);
ksession.fireAllRules();
}
int count = 200000;
long now = System.currentTimeMillis();
for (int j = 0; j < count; j++) {
for (int i = 0; i < orderList.size(); i++) {
Order o = orderList.get(i);
ksession.insert(o);
ksession.fireAllRules();
}
}
long cost = System.currentTimeMillis() - now;
System.out.println("drools Program execution :" + count + " Time , Time consuming :" + cost + "ms");
ksession.dispose();
}
private static List<Order> getInitData() throws Exception {
List<Order> orderList = new ArrayList<Order>();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
{
Order order = new Order();
order.setAmout(80);
order.setBookingDate(df.parse("2015-07-01"));
User user = new User();
user.setLevel(1);
user.setName("Name1");
order.setUser(user);
order.setScore(111);
orderList.add(order);
}
{
Order order = new Order();
order.setAmout(200);
order.setBookingDate(df.parse("2015-07-02"));
User user = new User();
user.setLevel(2);
user.setName("Name2");
order.setUser(user);
orderList.add(order);
}
{
Order order = new Order();
order.setAmout(800);
order.setBookingDate(df.parse("2015-07-03"));
User user = new User();
user.setLevel(3);
user.setName("Name3");
order.setUser(user);
orderList.add(order);
}
{
Order order = new Order();
order.setAmout(1500);
order.setBookingDate(df.parse("2015-07-04"));
User user = new User();
user.setLevel(4);
user.setName("Name4");
order.setUser(user);
orderList.add(order);
}
return orderList;
}
}
pom.xml
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-core</artifactId>
<version>${drools.version}</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-compiler</artifactId>
<version>${drools.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<drools.version>7.0.0.Final</drools.version>
<java.version>1.8</java.version>
<tomcat.version>8.5.11</tomcat.version>
</properties>
QLExpression
public class QLExpressionApplication {
private static ExpressRunner runner = new ExpressRunner();
private static IExpressContext<String, Object> expressContext = new DefaultContext<String, Object>();
public static void main(String[] args) throws Exception {
String expression = "if (o.amout > 1000) return 1000;"
+ " if (o.amout > 500 && o.amout <= 1000) return 500 ;"
+ " if (o.amout > 100 && o.amout <= 500) return 100 ;"
+ " if (o.amout <= 100 ) return 0 ;";
List<Order> orderList = getInitData();
int count = 200000;
long now = System.currentTimeMillis();
for (int j = 0; j < count; j++) {
for (int i = 0; i < orderList.size(); i++) {
Order order = orderList.get(i);
String ret = execute(order, expression);
if (!StringUtils.isEmpty(ret)) {
order.setScore(Integer.valueOf(ret));
}
}
}
long cost = System.currentTimeMillis() - now;
System.out.println("java Program execution :" + count + " Time , Time consuming :" + cost + "ms");
}
public static String execute(Object obj, String expression) throws Exception {
expressContext.put("o", obj);
List<String> errorInfo = new ArrayList<String>();
Object resultObj = runner.execute(expression, expressContext, errorInfo, true, false);
return null == resultObj ? "" : resultObj.toString();
}
private static List<Order> getInitData() throws Exception {
List<Order> orderList = new ArrayList<Order>();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
{
Order order = new Order();
order.setAmout(80);
order.setBookingDate(df.parse("2015-07-01"));
User user = new User();
user.setLevel(1);
user.setName("Name1");
order.setUser(user);
orderList.add(order);
}
{
Order order = new Order();
order.setAmout(200);
order.setBookingDate(df.parse("2015-07-02"));
User user = new User();
user.setLevel(2);
user.setName("Name2");
order.setUser(user);
orderList.add(order);
}
{
Order order = new Order();
order.setAmout(800);
order.setBookingDate(df.parse("2015-07-03"));
User user = new User();
user.setLevel(3);
user.setName("Name3");
order.setUser(user);
orderList.add(order);
}
{
Order order = new Order();
order.setAmout(1500);
order.setBookingDate(df.parse("2015-07-04"));
User user = new User();
user.setLevel(4);
user.setName("Name4");
order.setUser(user);
orderList.add(order);
}
return orderList;
}
}
pom.xml
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>QLExpress</artifactId>
<version>3.2.2</version>
</dependency>
All the above tests are in JDK1.8 Next finish
This article code warehouse : https://github.com/philip502/rule-engine-demo.git
边栏推荐
- Opencv learning log 13 corrosion, expansion, opening and closing operations
- Learning record: understand systick system timer and write delay function
- Cost accounting [16]
- mysql导入数据库报错 [Err] 1273 – Unknown collation: ‘utf8mb4_0900_ai_ci’
- Research Report on market supply and demand and strategy of China's earth drilling industry
- Research Report on surgical fluid treatment industry - market status analysis and development prospect prediction
- 初入Redis
- Gartner:关于零信任网络访问最佳实践的五个建议
- Hospital privacy screen Industry Research Report - market status analysis and development prospect forecast
- Opencv learning log 18 Canny operator
猜你喜欢
随机推荐
China chart recorder market trend report, technology dynamic innovation and market forecast
Printing quality inspection and verification system Industry Research Report - market status analysis and development prospect forecast
【练习-5】(Uva 839)Not so Mobile(天平)
【练习-9】Zombie’s Treasure Chest
Accounting regulations and professional ethics [4]
Opencv learning log 31 -- background difference
【练习-2】(Uva 712) S-Trees (S树)
Learning records: serial communication and solutions to errors encountered
STM32学习记录:LED灯闪烁(寄存器版)
7-1 懂的都懂 (20 分)
1010 things that college students majoring in it must do before graduation
通俗地理解什么是编程语言
毕业才知道IT专业大学生毕业前必做的1010件事
【练习-6】(Uva 725)Division(除法)== 暴力
China's earthwork tire market trend report, technical dynamic innovation and market forecast
STM32 learning record: LED light flashes (register version)
力扣刷题记录
Accounting regulations and professional ethics [3]
Research Report on shell heater industry - market status analysis and development prospect forecast
Learning record: STM32F103 clock system overview working principle