|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.drools.benchmarks.operators; |
| 21 | + |
| 22 | +import java.util.HashSet; |
| 23 | +import java.util.Set; |
| 24 | + |
| 25 | +import org.drools.benchmarks.common.AbstractBenchmark; |
| 26 | +import org.drools.benchmarks.common.model.Account; |
| 27 | +import org.drools.benchmarks.common.util.BuildtimeUtil; |
| 28 | +import org.drools.benchmarks.common.util.RuntimeUtil; |
| 29 | +import org.openjdk.jmh.annotations.Benchmark; |
| 30 | +import org.openjdk.jmh.annotations.Level; |
| 31 | +import org.openjdk.jmh.annotations.Param; |
| 32 | +import org.openjdk.jmh.annotations.Setup; |
| 33 | +import org.openjdk.jmh.infra.Blackhole; |
| 34 | + |
| 35 | +public class MatchesBenchmark extends AbstractBenchmark { |
| 36 | + |
| 37 | + @Param({"16", "32"}) |
| 38 | + private int _rulesNumber; |
| 39 | + |
| 40 | + @Param({"32", "256"}) |
| 41 | + private int _factsNumber; |
| 42 | + |
| 43 | + @Param({"true", "false"}) |
| 44 | + private boolean cacheEnabled; |
| 45 | + |
| 46 | + private Set<Account> accounts; |
| 47 | + |
| 48 | + @Setup |
| 49 | + public void setupKieBase() { |
| 50 | + |
| 51 | + if (cacheEnabled) { |
| 52 | + System.setProperty("drools.matches.compiled.cache.count", "100"); |
| 53 | + } |
| 54 | + |
| 55 | + StringBuilder sb = new StringBuilder(); |
| 56 | + sb.append("import " + Account.class.getCanonicalName() + ";\n"); |
| 57 | + |
| 58 | + for (int i = 1; i <= _rulesNumber; i++) { |
| 59 | + |
| 60 | + sb.append(" rule NameMatches" + i + "\n" + |
| 61 | + " when \n " + |
| 62 | + " $account : Account(name matches \"A.*t" + i + "\")\n " + |
| 63 | + " then\n " + |
| 64 | + " end\n" ); |
| 65 | + } |
| 66 | + |
| 67 | + // use canonical model |
| 68 | + kieBase = BuildtimeUtil.createKieBaseFromDrl(true, sb.toString()); |
| 69 | + } |
| 70 | + |
| 71 | + @Setup |
| 72 | + public void generateFacts() { |
| 73 | + accounts = new HashSet<>(); |
| 74 | + for (int i = 1; i <= _factsNumber; i++) { |
| 75 | + final Account account = new Account(); |
| 76 | + account.setName("Account" + i); |
| 77 | + accounts.add(account); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @Setup(Level.Iteration) |
| 82 | + public void setup() { |
| 83 | + kieSession = RuntimeUtil.createKieSession(kieBase); |
| 84 | + } |
| 85 | + |
| 86 | + @Benchmark |
| 87 | + public int test(final Blackhole eater) { |
| 88 | + for (Account account : accounts) { |
| 89 | + eater.consume(kieSession.insert(account)); |
| 90 | + } |
| 91 | + return kieSession.fireAllRules(); |
| 92 | + } |
| 93 | +} |
0 commit comments