Skip to content

Commit

Permalink
[incubator-kie-drools-5841] Improve matches operator performance (#283)
Browse files Browse the repository at this point in the history
  • Loading branch information
tkobayas authored Apr 16, 2024
1 parent 4503455 commit bc08f89
Showing 1 changed file with 93 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.drools.benchmarks.operators;

import java.util.HashSet;
import java.util.Set;

import org.drools.benchmarks.common.AbstractBenchmark;
import org.drools.benchmarks.common.model.Account;
import org.drools.benchmarks.common.util.BuildtimeUtil;
import org.drools.benchmarks.common.util.RuntimeUtil;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.infra.Blackhole;

public class MatchesBenchmark extends AbstractBenchmark {

@Param({"16", "32"})
private int _rulesNumber;

@Param({"32", "256"})
private int _factsNumber;

@Param({"true", "false"})
private boolean cacheEnabled;

private Set<Account> accounts;

@Setup
public void setupKieBase() {

if (cacheEnabled) {
System.setProperty("drools.matches.compiled.cache.count", "100");
}

StringBuilder sb = new StringBuilder();
sb.append("import " + Account.class.getCanonicalName() + ";\n");

for (int i = 1; i <= _rulesNumber; i++) {

sb.append(" rule NameMatches" + i + "\n" +
" when \n " +
" $account : Account(name matches \"A.*t" + i + "\")\n " +
" then\n " +
" end\n" );
}

// use canonical model
kieBase = BuildtimeUtil.createKieBaseFromDrl(true, sb.toString());
}

@Setup
public void generateFacts() {
accounts = new HashSet<>();
for (int i = 1; i <= _factsNumber; i++) {
final Account account = new Account();
account.setName("Account" + i);
accounts.add(account);
}
}

@Setup(Level.Iteration)
public void setup() {
kieSession = RuntimeUtil.createKieSession(kieBase);
}

@Benchmark
public int test(final Blackhole eater) {
for (Account account : accounts) {
eater.consume(kieSession.insert(account));
}
return kieSession.fireAllRules();
}
}

0 comments on commit bc08f89

Please sign in to comment.