当前位置:网站首页>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 33 Gaussian mean filtering
- 【练习-11】4 Values whose Sum is 0(和为0的4个值)
- STM32如何使用STLINK下载程序:点亮LED跑马灯(库版本)
- Truck History
- Opencv learning log 30 -- histogram equalization
- Cost accounting [13]
- Cost accounting [22]
- Cost accounting [16]
- Market trend report, technical innovation and market forecast of Chinese hospital respiratory humidification equipment
- Matlab example: two expressions of step function
猜你喜欢
Matlab example: two expressions of step function
Learning record: USART serial communication
基于web的照片数码冲印网站
Record of force deduction and question brushing
MATLAB实例:阶跃函数的两种表达方式
学习记录:串口通信和遇到的错误解决方法
STM32 learning record: LED light flashes (register version)
ucore lab 2
Ball Dropping
Learning record: how to perform PWM output
随机推荐
Cost accounting [22]
Market trend report, technical innovation and market forecast of lip care products in China and Indonesia
Market trend report, technological innovation and market forecast of pneumonia drugs obtained by Chinese hospitals
Cost accounting [13]
China's earthwork tire market trend report, technical dynamic innovation and market forecast
【练习-8】(Uva 246)10-20-30==模拟
学习记录:理解 SysTick系统定时器,编写延时函数
学习记录:USART—串口通讯
Medical colposcope Industry Research Report - market status analysis and development prospect forecast
Learning record: Tim - Basic timer
Accounting regulations and professional ethics [5]
JS调用摄像头
差分(一维,二维,三维) 蓝桥杯三体攻击
Research Report on market supply and demand and strategy of Chinese hospital cleaning chemicals industry
Opencv learning log 15 count the number of solder joints and output
E. Breaking the Wall
Research Report on pharmaceutical R & D outsourcing service industry - market status analysis and development prospect forecast
数据在内存中的存储&载入内存,让程序运行起来
毕业才知道IT专业大学生毕业前必做的1010件事
Research Report of pharmaceutical solvent industry - market status analysis and development prospect prediction