xunliu commented on code in PR #7717: URL: https://github.com/apache/gravitino/pull/7717#discussion_r2208927516
########## core/src/jmh/java/org/apache/gravitino/cache/it/AbstractEntityStorageBenchmark.java: ########## Review Comment: hi @Abyss-lord I think maybe we need move these files to `https://github.com/apache/gravitino/blob/main/core/src/test/java/org/apache/gravitino/cache/it`? ########## core/src/jmh/java/org/apache/gravitino/cache/it/AbstractEntityStorageBenchmark.java: ########## @@ -0,0 +1,330 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.gravitino.cache.it; + +import static org.apache.gravitino.Configs.DEFAULT_ENTITY_RELATIONAL_STORE; +import static org.apache.gravitino.Configs.ENTITY_RELATIONAL_JDBC_BACKEND_DRIVER; +import static org.apache.gravitino.Configs.ENTITY_RELATIONAL_JDBC_BACKEND_MAX_CONNECTIONS; +import static org.apache.gravitino.Configs.ENTITY_RELATIONAL_JDBC_BACKEND_PASSWORD; +import static org.apache.gravitino.Configs.ENTITY_RELATIONAL_JDBC_BACKEND_PATH; +import static org.apache.gravitino.Configs.ENTITY_RELATIONAL_JDBC_BACKEND_URL; +import static org.apache.gravitino.Configs.ENTITY_RELATIONAL_JDBC_BACKEND_USER; +import static org.apache.gravitino.Configs.ENTITY_RELATIONAL_JDBC_BACKEND_WAIT_MILLISECONDS; +import static org.apache.gravitino.Configs.ENTITY_RELATIONAL_STORE; +import static org.apache.gravitino.Configs.ENTITY_STORE; +import static org.apache.gravitino.Configs.RELATIONAL_ENTITY_STORE; +import static org.apache.gravitino.Configs.STORE_DELETE_AFTER_TIME; +import static org.apache.gravitino.Configs.VERSION_RETENTION_COUNT; +import static org.mockito.Mockito.mock; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Maps; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.UUID; +import java.util.concurrent.TimeUnit; +import org.apache.commons.io.FileUtils; +import org.apache.commons.lang3.reflect.FieldUtils; +import org.apache.gravitino.Catalog; +import org.apache.gravitino.Config; +import org.apache.gravitino.Configs; +import org.apache.gravitino.Entity; +import org.apache.gravitino.EntityStore; +import org.apache.gravitino.EntityStoreFactory; +import org.apache.gravitino.HasIdentifier; +import org.apache.gravitino.Namespace; +import org.apache.gravitino.SupportsRelationOperations; +import org.apache.gravitino.meta.BaseMetalake; +import org.apache.gravitino.meta.CatalogEntity; +import org.apache.gravitino.meta.ModelEntity; +import org.apache.gravitino.meta.RoleEntity; +import org.apache.gravitino.meta.SchemaEntity; +import org.apache.gravitino.meta.SchemaVersion; +import org.apache.gravitino.meta.TopicEntity; +import org.apache.gravitino.meta.UserEntity; +import org.apache.gravitino.storage.RandomIdGenerator; +import org.apache.gravitino.storage.relational.RelationalEntityStore; +import org.apache.gravitino.storage.relational.converters.H2ExceptionConverter; +import org.apache.gravitino.storage.relational.converters.SQLExceptionConverterFactory; +import org.apache.gravitino.utils.RandomNameUtils; +import org.apache.gravitino.utils.TestUtil; +import org.mockito.Mockito; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Level; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Param; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.TearDown; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testcontainers.shaded.com.google.common.collect.ImmutableMap; + +/** + * Benchmark base class for testing EntityStore implementations using JMH. + * + * <p>This abstract class provides a configurable setup for benchmarking relational entity store + * operations, such as put, get, and insertRelation. It uses H2 as an embedded JDBC backend and + * generates random metadata entities (models, users, topics, etc.) to simulate real-world usage. + * + * <p>Subclasses should implement JMH `@Benchmark` methods to benchmark desired operations. + */ +@BenchmarkMode({Mode.Throughput, Mode.AverageTime}) +@OutputTimeUnit(TimeUnit.SECONDS) +@State(Scope.Thread) +public class AbstractEntityStorageBenchmark<E extends Entity & HasIdentifier> { + protected static final Random random = new Random(); + private static final Logger LOG = + LoggerFactory.getLogger(AbstractEntityStorageBenchmark.class.getName()); + private static final String JDBC_STORE_PATH = + "/tmp/gravitino_jdbc_entityStore_benchmark_" + UUID.randomUUID().toString().replace("-", ""); + private static final String DB_DIR = JDBC_STORE_PATH + "/testdb"; + private static final String H2_FILE = DB_DIR + ".mv.db"; + private static final String BENCHMARK_METALAKE_NAME = "benchmark_metalake"; + private static final String BENCHMARK_CATALOG_NAME = "benchmark_catalog"; + private static final String BENCHMARK_SCHEMA_NAME = "benchmark_schema"; + private static RandomIdGenerator generator = new RandomIdGenerator(); + protected List<ModelEntity> entities; + protected Map<UserEntity, Entity> entitiesWithRelations; + protected EntityStore store; + protected BaseMetalake metalake; + protected CatalogEntity catalog; + protected SchemaEntity schema; + + @Param({"10", "100", "1000"}) + public int totalCnt; + + @Setup(Level.Trial) + public final void init() throws IOException { + initStore(); + initParentEntities(); + initBenchmarkEntities(); + } + + @TearDown(Level.Trial) + public void destroy() throws IOException { + try { + if (store != null) { + store.close(); + } + } catch (Exception e) { + LOG.warn("WARNING: Failed to close store during benchmark teardown: " + e.getMessage()); + } + File dir = new File(DB_DIR); + if (dir.exists()) { + dir.delete(); + } + + FileUtils.deleteQuietly(new File(H2_FILE)); + } + + /** + * Get test entities. + * + * @param entityCnt The number of entity to create. + * @return The list of test entities. + */ + protected List<ModelEntity> getEntities(int entityCnt) { + List<ModelEntity> entities = new ArrayList<>(entityCnt); + for (int i = 0; i < entityCnt; i++) { + entities.add( + TestUtil.getTestModelEntity( + generator.nextId(), + RandomNameUtils.genRandomName("model"), + Namespace.of( + BENCHMARK_METALAKE_NAME, BENCHMARK_CATALOG_NAME, BENCHMARK_SCHEMA_NAME))); + } + + return entities; + } + + /** + * Get relation entities. + * + * @param entityCnt The number of entities to create. + * @return The map of relation entities. + */ + protected Map<UserEntity, Entity> getRelationEntities(int entityCnt) { + Review Comment: Please remove this blank line. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
