Skip to content

Commit

Permalink
Parametrized benchmark (apache#11)
Browse files Browse the repository at this point in the history
* Fixes and benchmark for 26x2x4 rules

* Parametrized benchmark
  • Loading branch information
tarilabs authored and lucamolteni committed Sep 9, 2020
1 parent 5b977a0 commit 0e820ce
Show file tree
Hide file tree
Showing 5 changed files with 383 additions and 35 deletions.
5 changes: 5 additions & 0 deletions kie-dmn/kie-dmn-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@
<artifactId>jmh-core</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>

<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-dmn-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@

package org.kie.dmn.core.alphasupport;

import java.io.StringWriter;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;
import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.dmn.api.core.DMNContext;
Expand All @@ -34,6 +41,7 @@
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
Expand All @@ -42,8 +50,8 @@

@BenchmarkMode(Mode.SingleShotTime)
@State(Scope.Thread)
@Warmup(iterations = 100)
@Measurement(iterations = 50)
@Warmup(iterations = 200)
@Measurement(iterations = 100)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public class DMNDecisionTableAlphaSupportingDraftBench {

Expand All @@ -53,11 +61,35 @@ public class DMNDecisionTableAlphaSupportingDraftBench {
private String existingCustomer;
private BigDecimal score;

@Param({"0", "1", "2", "3", "4", "5", "10", "15", "20", "30", "40", "52"})
private int alphalength;
private char[] alphabet;

@Setup()
public void init() {
public void init() throws Exception {
char[] az = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();
this.alphabet = Arrays.copyOf(az, alphalength);
System.setProperty("alphalength", Integer.toString(alphalength));

Configuration cfg = new Configuration(Configuration.VERSION_2_3_26);
cfg.setClassForTemplateLoading(DMNDecisionTableAlphaSupportingDraftBench.class, "");
cfg.setDefaultEncoding("UTF-8");
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);

Template temp = cfg.getTemplate("alphasupport.dmn.ftlh");
StringWriter out = new StringWriter();
Map<String, Object> root = new HashMap<>();
root.put("letters", alphabet);
temp.process(root, out);
String xml = out.getBuffer().toString();

LOG.debug("{}", xml);

final KieServices ks = KieServices.Factory.get();
final KieContainer kieContainer = KieHelper.getKieContainer(ks.newReleaseId("org.kie", "dmn-test-" + UUID.randomUUID(), "1.0"),
ks.getResources().newClassPathResource("alphasupport.dmn", DMNDecisionTableAlphaSupportingDraftBench.class));
ks.getResources()
.newByteArrayResource(xml.getBytes())
.setTargetPath("src/main/resources/alphasupport.dmn"));
runtime = kieContainer.newKieSession().getKieRuntime(DMNRuntime.class);
dmnModel = runtime.getModel("http://www.trisotech.com/definitions/_c0cf6e20-0b43-43ce-9def-c759a5f86df2", "DMN Specification Chapter 11 Example Reduced");
}
Expand All @@ -69,12 +101,11 @@ public void initIterationValues() {
}

public String existingCustomer() {
char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();
int randomIdx = new Random().nextInt(alphabet.length + 2);
if (randomIdx < alphabet.length - 2) {
if (randomIdx < alphabet.length) {
return String.valueOf(alphabet[randomIdx]);
} else {
return (randomIdx - 2) == 0 ? "true" : "false";
return (randomIdx - alphabet.length) == 0 ? "true" : "false";
}
}

Expand All @@ -93,10 +124,14 @@ public void testSimpleDecision() {
LOG.debug("{}", dmnResult);
}

public static void main(String[] args) {
public static void main(String[] args) throws Exception {
DMNDecisionTableAlphaSupportingDraftBench u = new DMNDecisionTableAlphaSupportingDraftBench();
u.alphalength = 2;
u.init();
u.initIterationValues();
u.doTest();
for (int i = 0; i < 1000; i++) {
u.initIterationValues();
u.doTest();
}
System.out.println("done.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package org.kie.dmn.core.compiler.alphanetbased;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

import org.drools.compiler.builder.impl.KnowledgeBuilderImpl;
import org.drools.core.base.ClassObjectType;
Expand Down Expand Up @@ -56,6 +58,7 @@
import static org.kie.dmn.feel.codegen.feel11.CompiledFEELSemanticMappings.includes;
import static org.kie.dmn.feel.codegen.feel11.CompiledFEELSemanticMappings.lt;
import static org.kie.dmn.feel.codegen.feel11.CompiledFEELSemanticMappings.range;

public class CompiledAlphaNetwork {

private final ResultCollector resultCollector = new ResultCollector();
Expand All @@ -73,6 +76,7 @@ private Object applyHitPolicy( List<Object> results ) {
}

public static final org.kie.dmn.feel.runtime.UnaryTest UT1 = (feelExprCtx, left) -> gracefulEq(feelExprCtx, "false", left);
public static final org.kie.dmn.feel.runtime.UnaryTest UT1x = (feelExprCtx, left) -> gracefulEq(feelExprCtx, "false", left);

public static final java.math.BigDecimal K_80 = new java.math.BigDecimal(80, java.math.MathContext.DECIMAL128);
public static final java.math.BigDecimal K_90 = new java.math.BigDecimal(90, java.math.MathContext.DECIMAL128);
Expand Down Expand Up @@ -119,30 +123,33 @@ public static CompiledAlphaNetwork generateCompiledNetwork() {
NetworkBuilderContext ctx = new NetworkBuilderContext();

Index index1 = createIndex(String.class, x -> (String)x.getValue("Existing Customer"), "false");
AlphaNode alphac1r1 = createAlphaNode(ctx, ctx.otn, x -> UT1.apply(x, x.getValue("Existing Customer")), index1);
AlphaNode alphac1r1 = createAlphaNode(ctx, ctx.otn, "\"false\"", x -> UT1.apply(x, x.getValue("Existing Customer")), index1);

AlphaNode alphac2r1 = createAlphaNode(ctx, alphac1r1, x -> UT2.apply(x, x.getValue("Application Risk Score")));
AlphaNode alphac2r1 = createAlphaNode(ctx, alphac1r1, "<100", x -> UT2.apply(x, x.getValue("Application Risk Score")));
addResultSink(ctx, network, alphac2r1, "HIGH");
AlphaNode alphac2r2 = createAlphaNode(ctx, alphac1r1, x -> UT3.apply(x, x.getValue("Application Risk Score")));
AlphaNode alphac2r2 = createAlphaNode(ctx, alphac1r1, "[100..120)", x -> UT3.apply(x, x.getValue("Application Risk Score")));
addResultSink(ctx, network, alphac2r2, "MEDIUM");
AlphaNode alphac2r3 = createAlphaNode(ctx, alphac1r1, x -> UT4.apply(x, x.getValue("Application Risk Score")));
AlphaNode alphac2r3 = createAlphaNode(ctx, alphac1r1, "[120..130]", x -> UT4.apply(x, x.getValue("Application Risk Score")));
addResultSink(ctx, network, alphac2r3, "LOW");
AlphaNode alphac2r4 = createAlphaNode(ctx, alphac1r1, x -> UT5.apply(x, x.getValue("Application Risk Score")));
AlphaNode alphac2r4 = createAlphaNode(ctx, alphac1r1, ">130", x -> UT5.apply(x, x.getValue("Application Risk Score")));
addResultSink(ctx, network, alphac2r4, "VERY LOW");

Index index2 = createIndex(String.class, x -> (String)x.getValue("Existing Customer"), "true");
AlphaNode alphac1r5 = createAlphaNode(ctx, ctx.otn, x -> UT6.apply(x, x.getValue("Existing Customer")), index2);
AlphaNode alphac1r5 = createAlphaNode(ctx, ctx.otn, "\"true\"", x -> UT6.apply(x, x.getValue("Existing Customer")), index2);

AlphaNode alphac2r5 = createAlphaNode(ctx, alphac1r5, x -> UT7.apply(x, x.getValue("Application Risk Score")));
AlphaNode alphac2r5 = createAlphaNode(ctx, alphac1r5, "<80", x -> UT7.apply(x, x.getValue("Application Risk Score")));
addResultSink(ctx, network, alphac2r5, "DECLINE");
AlphaNode alphac2r6 = createAlphaNode(ctx, alphac1r5, x -> UT8.apply(x, x.getValue("Application Risk Score")));
AlphaNode alphac2r6 = createAlphaNode(ctx, alphac1r5, "[80..90)", x -> UT8.apply(x, x.getValue("Application Risk Score")));
addResultSink(ctx, network, alphac2r6, "HIGH");
AlphaNode alphac2r7 = createAlphaNode(ctx, alphac1r5, x -> UT9.apply(x, x.getValue("Application Risk Score")));
AlphaNode alphac2r7 = createAlphaNode(ctx, alphac1r5, "[90..110]", x -> UT9.apply(x, x.getValue("Application Risk Score")));
addResultSink(ctx, network, alphac2r7, "MEDIUM");
AlphaNode alphac2r8 = createAlphaNode(ctx, alphac1r5, x -> UT10.apply(x, x.getValue("Application Risk Score")));
AlphaNode alphac2r8 = createAlphaNode(ctx, alphac1r5, ">110", x -> UT10.apply(x, x.getValue("Application Risk Score")));
addResultSink(ctx, network, alphac2r8, "LOW");

char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();
System.out.println(System.getProperty("alphalength"));
int alphalength = Integer.valueOf(System.getProperty("alphalength", "52"));
alphabet = Arrays.copyOf(alphabet, alphalength);
for (char c : alphabet) {
alphabet(network, ctx, String.valueOf(c));
}
Expand All @@ -157,35 +164,42 @@ public static CompiledAlphaNetwork generateCompiledNetwork() {
}

private static void alphabet(CompiledAlphaNetwork network, NetworkBuilderContext ctx, String sChar) {
System.out.println(sChar);
final org.kie.dmn.feel.runtime.UnaryTest UTx = (feelExprCtx, left) -> gracefulEq(feelExprCtx, sChar, left);
Index index1 = createIndex(String.class, x -> (String) x.getValue("Existing Customer"), sChar);
AlphaNode alphac1r1 = createAlphaNode(ctx, ctx.otn, x -> UTx.apply(x, x.getValue("Existing Customer")), index1);
AlphaNode alphac1r1 = createAlphaNode(ctx, ctx.otn, "\"" + sChar + "\"", x -> UTx.apply(x, x.getValue("Existing Customer")), index1);

AlphaNode alphac2r1 = createAlphaNode(ctx, alphac1r1, x -> UT2.apply(x, x.getValue("Application Risk Score")));
AlphaNode alphac2r1 = createAlphaNode(ctx, alphac1r1, "<100", x -> UT2.apply(x, x.getValue("Application Risk Score")));
addResultSink(ctx, network, alphac2r1, "HIGH");
AlphaNode alphac2r2 = createAlphaNode(ctx, alphac1r1, x -> UT3.apply(x, x.getValue("Application Risk Score")));
AlphaNode alphac2r2 = createAlphaNode(ctx, alphac1r1, "[100..120)", x -> UT3.apply(x, x.getValue("Application Risk Score")));
addResultSink(ctx, network, alphac2r2, "MEDIUM");
AlphaNode alphac2r3 = createAlphaNode(ctx, alphac1r1, x -> UT4.apply(x, x.getValue("Application Risk Score")));
AlphaNode alphac2r3 = createAlphaNode(ctx, alphac1r1, "[120..130]", x -> UT4.apply(x, x.getValue("Application Risk Score")));
addResultSink(ctx, network, alphac2r3, "LOW");
AlphaNode alphac2r4 = createAlphaNode(ctx, alphac1r1, x -> UT5.apply(x, x.getValue("Application Risk Score")));
AlphaNode alphac2r4 = createAlphaNode(ctx, alphac1r1, ">130", x -> UT5.apply(x, x.getValue("Application Risk Score")));
addResultSink(ctx, network, alphac2r4, "VERY LOW");
}

private static void addResultSink( NetworkBuilderContext ctx, CompiledAlphaNetwork network, ObjectSource source, Object result ) {
source.addObjectSink( new ResultCollectorAlphaSink( ctx.buildContext.getNextId(), source, ctx.buildContext, result, network.resultCollector ) );
}

private static AlphaNode createAlphaNode( NetworkBuilderContext ctx, ObjectSource source, Predicate1<EvaluationContext> predicate ) {
return createAlphaNode( ctx, source, predicate, null );
private static AlphaNode createAlphaNode(NetworkBuilderContext ctx, ObjectSource source, String id, Predicate1<EvaluationContext> predicate) {
return createAlphaNode( ctx, source, id, predicate, null );
}

@Deprecated
private static AlphaNode createAlphaNode( NetworkBuilderContext ctx, ObjectSource source, Predicate1<EvaluationContext> predicate, Index index ) {
SingleConstraint1 constraint = new SingleConstraint1(ctx.variable, predicate);
constraint.setIndex( index );
LambdaConstraint lambda = new LambdaConstraint(new ConstraintEvaluator(new Declaration[] { ctx.declaration }, constraint));
lambda.setType( Constraint.ConstraintType.ALPHA );
return attachNode( ctx.buildContext, new AlphaNode( ctx.buildContext.getNextId(), lambda, source, ctx.buildContext ) );
return createAlphaNode(ctx, source, UUID.randomUUID().toString(), predicate, null);
}

/**
* IMPORTANT: remember to use the FEEL expression as an Identifier for the same constraint
*/
private static AlphaNode createAlphaNode(NetworkBuilderContext ctx, ObjectSource source, String id, Predicate1<EvaluationContext> predicate, Index index) {
SingleConstraint1 constraint = new SingleConstraint1(id, ctx.variable, predicate);
constraint.setIndex(index);
LambdaConstraint lambda = new LambdaConstraint(new ConstraintEvaluator(new Declaration[]{ctx.declaration}, constraint));
lambda.setType(Constraint.ConstraintType.ALPHA);
return attachNode(ctx.buildContext, new AlphaNode(ctx.buildContext.getNextId(), lambda, source, ctx.buildContext));
}

private static <I> AlphaIndexImpl<EvaluationContext, I> createIndex( Class<I> indexedClass, Function1<EvaluationContext, I> leftExtractor, I rightValue) {
Expand Down
4 changes: 2 additions & 2 deletions kie-dmn/kie-dmn-core/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
</encoder>
</appender>

<logger name="org.kie" level="debug"/>
<logger name="org.drools.compiler.reteoo.compiled" level="debug"/>
<logger name="org.kie" level="info"/>
<logger name="org.drools.compiler.reteoo.compiled" level="info"/>
<logger name="org.kie.dmn.core.compiler.execmodelbased" level="info"/> <!-- useful default when moving generic org.kie to <info -->
<logger name="org.kie.dmn.feel.codegen" level="info"/> <!-- useful default when moving generic org.kie to <info -->

Expand Down
Loading

0 comments on commit 0e820ce

Please sign in to comment.