|
| 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.apache.iotdb.db.storageengine.dataregion.memtable; |
| 21 | + |
| 22 | +import org.apache.iotdb.commons.file.SystemFileFactory; |
| 23 | +import org.apache.iotdb.db.storageengine.dataregion.DataRegionInfo; |
| 24 | +import org.apache.iotdb.db.storageengine.dataregion.DataRegionTest; |
| 25 | +import org.apache.iotdb.db.storageengine.rescon.memory.SystemInfo; |
| 26 | +import org.apache.iotdb.db.utils.EnvironmentUtils; |
| 27 | +import org.apache.iotdb.db.utils.ManualPerformanceTestUtils; |
| 28 | +import org.apache.iotdb.db.utils.ManualPerformanceTestUtils.Measurement; |
| 29 | +import org.apache.iotdb.db.utils.ManualPerformanceTestUtils.Summary; |
| 30 | +import org.apache.iotdb.db.utils.constant.TestConstant; |
| 31 | + |
| 32 | +import org.apache.tsfile.file.metadata.TableSchema; |
| 33 | +import org.junit.Assert; |
| 34 | +import org.junit.Assume; |
| 35 | +import org.junit.Test; |
| 36 | + |
| 37 | +import java.io.File; |
| 38 | +import java.util.Locale; |
| 39 | +import java.util.concurrent.atomic.AtomicLong; |
| 40 | +import java.util.function.Function; |
| 41 | + |
| 42 | +public class TsFileTableSchemaRegistrationPerformanceTest { |
| 43 | + |
| 44 | + private static final String ENABLED_PROPERTY = "iotdb.tsfile.table.schema.perf.enabled"; |
| 45 | + private static final String WARMUP_ITERATIONS_PROPERTY = |
| 46 | + "iotdb.tsfile.table.schema.perf.warmup.iterations"; |
| 47 | + private static final String ITERATIONS_PROPERTY = "iotdb.tsfile.table.schema.perf.iterations"; |
| 48 | + private static final String ROUNDS_PROPERTY = "iotdb.tsfile.table.schema.perf.rounds"; |
| 49 | + private static volatile int benchmarkBlackhole; |
| 50 | + |
| 51 | + @Test |
| 52 | + public void tableSchemaRegistrationBenchmark() throws Exception { |
| 53 | + Assume.assumeTrue( |
| 54 | + String.format( |
| 55 | + "Manual performance UT. Enable with -D%s=true, optionally tune -D%s, -D%s and -D%s.", |
| 56 | + ENABLED_PROPERTY, WARMUP_ITERATIONS_PROPERTY, ITERATIONS_PROPERTY, ROUNDS_PROPERTY), |
| 57 | + Boolean.getBoolean(ENABLED_PROPERTY)); |
| 58 | + Assume.assumeTrue( |
| 59 | + "Current-thread CPU time and allocation metrics are required.", |
| 60 | + ManualPerformanceTestUtils.enableThreadMetrics()); |
| 61 | + |
| 62 | + final int warmupIterations = Integer.getInteger(WARMUP_ITERATIONS_PROPERTY, 20_000); |
| 63 | + final int iterations = Integer.getInteger(ITERATIONS_PROPERTY, 100_000); |
| 64 | + final int rounds = Integer.getInteger(ROUNDS_PROPERTY, 7); |
| 65 | + Assert.assertTrue(warmupIterations > 0); |
| 66 | + Assert.assertTrue(iterations > 0); |
| 67 | + Assert.assertTrue(rounds > 0); |
| 68 | + |
| 69 | + final String storageGroup = "root.vehicle"; |
| 70 | + final String systemDir = TestConstant.OUTPUT_DATA_DIR.concat("info"); |
| 71 | + final String filePath = TestConstant.getTestTsFilePath(storageGroup, 0, 0, 0); |
| 72 | + TsFileProcessor processor = null; |
| 73 | + EnvironmentUtils.envSetUp(); |
| 74 | + try { |
| 75 | + final File file = SystemFileFactory.INSTANCE.getFile(filePath); |
| 76 | + if (!file.getParentFile().exists()) { |
| 77 | + Assert.assertTrue(file.getParentFile().mkdirs()); |
| 78 | + } |
| 79 | + final DataRegionInfo regionInfo = |
| 80 | + new DataRegionInfo(new DataRegionTest.DummyDataRegion(systemDir, storageGroup)); |
| 81 | + processor = |
| 82 | + new TsFileProcessor( |
| 83 | + storageGroup, |
| 84 | + file, |
| 85 | + regionInfo, |
| 86 | + tsFileProcessor -> {}, |
| 87 | + (tsFileProcessor, updateMap, systemFlushTime) -> {}, |
| 88 | + true); |
| 89 | + final TsFileProcessorInfo processorInfo = new TsFileProcessorInfo(regionInfo); |
| 90 | + processor.setTsFileProcessorInfo(processorInfo); |
| 91 | + regionInfo.initTsFileProcessorInfo(processor); |
| 92 | + SystemInfo.getInstance().reportStorageGroupStatus(regionInfo, processor); |
| 93 | + |
| 94 | + final AtomicLong legacySupplierCalls = new AtomicLong(); |
| 95 | + final AtomicLong cachedSupplierCalls = new AtomicLong(); |
| 96 | + final Function<String, TableSchema> legacySupplier = |
| 97 | + tableName -> { |
| 98 | + legacySupplierCalls.incrementAndGet(); |
| 99 | + return new TableSchema(tableName); |
| 100 | + }; |
| 101 | + final Function<String, TableSchema> cachedSupplier = |
| 102 | + tableName -> { |
| 103 | + cachedSupplierCalls.incrementAndGet(); |
| 104 | + return new TableSchema(tableName); |
| 105 | + }; |
| 106 | + |
| 107 | + runLegacy(processor, warmupIterations, legacySupplier); |
| 108 | + runCached(processor, warmupIterations, cachedSupplier); |
| 109 | + legacySupplierCalls.set(0); |
| 110 | + cachedSupplierCalls.set(0); |
| 111 | + |
| 112 | + final Measurement[] legacyMeasurements = new Measurement[rounds]; |
| 113 | + final Measurement[] cachedMeasurements = new Measurement[rounds]; |
| 114 | + for (int round = 0; round < rounds; ++round) { |
| 115 | + if ((round & 1) == 0) { |
| 116 | + legacyMeasurements[round] = measureLegacy(processor, iterations, legacySupplier); |
| 117 | + cachedMeasurements[round] = measureCached(processor, iterations, cachedSupplier); |
| 118 | + } else { |
| 119 | + cachedMeasurements[round] = measureCached(processor, iterations, cachedSupplier); |
| 120 | + legacyMeasurements[round] = measureLegacy(processor, iterations, legacySupplier); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + final Summary legacySummary = |
| 125 | + ManualPerformanceTestUtils.summarize(legacyMeasurements, iterations); |
| 126 | + final Summary cachedSummary = |
| 127 | + ManualPerformanceTestUtils.summarize(cachedMeasurements, iterations); |
| 128 | + Assert.assertEquals((long) iterations * rounds, legacySupplierCalls.get()); |
| 129 | + Assert.assertEquals(0, cachedSupplierCalls.get()); |
| 130 | + printResult( |
| 131 | + warmupIterations, |
| 132 | + iterations, |
| 133 | + rounds, |
| 134 | + legacySummary, |
| 135 | + cachedSummary, |
| 136 | + legacySupplierCalls.get(), |
| 137 | + cachedSupplierCalls.get()); |
| 138 | + } finally { |
| 139 | + if (processor != null) { |
| 140 | + processor.syncClose(); |
| 141 | + } |
| 142 | + EnvironmentUtils.cleanEnv(); |
| 143 | + EnvironmentUtils.cleanDir(TestConstant.OUTPUT_DATA_DIR); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + private static Measurement measureLegacy( |
| 148 | + TsFileProcessor processor, |
| 149 | + int iterations, |
| 150 | + Function<String, TableSchema> tableSchemaFunction) { |
| 151 | + return ManualPerformanceTestUtils.measure( |
| 152 | + iterations, () -> legacyRegister(processor, tableSchemaFunction)); |
| 153 | + } |
| 154 | + |
| 155 | + private static Measurement measureCached( |
| 156 | + TsFileProcessor processor, |
| 157 | + int iterations, |
| 158 | + Function<String, TableSchema> tableSchemaFunction) { |
| 159 | + return ManualPerformanceTestUtils.measure( |
| 160 | + iterations, () -> cachedRegister(processor, tableSchemaFunction)); |
| 161 | + } |
| 162 | + |
| 163 | + private static void runLegacy( |
| 164 | + TsFileProcessor processor, |
| 165 | + int iterations, |
| 166 | + Function<String, TableSchema> tableSchemaFunction) { |
| 167 | + for (int i = 0; i < iterations; ++i) { |
| 168 | + legacyRegister(processor, tableSchemaFunction); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + private static void runCached( |
| 173 | + TsFileProcessor processor, |
| 174 | + int iterations, |
| 175 | + Function<String, TableSchema> tableSchemaFunction) { |
| 176 | + for (int i = 0; i < iterations; ++i) { |
| 177 | + cachedRegister(processor, tableSchemaFunction); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + private static void legacyRegister( |
| 182 | + TsFileProcessor processor, Function<String, TableSchema> tableSchemaFunction) { |
| 183 | + final String tableName = "legacy_table"; |
| 184 | + processor |
| 185 | + .getWriter() |
| 186 | + .getSchema() |
| 187 | + .getTableSchemaMap() |
| 188 | + .put(tableName, tableSchemaFunction.apply(tableName)); |
| 189 | + benchmarkBlackhole = processor.getWriter().getSchema().getTableSchemaMap().size(); |
| 190 | + } |
| 191 | + |
| 192 | + private static void cachedRegister( |
| 193 | + TsFileProcessor processor, Function<String, TableSchema> tableSchemaFunction) { |
| 194 | + processor.registerToTsFile("cached_table", 1, tableSchemaFunction); |
| 195 | + benchmarkBlackhole = processor.getWriter().getSchema().getTableSchemaMap().size(); |
| 196 | + } |
| 197 | + |
| 198 | + private static void printResult( |
| 199 | + int warmupIterations, |
| 200 | + int iterations, |
| 201 | + int rounds, |
| 202 | + Summary legacySummary, |
| 203 | + Summary cachedSummary, |
| 204 | + long legacySupplierCalls, |
| 205 | + long cachedSupplierCalls) { |
| 206 | + System.out.printf( |
| 207 | + Locale.ROOT, |
| 208 | + "TsFile table schema registration benchmark: warmup=%d, iterations=%d, rounds=%d%n" |
| 209 | + + "legacy: cpu=%.2f ns/op, allocation=%.2f bytes/op, supplierCalls=%d%n" |
| 210 | + + "cached: cpu=%.2f ns/op, allocation=%.2f bytes/op, supplierCalls=%d%n" |
| 211 | + + "reduction: cpu=%.2f%%, allocation=%.2f%%%n", |
| 212 | + warmupIterations, |
| 213 | + iterations, |
| 214 | + rounds, |
| 215 | + legacySummary.getCpuNanosPerOperation(), |
| 216 | + legacySummary.getAllocatedBytesPerOperation(), |
| 217 | + legacySupplierCalls, |
| 218 | + cachedSummary.getCpuNanosPerOperation(), |
| 219 | + cachedSummary.getAllocatedBytesPerOperation(), |
| 220 | + cachedSupplierCalls, |
| 221 | + reduction(legacySummary.getCpuNanosPerOperation(), cachedSummary.getCpuNanosPerOperation()), |
| 222 | + reduction( |
| 223 | + legacySummary.getAllocatedBytesPerOperation(), |
| 224 | + cachedSummary.getAllocatedBytesPerOperation())); |
| 225 | + } |
| 226 | + |
| 227 | + private static double reduction(double before, double after) { |
| 228 | + return before == 0 ? 0 : (before - after) * 100 / before; |
| 229 | + } |
| 230 | +} |
0 commit comments