|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.flink.cdc.pipeline.tests.benchmark; |
| 19 | + |
| 20 | +import org.apache.flink.cdc.common.event.TableId; |
| 21 | +import org.apache.flink.cdc.common.schema.Selectors; |
| 22 | + |
| 23 | +import org.apache.flink.shaded.guava31.com.google.common.collect.ImmutableList; |
| 24 | + |
| 25 | +import org.openjdk.jmh.annotations.Benchmark; |
| 26 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 27 | +import org.openjdk.jmh.annotations.Fork; |
| 28 | +import org.openjdk.jmh.annotations.Level; |
| 29 | +import org.openjdk.jmh.annotations.Measurement; |
| 30 | +import org.openjdk.jmh.annotations.Mode; |
| 31 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 32 | +import org.openjdk.jmh.annotations.Scope; |
| 33 | +import org.openjdk.jmh.annotations.Setup; |
| 34 | +import org.openjdk.jmh.annotations.State; |
| 35 | +import org.openjdk.jmh.annotations.Threads; |
| 36 | +import org.openjdk.jmh.annotations.Warmup; |
| 37 | +import org.openjdk.jmh.runner.Runner; |
| 38 | +import org.openjdk.jmh.runner.options.Options; |
| 39 | +import org.openjdk.jmh.runner.options.OptionsBuilder; |
| 40 | + |
| 41 | +import java.util.List; |
| 42 | +import java.util.concurrent.TimeUnit; |
| 43 | + |
| 44 | +/** |
| 45 | + * Benchmark for table selector performance with and without cache. |
| 46 | + * |
| 47 | + * <pre> |
| 48 | + * Benchmark Mode Cnt Score Error Units |
| 49 | + * SelectorsBenchmark.testSelectorWithCache thrpt 20 1028.979 ± 218.663 ops/ms |
| 50 | + * SelectorsBenchmark.testSelectorWithoutCache thrpt 20 136.747 ± 11.872 ops/ms |
| 51 | + * </pre> |
| 52 | + */ |
| 53 | +@BenchmarkMode(Mode.Throughput) |
| 54 | +@OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 55 | +@State(Scope.Benchmark) |
| 56 | +@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) |
| 57 | +@Measurement(iterations = 10, time = 2, timeUnit = TimeUnit.SECONDS) |
| 58 | +@Fork(2) |
| 59 | +@Threads(2) |
| 60 | +public class SelectorsBenchmark { |
| 61 | + |
| 62 | + private Selectors selectors; |
| 63 | + private List<TableId> queryTableIds; |
| 64 | + |
| 65 | + @Setup(Level.Trial) |
| 66 | + public void setup() { |
| 67 | + selectors = |
| 68 | + new Selectors.SelectorsBuilder() |
| 69 | + .includeTables( |
| 70 | + "test_wms_inventory_[a-z]+.inventory_batch_detail," |
| 71 | + + "test_wms_inventory_[a-z]+.inventory_batch_detail_record," |
| 72 | + + "test_wms_inventory_[a-z]+.inventory_batch_input," |
| 73 | + + "test_wms_inventory_[a-z]+.inventory_flow_volume_level," |
| 74 | + + "test_wms_inventory_[a-z]+.inventory_snapshot," |
| 75 | + + "test_wms_log_[a-z]+.log_common_log_[a-z]+") |
| 76 | + .build(); |
| 77 | + |
| 78 | + queryTableIds = |
| 79 | + ImmutableList.of( |
| 80 | + TableId.tableId( |
| 81 | + "test_wms_common_europe.occupy_strategy_exe_progress_order"), |
| 82 | + TableId.tableId("test_wms_common_europe.wave_strategy_rule_relation"), |
| 83 | + TableId.tableId("db.sc2.A1"), |
| 84 | + TableId.tableId("db2.sc2.A1"), |
| 85 | + TableId.tableId("test_wms_output_s.out_moment_storage_location_relation"), |
| 86 | + TableId.tableId("test_wms_output_a.out_moment_storage_location_relation")); |
| 87 | + |
| 88 | + // warm up cache |
| 89 | + for (TableId id : queryTableIds) { |
| 90 | + selectors.isMatch(id); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Benchmark to evaluate the performance of table selector with caching enabled. |
| 96 | + * |
| 97 | + * <p>This benchmark measures throughput when using a pre-built {@link Selectors} instance that |
| 98 | + * leverages internal caching mechanisms. This simulates a typical usage scenario where selector |
| 99 | + * rules are initialized once and reused across multiple queries. |
| 100 | + * |
| 101 | + * <p>Expected to perform significantly better than non-cached version due to avoidance of |
| 102 | + * repeated regex parsing and compilation. |
| 103 | + */ |
| 104 | + @Benchmark |
| 105 | + public void testSelectorWithCache() { |
| 106 | + for (TableId id : queryTableIds) { |
| 107 | + selectors.isMatch(id); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Benchmark to evaluate the performance of table selector without using cache. |
| 113 | + * |
| 114 | + * <p>This benchmark constructs a new {@link Selectors} instance for each invocation, simulating |
| 115 | + * a cold-start or ad-hoc usage scenario. The overhead includes pattern parsing and matcher |
| 116 | + * construction, which significantly impacts throughput. |
| 117 | + * |
| 118 | + * <p>Useful for understanding worst-case performance and comparing against the cached version. |
| 119 | + */ |
| 120 | + @Benchmark |
| 121 | + public void testSelectorWithoutCache() { |
| 122 | + Selectors freshSelectors = |
| 123 | + new Selectors.SelectorsBuilder() |
| 124 | + .includeTables( |
| 125 | + "test_wms_inventory_[a-z]+.inventory_batch_detail," |
| 126 | + + "test_wms_inventory_[a-z]+.inventory_batch_detail_record," |
| 127 | + + "test_wms_inventory_[a-z]+.inventory_batch_input," |
| 128 | + + "test_wms_inventory_[a-z]+.inventory_flow_volume_level," |
| 129 | + + "test_wms_inventory_[a-z]+.inventory_snapshot," |
| 130 | + + "test_wms_log_[a-z]+.log_common_log_[a-z]+") |
| 131 | + .build(); |
| 132 | + for (TableId id : queryTableIds) { |
| 133 | + freshSelectors.isMatch(id); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + public static void main(String[] args) throws Exception { |
| 138 | + Options options = |
| 139 | + new OptionsBuilder() |
| 140 | + .include(SelectorsBenchmark.class.getSimpleName()) |
| 141 | + .detectJvmArgs() |
| 142 | + .build(); |
| 143 | + new Runner(options).run(); |
| 144 | + } |
| 145 | +} |
0 commit comments