|
| 1 | +package org.apache.flink.cdc.pipeline.tests.benchmark; |
| 2 | + |
| 3 | +import org.apache.flink.cdc.common.event.TableId; |
| 4 | +import org.apache.flink.cdc.common.schema.Selectors; |
| 5 | + |
| 6 | +import org.apache.flink.shaded.guava31.com.google.common.collect.ImmutableList; |
| 7 | + |
| 8 | +import org.openjdk.jmh.annotations.Benchmark; |
| 9 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 10 | +import org.openjdk.jmh.annotations.Fork; |
| 11 | +import org.openjdk.jmh.annotations.Level; |
| 12 | +import org.openjdk.jmh.annotations.Measurement; |
| 13 | +import org.openjdk.jmh.annotations.Mode; |
| 14 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 15 | +import org.openjdk.jmh.annotations.Scope; |
| 16 | +import org.openjdk.jmh.annotations.Setup; |
| 17 | +import org.openjdk.jmh.annotations.State; |
| 18 | +import org.openjdk.jmh.annotations.Threads; |
| 19 | +import org.openjdk.jmh.annotations.Warmup; |
| 20 | +import org.openjdk.jmh.runner.Runner; |
| 21 | +import org.openjdk.jmh.runner.options.Options; |
| 22 | +import org.openjdk.jmh.runner.options.OptionsBuilder; |
| 23 | + |
| 24 | +import java.util.List; |
| 25 | +import java.util.concurrent.TimeUnit; |
| 26 | + |
| 27 | +/** |
| 28 | + * Benchmark for table selector performance with and without cache. |
| 29 | + * |
| 30 | + * <pre> |
| 31 | + * Benchmark Mode Cnt Score Error Units |
| 32 | + * SelectorsBenchmark.testSelectorWithCache thrpt 20 1028.979 ± 218.663 ops/ms |
| 33 | + * SelectorsBenchmark.testSelectorWithoutCache thrpt 20 136.747 ± 11.872 ops/ms |
| 34 | + * </pre> |
| 35 | + */ |
| 36 | +@BenchmarkMode(Mode.Throughput) |
| 37 | +@OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 38 | +@State(Scope.Benchmark) |
| 39 | +@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) |
| 40 | +@Measurement(iterations = 10, time = 2, timeUnit = TimeUnit.SECONDS) |
| 41 | +@Fork(2) |
| 42 | +@Threads(2) |
| 43 | +public class SelectorsBenchmark { |
| 44 | + |
| 45 | + private Selectors selectors; |
| 46 | + private List<TableId> queryTableIds; |
| 47 | + |
| 48 | + @Setup(Level.Trial) |
| 49 | + public void setup() { |
| 50 | + selectors = |
| 51 | + new Selectors.SelectorsBuilder() |
| 52 | + .includeTables( |
| 53 | + "test_wms_inventory_[a-z]+.inventory_batch_detail," |
| 54 | + + "test_wms_inventory_[a-z]+.inventory_batch_detail_record," |
| 55 | + + "test_wms_inventory_[a-z]+.inventory_batch_input," |
| 56 | + + "test_wms_inventory_[a-z]+.inventory_flow_volume_level," |
| 57 | + + "test_wms_inventory_[a-z]+.inventory_snapshot," |
| 58 | + + "test_wms_log_[a-z]+.log_common_log_[a-z]+") |
| 59 | + .build(); |
| 60 | + |
| 61 | + queryTableIds = |
| 62 | + ImmutableList.of( |
| 63 | + TableId.tableId( |
| 64 | + "test_wms_common_europe.occupy_strategy_exe_progress_order"), |
| 65 | + TableId.tableId("test_wms_common_europe.wave_strategy_rule_relation"), |
| 66 | + TableId.tableId("db.sc2.A1"), |
| 67 | + TableId.tableId("db2.sc2.A1"), |
| 68 | + TableId.tableId("test_wms_output_s.out_moment_storage_location_relation"), |
| 69 | + TableId.tableId("test_wms_output_a.out_moment_storage_location_relation")); |
| 70 | + |
| 71 | + // warm up cache |
| 72 | + for (TableId id : queryTableIds) { |
| 73 | + selectors.isMatch(id); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Benchmark to evaluate the performance of table selector with caching enabled. |
| 79 | + * |
| 80 | + * <p>This benchmark measures throughput when using a pre-built {@link Selectors} instance that |
| 81 | + * leverages internal caching mechanisms. This simulates a typical usage scenario where selector |
| 82 | + * rules are initialized once and reused across multiple queries. |
| 83 | + * |
| 84 | + * <p>Expected to perform significantly better than non-cached version due to avoidance of |
| 85 | + * repeated regex parsing and compilation. |
| 86 | + */ |
| 87 | + @Benchmark |
| 88 | + public void testSelectorWithCache() { |
| 89 | + for (TableId id : queryTableIds) { |
| 90 | + selectors.isMatch(id); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Benchmark to evaluate the performance of table selector without using cache. |
| 96 | + * |
| 97 | + * <p>This benchmark constructs a new {@link Selectors} instance for each invocation, simulating |
| 98 | + * a cold-start or ad-hoc usage scenario. The overhead includes pattern parsing and matcher |
| 99 | + * construction, which significantly impacts throughput. |
| 100 | + * |
| 101 | + * <p>Useful for understanding worst-case performance and comparing against the cached version. |
| 102 | + */ |
| 103 | + @Benchmark |
| 104 | + public void testSelectorWithoutCache() { |
| 105 | + Selectors freshSelectors = |
| 106 | + new Selectors.SelectorsBuilder() |
| 107 | + .includeTables( |
| 108 | + "test_wms_inventory_[a-z]+.inventory_batch_detail," |
| 109 | + + "test_wms_inventory_[a-z]+.inventory_batch_detail_record," |
| 110 | + + "test_wms_inventory_[a-z]+.inventory_batch_input," |
| 111 | + + "test_wms_inventory_[a-z]+.inventory_flow_volume_level," |
| 112 | + + "test_wms_inventory_[a-z]+.inventory_snapshot," |
| 113 | + + "test_wms_log_[a-z]+.log_common_log_[a-z]+") |
| 114 | + .build(); |
| 115 | + for (TableId id : queryTableIds) { |
| 116 | + freshSelectors.isMatch(id); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + public static void main(String[] args) throws Exception { |
| 121 | + Options options = |
| 122 | + new OptionsBuilder() |
| 123 | + .include(SelectorsBenchmark.class.getSimpleName()) |
| 124 | + .detectJvmArgs() |
| 125 | + .build(); |
| 126 | + new Runner(options).run(); |
| 127 | + } |
| 128 | +} |
0 commit comments