当前位置: 首页 > 知识库问答 >
问题:

Drools 6.5最终CEP(融合)稳定性和性能问题

国晟睿
2023-03-14

我想看看Drools CEP的最大EPS容量。我使用的是8核2.6GHz CPU和16 GB RAM。我只是用2个规则测试200个EPS。Drools开始时很好,但后来(在5到15分钟内)它停下来或开始不着火,我已经用fireAllRules和FireUntilHalt测试过了。

package com.anet.correlation;
public class TestRealCase {
public static void main(String[] args) {

    Main.initTest();
    RulesRegistery.starttime = System.currentTimeMillis();

    if (RuleRunTimeRegistery.isFireuntilHalt) {

        Thread t = new Thread(new FT());
        t.start();

    }

    int i = 0;
    if (Main.ruleEngine != null) {
        while (true) {
            GeneralCorrelationObject ao1 = new GeneralCorrelationObject();

            ao1.setLOGTYPE("Firewalls");
            ao1.setSourceMachine("1.2.3.4" + (i % 500));
            ao1.setDestinationPort(i);
            Main.ruleEngine.evaluate(ao1);
            i++;

            if (i % RulesRegistery.EPS == 0)
                try {
                    Thread.sleep(1000);

                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

        }
    }
}
import com.anet.correlation.*;
import java.util.*;
import com.anet.ntLog.collector.*; 
import com.anet.alarm.*;
import com.anet.util.*;
import com.anet.correlation.operators.*; 
import com.anet.correlation.utils.*; 

declare GeneralCorrelationObject
               @role(event)
               @expires( 1200s )
end


rule "Port Scan_Port Scan_16"
salience 0
no-loop
when
$map1: Map()
from accumulate(
               generalcorrelationobject1:GeneralCorrelationObject(LOGTYPE=='Firewalls') over window:time( 1m )
 ,init( Map m = new HashMap();Hashtable ht= new Hashtable(); ),
action(
if(generalcorrelationobject1.getSourceMachine()==null)
{
               return;
}
               String key="SourceMachine="+generalcorrelationobject1.getSourceMachine();
               List list = (List)m.get(key);
               if( list == null )
                              list = new ArrayList();
Object val1=generalcorrelationobject1.getDestinationPort();
String value1;
if (val1 instanceof Integer)
               value1=val1+"";
else
               value1=(String)val1;

String not_key=value1;
if (ht.containsKey(key)){
               Hashtable ht_hash=(Hashtable)ht.get(key);
               Object ht_val=ht_hash.get(not_key);
               String ht_value;
               if (ht_val instanceof Integer)
                              ht_value=ht_val+"";
               else
                              ht_value=(String)ht_val;
               if (!not_key.equalsIgnoreCase(ht_value)){
                              ht_hash.put(not_key, not_key); 
               ht.put(key, ht_hash); 
                              list.add( generalcorrelationobject1 );
               }
}
else{
Hashtable ht_hash=new Hashtable(); 
               ht_hash.put(not_key, not_key);
               ht.put(key, ht_hash);
               list.add( generalcorrelationobject1 );
}
               m.put(key,list);),
               result( m ) 
)
then
/*
if ((new CheckListSize()).check($map1,10)){
System.out.println("Done");


}
*/



            Iterator s = $map1.keySet().iterator();
            while (s.hasNext()) {
                String key = (String) s.next();
                List list = (List) $map1.get(key);

                System.out.println(key+" : "+list.size());

            }




end




rule "Port eee Scan_161"
salience 100
no-loop
when

              ee:GeneralCorrelationObject() 
then

if (RulesRegistery.numberofsingleruleexecution % RulesRegistery.printEPS == 0) {
System.out.println(ee.getSourceMachine());
}

RulesRegistery.numberofsingleruleexecution++;
end

RuleEngine代码

package com.anet.correlation;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Date;

import org.kie.api.KieBase;
import org.kie.api.KieBaseConfiguration;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.Message;
import org.kie.api.builder.ReleaseId;
import org.kie.api.conf.EventProcessingOption;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.KieSessionConfiguration;
import org.kie.internal.builder.conf.RuleEngineOption;

import org.kie.internal.KnowledgeBase;
import org.kie.internal.KnowledgeBaseFactory;
import org.kie.internal.builder.KnowledgeBuilder;
import org.kie.internal.builder.KnowledgeBuilderFactory;
import org.kie.internal.io.ResourceFactory;
import org.kie.api.io.ResourceType;

public final class RulesEngine {
    KieSession ksession;
    KieBuilder kbuilder;

    public static String header = null;


    public RulesEngine(boolean b) {
        KieServices ks = KieServices.Factory.get();

        KieFileSystem kfs = ks.newKieFileSystem();
        kfs.write("src/main/resources/bench.drl", getRule());

        this.kbuilder = ks.newKieBuilder(kfs);

        this.kbuilder.buildAll();
        if (this.kbuilder.getResults().hasMessages(new Message.Level[] { Message.Level.ERROR })) {
            throw new IllegalArgumentException(this.kbuilder.getResults().toString());
        }
        ReleaseId relId = this.kbuilder.getKieModule().getReleaseId();

        KieContainer kcontainer = ks.newKieContainer(relId);
        KieBaseConfiguration kbconf = ks.newKieBaseConfiguration();

        kbconf.setOption(EventProcessingOption.STREAM);
        kbconf.setOption(RuleEngineOption.PHREAK);
        // kbconf.setOption(RuleEngineOption.RETEOO);
        System.out.println("KB " + kbconf.getProperty("drools.ruleEngine"));
        KieBase kbase = kcontainer.newKieBase(kbconf);
        KieSessionConfiguration ksconf = ks.newKieSessionConfiguration();

        this.ksession = kbase.newKieSession(ksconf, null);

    }

    String readFile(String fileName) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(fileName));
        try {
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();
            while (line != null) {

                sb.append(line);
                sb.append("\n");
                line = br.readLine();
            }

            String rule = (sb.toString());

            System.out.println("New Final");
            System.out.println(rule);
            return rule;
        } finally {
            br.close();
        }
    }

    public String getRule() {
        try {
            return readFile(".." + File.separator + "rules" + File.separator + "all.drl");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void evaluate(GeneralCorrelationObject message) {

        if (message == null) {

            System.out.println("message null ");

            return;
        }
        if (ksession == null) {

            System.out.println("ksession null ");

            return;
        }

        try {
            Long n = System.currentTimeMillis();

            if (RulesRegistery.numberofrules % RulesRegistery.printEPS == 0) {

                System.out.println("Inserting Objects " + RulesRegistery.numberofrules + " EPS : "
                        + (RulesRegistery.numberofrules / ((n - RulesRegistery.starttime) / 1000)) + " : Total time "
                        + ((n - RulesRegistery.starttime) / (1000 * 60)) + " : " + new Date());
            }

            ksession.insert(message);

            if (RulesRegistery.numberofrules % RulesRegistery.printEPS == 0) {
                System.out.println("Inserted Objects " + RulesRegistery.numberofrules + " EPS : "
                        + (RulesRegistery.numberofrules / ((n - RulesRegistery.starttime) / 1000)) + " : Total time "
                        + ((n - RulesRegistery.starttime) / (1000 * 60)) + " : " + new Date());
            }

            if (!RuleRunTimeRegistery.isFireuntilHalt) {


                    if (RulesRegistery.numberofrules % RulesRegistery.printEPS == 0) {
                        System.out.println("Running Rules " + RulesRegistery.numberofrules + " EPS : "
                                + (RulesRegistery.numberofrules / ((n - RulesRegistery.starttime) / 1000))
                                + " : Total time " + ((n - RulesRegistery.starttime) / (1000 * 60)) + " : "
                                + new Date());
                    }
                    ksession.fireAllRules();

                    if (RulesRegistery.numberofrules % RulesRegistery.printEPS == 0) {
                        System.out.println("Runned Rules " + RulesRegistery.numberofrules + " EPS : "
                                + (RulesRegistery.numberofrules / ((n - RulesRegistery.starttime) / 1000))
                                + " : Total time " + ((n - RulesRegistery.starttime) / (1000 * 60)) + " : "
                                + new Date());
                    }


            }

            RulesRegistery.numberofrules++;
            RuleRunTimeRegistery.lasttiem = n;
        } catch (Exception ee) {
            ee.printStackTrace();
        }

    }



}

共有1个答案

姬振
2023-03-14

除非我错过了:代码中没有retract语句。这意味着您继续插入对象,通常的结果,在一段时间后。

一旦你的规则中不再需要事实,就收回它们。

 类似资料:
  • 概览 首先我们了解一下 YODAOS 的运行时:YODAOS 基于 ShadowNode 它采用事件驱动、非阻塞I/O模型;在设计之初,ShadowNode 的接口与 Node.js 兼容,因此在大部分场景下,开发者可以像 Node.js 一样使用 ShadowNode,了解这些有利于开发者更快速的进行 YODAOS 上的应用开发。 YODAOS 开发应用时,需要关注应用的性能与稳定性,包括但不限

  • 我面临的问题是,这不起作用,因为您可以插入大量您想要的事件,而drools稍后将处理这些事件。所以这一部分的时间显然总是小于一秒钟。所以我的问题是,在处理完所有事件之后,如何调用?

  • TS 作为 JS 的超集能否提高 JS 的性能或者降低生产出错的概率 ?是否值得去坚持使用 ?最近项目初始化的时候用 Vite 选的就是 TS 但项目有很多稀奇古怪的需求,导致 TS 各种报错提示什么的,有点拖慢进度,所以内心有些动摇了,TS 是否值得坚定的使用下去 ?规范问题不用太担心,我自己是强迫症,代码不会乱七八糟,我就怕性能和隐性的报错,请大佬指教,我自己用的是 React

  • 问题内容: 我遇到了一个非常原始的数组上非常简单的map / reduce操作的性能配置文件实例。这是我的jmh基准代码: 以下是典型输出的摘要: 关键时刻发生在迭代13和113:首先将性能降低十倍,然后将其恢复。相应的时间是测试运行的2.5和22.5秒。这些事件的时间对阵列大小BTW非常敏感。 有什么可能解释这种行为?JIT编译器可能已经在第一次迭代中完成了工作。没有要说的GC操作(由Visua

  • 我必须为每个客户端每秒存储大约250个数值,即每小时大约90万个数字。它可能不会是全天的记录(可能每天5-10个小时),但我会根据客户端ID和读取日期对数据进行分区。最大行长约为22-23M,这仍然是可管理的。无论如何,我的方案看起来像这样: 密钥空间的复制因子为2,仅用于测试,告密者为和。我知道复制因子3更符合生产标准。 接下来,我在公司服务器上创建了一个小型集群,三台裸机虚拟化机器,具有2个C

  • 我有3个表(SuccessOrder、FailedOrder和PendingOrder)具有相同的列。每个表都有超过200万条记录。我需要组合来自这3个表的所有数据,并按CreatedDate对它们进行排序,以显示在我的门户中。我正在使用UNION ALL组合所有表的结果。 如果我执行每个子查询,在1或2秒内得到结果。如果我执行整个查询(带有UNION ALL的3个子查询),则需要超过5分钟。 除