|
| 1 | +/* |
| 2 | + * Copyright Doma Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.seasar.doma.internal.expr; |
| 17 | + |
| 18 | +import static org.seasar.doma.internal.expr.ExpressionTokenType.EOE; |
| 19 | + |
| 20 | +import java.util.concurrent.TimeUnit; |
| 21 | +import org.openjdk.jmh.annotations.Benchmark; |
| 22 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 23 | +import org.openjdk.jmh.annotations.Mode; |
| 24 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 25 | +import org.openjdk.jmh.annotations.Param; |
| 26 | +import org.openjdk.jmh.annotations.Scope; |
| 27 | +import org.openjdk.jmh.annotations.Setup; |
| 28 | +import org.openjdk.jmh.annotations.State; |
| 29 | +import org.openjdk.jmh.infra.Blackhole; |
| 30 | + |
| 31 | +/** |
| 32 | + * JMH benchmark comparing performance between ExpressionTokenizer and ClassicExpressionTokenizer. |
| 33 | + */ |
| 34 | +@BenchmarkMode(Mode.AverageTime) |
| 35 | +@OutputTimeUnit(TimeUnit.NANOSECONDS) |
| 36 | +@State(Scope.Benchmark) |
| 37 | +public class ExpressionTokenizerBenchmark { |
| 38 | + |
| 39 | + // Test expression samples representing different complexity levels |
| 40 | + private static final String SIMPLE_EXPRESSION = "employee.name"; |
| 41 | + |
| 42 | + private static final String COMPLEX_EXPRESSION = |
| 43 | + "employee.getName().trim().toUpperCase() == \"JOHN\" && " |
| 44 | + + "(employee.getAge() >= 18 && employee.getAge() <= 65) || " |
| 45 | + + "employee.getStatus() != null && employee.getStatus().isActive()"; |
| 46 | + |
| 47 | + private static final String LITERAL_HEAVY_EXPRESSION = |
| 48 | + "\"Hello World\" + 123 + 45.67F + 89.01D + 123L + 456B + " |
| 49 | + + "true && false || null != \"test string with spaces\""; |
| 50 | + |
| 51 | + private static final String FUNCTION_HEAVY_EXPRESSION = |
| 52 | + "@prefix(name, \"Mr.\") + @suffix(name, \"Jr.\") + " |
| 53 | + + "@java.lang.String@valueOf(age) + @java.lang.Math@max(10, 20) + " |
| 54 | + + "@isEmpty(list) || @isNotEmpty(collection)"; |
| 55 | + |
| 56 | + private static final String OPERATOR_HEAVY_EXPRESSION = |
| 57 | + "!active && (x > 0 || y < 0) && z >= 10 && w <= 20 && " |
| 58 | + + "a == b && c != d && (e + f - g * h / i % j) > 0"; |
| 59 | + |
| 60 | + @Param({ |
| 61 | + "SIMPLE", |
| 62 | + "COMPLEX", |
| 63 | + "LITERAL_HEAVY", |
| 64 | + "FUNCTION_HEAVY", |
| 65 | + "OPERATOR_HEAVY", |
| 66 | + }) |
| 67 | + private String expressionType; |
| 68 | + |
| 69 | + private String expression; |
| 70 | + |
| 71 | + @Setup |
| 72 | + public void setup() { |
| 73 | + switch (expressionType) { |
| 74 | + case "SIMPLE": |
| 75 | + expression = SIMPLE_EXPRESSION; |
| 76 | + break; |
| 77 | + case "COMPLEX": |
| 78 | + expression = COMPLEX_EXPRESSION; |
| 79 | + break; |
| 80 | + case "LITERAL_HEAVY": |
| 81 | + expression = LITERAL_HEAVY_EXPRESSION; |
| 82 | + break; |
| 83 | + case "FUNCTION_HEAVY": |
| 84 | + expression = FUNCTION_HEAVY_EXPRESSION; |
| 85 | + break; |
| 86 | + case "OPERATOR_HEAVY": |
| 87 | + expression = OPERATOR_HEAVY_EXPRESSION; |
| 88 | + break; |
| 89 | + default: |
| 90 | + throw new IllegalArgumentException("Unknown expression type: " + expressionType); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + @Benchmark |
| 95 | + public void optimizedTokenizer(Blackhole bh) { |
| 96 | + ExpressionTokenizer tokenizer = new ExpressionTokenizer(expression); |
| 97 | + while (tokenizer.next() != EOE) { |
| 98 | + bh.consume(tokenizer.getToken()); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @Benchmark |
| 103 | + public void classicTokenizer(Blackhole bh) { |
| 104 | + ClassicExpressionTokenizer tokenizer = new ClassicExpressionTokenizer(expression); |
| 105 | + while (tokenizer.next() != EOE) { |
| 106 | + bh.consume(tokenizer.getToken()); |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments