Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[incubator-kie-issues#1301] Create DMN benchmarks specifically targeting the codegen execution. #288

Merged
merged 2 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* 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.dmn.codegen;

import org.kie.dmn.api.core.DMNCompiler;
import org.kie.dmn.api.core.DMNCompilerConfiguration;
import org.kie.dmn.api.core.DMNContext;
import org.kie.dmn.api.core.DMNModel;
import org.kie.dmn.api.core.DMNRuntime;
import org.kie.dmn.core.compiler.DMNCompilerConfigurationImpl;
import org.kie.dmn.core.compiler.DMNCompilerImpl;
import org.kie.dmn.core.compiler.RuntimeTypeCheckOption;
import org.kie.dmn.core.impl.DMNRuntimeImpl;
import org.kie.dmn.core.internal.utils.DMNRuntimeBuilder;
import org.kie.dmn.feel.parser.feel11.profiles.DoCompileFEELProfile;
import org.kie.dmn.feel.util.Either;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;
import java.util.function.Function;

import static org.drools.benchmarks.dmn.util.DynamicTypeUtils.entry;
import static org.drools.benchmarks.dmn.util.DynamicTypeUtils.prototype;

public abstract class AbstractCodegenBenchmark {

private DMNRuntime dmnRuntime;
private DMNModel dmnModel;
private DMNContext dmnContext;

protected abstract String getResource();
protected abstract List<String> getAdditionalResources();
protected abstract String getNameSpace();
protected abstract String getModelName();
protected abstract Map<String, Object> getInputData();

protected void setupModelAndContext() {
Function<DMNCompilerConfiguration, DMNCompiler> dmnCompilerFn = dmnCompilerConfiguration -> {
((DMNCompilerConfigurationImpl) dmnCompilerConfiguration).addFEELProfile(new DoCompileFEELProfile());
return new DMNCompilerImpl(dmnCompilerConfiguration);
};
DMNRuntimeBuilder.DMNRuntimeBuilderConfigured dmnRuntimeBuilderConfigured = DMNRuntimeBuilder.fromDefaults()
.buildConfigurationUsingCustomCompiler(dmnCompilerFn);
Either<Exception, DMNRuntime> exceptionDMNRuntimeEither;
if (getAdditionalResources() != null && !getAdditionalResources().isEmpty()) {
exceptionDMNRuntimeEither = dmnRuntimeBuilderConfigured
.fromClasspathResources(getResource(), this.getClass(), getAdditionalResources().toArray(new String[0]));
} else {
exceptionDMNRuntimeEither = dmnRuntimeBuilderConfigured
.fromClasspathResource(getResource(), this.getClass());
}
dmnRuntime = exceptionDMNRuntimeEither
.getOrElseThrow(e -> new RuntimeException("Error initializing DMNRuntime", e));
((DMNRuntimeImpl) dmnRuntime).setOption(new RuntimeTypeCheckOption(true));
dmnModel = dmnRuntime.getModel(
getNameSpace(),
getModelName());
if (dmnModel == null) {
throw new RuntimeException("Model " + getNameSpace() + "." + getModelName() + " not found");
}
dmnContext = dmnRuntime.newContext();
getInputData().forEach((key, value) -> dmnContext.set(key, value));
}

protected Object evaluateModelBenchmark() {
return dmnRuntime.evaluateAll(dmnModel, dmnContext);
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* 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.dmn.codegen;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static org.drools.benchmarks.dmn.util.DynamicTypeUtils.entry;
import static org.drools.benchmarks.dmn.util.DynamicTypeUtils.prototype;

@BenchmarkMode(Mode.AverageTime)
@State(Scope.Thread)
@Warmup(iterations = 100, time = 200, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 20, time = 200, timeUnit = TimeUnit.MILLISECONDS)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public class ImportedModelCodegenBenchmark extends AbstractCodegenBenchmark {

@Override
protected String getResource() {
return "dmn/Importing_EmptyNamed_Model_With_Href_Namespace.dmn";
}

@Override
protected List<String> getAdditionalResources() {
return List.of("dmn/Imported_Model_Unamed.dmn");
}

@Override
protected String getNameSpace() {
return "http://www.trisotech.com/dmn/definitions/_f79aa7a4-f9a3-410a-ac95-bea496edabgc";
}

@Override
protected String getModelName() {
return "Importing empty-named Model";
}

@Override
protected Map<String, Object> getInputData() {
Map<String, Object> aPerson = prototype(entry("name", "John"), entry("age", 20));
Map<String, Object> anImportedPerson = prototype(entry("name", "Luke"), entry("age", 35));
return prototype(entry("A Person", aPerson), entry("An Imported Person", anImportedPerson));
}

@Setup()
public void setupModelAndContext() {
super.setupModelAndContext();
}

@Benchmark
public Object evaluateModelBenchmark() {
return super.evaluateModelBenchmark();
}

public static void main(String[] args) throws Exception {
ImportedModelCodegenBenchmark a = new ImportedModelCodegenBenchmark();
a.setupModelAndContext();
System.out.println(a.evaluateModelBenchmark());
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* 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.dmn.codegen;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static org.drools.benchmarks.dmn.util.DynamicTypeUtils.entry;
import static org.drools.benchmarks.dmn.util.DynamicTypeUtils.prototype;

@BenchmarkMode(Mode.AverageTime)
@State(Scope.Thread)
@Warmup(iterations = 100, time = 200, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 20, time = 200, timeUnit = TimeUnit.MILLISECONDS)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public class PrequalificationCodegenBenchmark extends AbstractCodegenBenchmark {

@Override
protected String getResource() {
return "dmn/Prequalification.dmn";
}

@Override
protected List<String> getAdditionalResources() {
return null;
}

@Override
protected String getNameSpace() {
return "http://www.trisotech.com/definitions/_f31e1f8e-d4ce-4a3a-ac3b-747efa6b3401";
}

@Override
protected String getModelName() {
return "Prequalification";
}

@Override
protected Map<String, Object> getInputData() {
Map<String, Object> borrower = prototype(entry("Monthly Income", 100), entry("Monthly Other Debt", 20));
return prototype(entry("Credit Score", 350),
entry("Loan Amount", 15),
entry("Appraised Value", 10),
entry("Best Rate", 5),
entry("Borrower", borrower));
}

@Setup()
public void setupModelAndContext() {
super.setupModelAndContext();
}

@Benchmark
public Object evaluateModelBenchmark() {
return super.evaluateModelBenchmark();
}


public static void main(String[] args) throws Exception {
PrequalificationCodegenBenchmark a = new PrequalificationCodegenBenchmark();
a.setupModelAndContext();
System.out.println(a.evaluateModelBenchmark());
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->

<dmn:definitions xmlns="http://www.trisotech.com/dmn/definitions/_f27bb64b-6fc7-4e1f-9848-11ba35e0df44"
xmlns:dc="http://www.omg.org/spec/DMN/20180521/DC/"
xmlns:di="http://www.omg.org/spec/DMN/20180521/DI/"
xmlns:dmndi="https://www.omg.org/spec/DMN/20230324/DMNDI/"
xmlns:feel="https://www.omg.org/spec/DMN/20230324/FEEL/"
xmlns:dmn="https://www.omg.org/spec/DMN/20230324/MODEL/"
xmlns:tc="http://www.omg.org/spec/DMN/20160719/testcase"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
exporter="DMN Modeler"
exporterVersion="6.0.3.201802231629"
id="_f27bb64b-6fc7-4e1f-9848-11ba35e0df44"
name="Imported Model"
namespace="http://www.trisotech.com/dmn/definitions/_f27bb64b-6fc7-4e1f-9848-11ba35e0df44">
<dmn:extensionElements>
</dmn:extensionElements>
<dmn:itemDefinition id="_63824D3F-9173-446D-A940-6A7F0FA056BB" name="tPerson" isCollection="false">
<dmn:itemComponent id="_9bb0759c-b3c1-482f-87f5-c047dc65cef0" name="name">
<dmn:typeRef>string</dmn:typeRef>
</dmn:itemComponent>
<dmn:itemComponent id="_929acc15-101c-4e49-9b11-494fff411e50" name="age">
<dmn:typeRef>number</dmn:typeRef>
</dmn:itemComponent>
</dmn:itemDefinition>
<dmn:inputData id="_51190b90-924d-479b-872b-4c6f3486c2cb" name="A Person">
<dmn:variable id="_44a44de4-c0ab-408e-9ba9-983d8ec2f6b5"
name="A Person"
typeRef="tPerson"/>
</dmn:inputData>
<dmn:inputData id="_51190b90-924d-479b-872b-4c6f3486c2de" name="An Imported Person">
<dmn:variable id="_44a44de4-c0ab-408e-9ba9-983d8ec2f6c6"
name="An Imported Person"
typeRef="tPerson"/>
</dmn:inputData>
<dmn:decision id="_bf4a9628-15ae-4887-97f2-7099426cb61g" name="Remote Greeting">
<dmn:variable id="_ecc6e0bb-a0af-4e99-aac6-5b8bed09c549"
name="Remote Greeting"
typeRef="string"/>
<dmn:informationRequirement>
<dmn:requiredInput href="#_51190b90-924d-479b-872b-4c6f3486c2de"/>
</dmn:informationRequirement>
<dmn:knowledgeRequirement>
<dmn:requiredKnowledge href="#_32543811-b499-4608-b784-6c6f294b1c58"/>
</dmn:knowledgeRequirement>
<dmn:literalExpression xmlns:triso="http://www.trisotech.com/2015/triso/modeling"
id="_d7e6836b-8491-487a-a653-5735daa85bf2"
triso:unparsed="true">
<dmn:text>Say Hello( An Imported Person )</dmn:text>
</dmn:literalExpression>
</dmn:decision>

<dmn:businessKnowledgeModel id="_32543811-b499-4608-b784-6c6f294b1c58" name="Say Hello">
<dmn:variable id="_a8eb10e1-30e6-40d8-a564-a868f4e0af34"
name="Say Hello"
typeRef="string"/>
<dmn:encapsulatedLogic kind="FEEL" id="_acbb96c9-34a3-4628-8179-dfc5f583e695">
<dmn:formalParameter id="_4a626f74-2ecc-4759-b76a-04baec6b795d"
name="Person"
typeRef="tPerson"/>
<dmn:literalExpression id="_c173a894-3719-4d2f-a365-25850e217310">
<dmn:text>"Hello " + Person.name + "!"</dmn:text>
</dmn:literalExpression>
</dmn:encapsulatedLogic>
</dmn:businessKnowledgeModel>

</dmn:definitions>
Loading
Loading