当前位置:网站首页>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 .

Performance comparison of common rule engine bases
Number of executions \ Rules engine

QLExpression 

(ms)

groovy

(ms)

drools

(ms)

v8
100w1590171364NA
1000w125517093530NA
5000w60230  3516 19414NA

because v8 use js code , Execution performance is low , Not included for comparison ;

Common rule engine bases are difficult to compare
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

原网站

版权声明
本文为[Empty one by one]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060919524529.html