|
| 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.dmn.ast; |
| 21 | + |
| 22 | +import org.antlr.v4.runtime.tree.ParseTree; |
| 23 | +import org.kie.dmn.feel.lang.EvaluationContext; |
| 24 | +import org.kie.dmn.feel.lang.FEELDialect; |
| 25 | +import org.kie.dmn.feel.lang.Type; |
| 26 | +import org.kie.dmn.feel.lang.ast.BaseNode; |
| 27 | +import org.kie.dmn.feel.lang.impl.EvaluationContextImpl; |
| 28 | +import org.kie.dmn.feel.parser.feel11.ASTBuilderVisitor; |
| 29 | +import org.kie.dmn.feel.parser.feel11.FEELParser; |
| 30 | +import org.kie.dmn.feel.parser.feel11.FEEL_1_1Parser; |
| 31 | +import org.kie.dmn.feel.util.ClassLoaderUtil; |
| 32 | +import org.openjdk.jmh.annotations.Benchmark; |
| 33 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 34 | +import org.openjdk.jmh.annotations.Measurement; |
| 35 | +import org.openjdk.jmh.annotations.Mode; |
| 36 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 37 | +import org.openjdk.jmh.annotations.Scope; |
| 38 | +import org.openjdk.jmh.annotations.Setup; |
| 39 | +import org.openjdk.jmh.annotations.State; |
| 40 | +import org.openjdk.jmh.annotations.Warmup; |
| 41 | + |
| 42 | +import java.util.Collections; |
| 43 | +import java.util.Map; |
| 44 | +import java.util.concurrent.TimeUnit; |
| 45 | + |
| 46 | +@BenchmarkMode(Mode.AverageTime) |
| 47 | +@State(Scope.Thread) |
| 48 | +@Warmup(iterations = 100, time = 200, timeUnit = TimeUnit.MILLISECONDS) |
| 49 | +@Measurement(iterations = 20, time = 200, timeUnit = TimeUnit.MILLISECONDS) |
| 50 | +@OutputTimeUnit(TimeUnit.MICROSECONDS) |
| 51 | +public abstract class AbstractASTBenchmark { |
| 52 | + |
| 53 | + private BaseNode baseNode; |
| 54 | + |
| 55 | + @Setup() |
| 56 | + public void setupBaseNode() { |
| 57 | + baseNode = getBaseNode(getBaseNodeExpression()); |
| 58 | + } |
| 59 | + |
| 60 | + protected abstract String getBaseNodeExpression(); |
| 61 | + |
| 62 | + @Benchmark |
| 63 | + public Object evaluateBaseNodeBenchmark() { |
| 64 | + return baseNode.evaluate(newEmptyEvaluationContext()); |
| 65 | + } |
| 66 | + |
| 67 | + private static EvaluationContext newEmptyEvaluationContext() { |
| 68 | + // Defaulting FEELDialect to FEEL |
| 69 | + return new EvaluationContextImpl(ClassLoaderUtil.findDefaultClassLoader(), null, FEELDialect.FEEL); |
| 70 | + } |
| 71 | + |
| 72 | + private static BaseNode getBaseNode(String baseNodeExpression) { |
| 73 | + Map<String, Type> inputTypes = Collections.emptyMap(); |
| 74 | + FEEL_1_1Parser parser = FEELParser.parse(null, baseNodeExpression, inputTypes, Collections.emptyMap(), Collections.emptyList(), Collections.emptyList(), null); |
| 75 | + |
| 76 | + ParseTree tree = parser.compilation_unit(); |
| 77 | + |
| 78 | + ASTBuilderVisitor v = new ASTBuilderVisitor(inputTypes, null); |
| 79 | + return tree.accept(v); |
| 80 | + } |
| 81 | + |
| 82 | +} |
0 commit comments