Drools规则模板-第一次调用比子序列调用慢1000倍 [英] Drools Rules Template - first invocation 1000 times slower than subsequence invocations

查看:298
本文介绍了Drools规则模板-第一次调用比子序列调用慢1000倍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经构建了使用Drools 6.4.0的Drools规则模板。最后

与提供的示例非常相似此处

It is pretty much identical to the example provided here.

我将其包装在 Spring RestController 中,并将其作为WAR部署到 Tomcat

I have wrapped it in a Spring RestController and deployed it as a WAR in to Tomcat.

我注意到第一个调用总是很慢才能得到结果,并且随着数字的增加而变慢 xls 电子表格中的行数增长。

I notice that the first call is always very slow to get a result and gets slower as the number of rows in the xls spreadsheet grows.

我想象drools在创建第一个 KieSession 时会建立一个索引。而且该索引随后被缓存,从而使进一步的调用变得更快了?

I imagine drools builds an index when the first ever KieSession is created. And that this index is subsequently cached making further calls much faster?

我直接从单元测试调用时看到了相同的行为。第一次测试很慢,后续测试则快1000倍。

I see the same behaviour when invoking directly from unit tests. First test is slow and subsequent tests are 1000 times faster.

例如,我看到第一次调用花费30秒,而随后的每次调用花费20毫秒,其中电子表格有1000行。

For example, I see first call take 30secs and every subsequent call take 20ms where the spreadsheet has ~1000 rows.

当Web应用程序服务器( Tomcat )处于运行状态时,是否有办法强制执行此索引编制步骤

Is there a way to force this "indexing" step to happen when the web application server (Tomcat) is starting up?

推荐答案

在您链接的示例中,有一行内容的作用超出了它看起来的作用。我要说的是:

In the example you have linked, there is a line that is doing more than what it appears to be doing. The line I'm talking about is:

KieSession ksession = kc.newKieSession( "DTableWithTemplateKS" );

KieContainer.newKieSession()创建指定的 KieSession 所属的 KieBase KieBase 是规则的二进制表示形式。生成 KieBase 后,就可以使用它生成多个 KieSessions (它们的运行时对应项)。 KieBase 的创建可能非常耗时。不是从中生成新的 KieSessions

One of the steps inside KieContainer.newKieSession() is to create the KieBase the specified KieSession belongs to. A KieBase is the binary representation of your rules. Once a KieBase is built, it can be used to spawn multiple KieSessions (their runtime counterpart). The creation of a KieBase could be very time consuming. Spawning new KieSessions from it it is not.

KieContainer 类使用内部地图中保留对已构建的 KieBases 的引用。第一次向 KieContainer 请求 KieSession KieContainer 首先要构建 KieBase 。在调用 newKieSession()之后,将重用已经构建的KieBase。请注意,只要您始终要求相同的 KieSession ,这就是事实。尝试使用多个 KieBases 并从中请求不同的 KieSessions ,您会发现您第一次请求 KieSession 来自新的 KieBase ,您将遇到此延迟。

The KieContainer class uses an internal Map to keep a reference to the KieBases that were already built. The first time you ask KieContainer for a KieSession the KieContainer has first to build the KieBase. Following invocations of newKieSession() will reuse the KieBase already built. Note that this is true as long as you always ask for the same KieSession. Try having multiple KieBases and ask for different KieSessions from them and you will see that the first time you ask for a KieSession from a new KieBase you will experience this delay.

您可以做的一件事是,在应用启动时,向 KieContainer 询问您的 KieBase 。您可以通过执行 kc.newKieSession( XXX); kc.getKieBase( YYY);

One thing you could do is to ask KieContainer for your KieBase when your app is starting. You can do this by either executing kc.newKieSession( "XXX" ); or kc.getKieBase("YYY");

希望有帮助,

这篇关于Drools规则模板-第一次调用比子序列调用慢1000倍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆