yuqi1129 commented on code in PR #7501:
URL: https://github.com/apache/gravitino/pull/7501#discussion_r2182696290


##########
gradle/libs.versions.toml:
##########
@@ -121,6 +121,8 @@ aliyun-credentials = "0.3.12"
 openlineage = "1.29.0"
 concurrent-trees = "2.6.0"
 jcstress = "0.8.15"
+jmh-plugin = "0.7.3"
+jmh = "1.37"

Review Comment:
   Where is the value used?



##########
core/src/jmh/java/org/apache/gravitino/cache/BenchmarkHelper.java:
##########
@@ -0,0 +1,106 @@
+/*
+ * 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;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Maps;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Random;
+import org.apache.gravitino.Entity;
+import org.apache.gravitino.HasIdentifier;
+import org.apache.gravitino.meta.ModelEntity;
+import org.apache.gravitino.meta.RoleEntity;
+import org.apache.gravitino.utils.TestUtil;
+
+public class BenchmarkHelper {
+  public static final int RELATION_CNT = 5;
+  private static final Random random = new Random();
+
+  private BenchmarkHelper() {
+    // Utility class, no constructor needed
+  }
+
+  /**
+   * Generates a map of entities with their relations.
+   *
+   * @param entityCnt the count of entities to generate.
+   * @return a map of entities with their relations.
+   * @param <E> the type of the entity.
+   */
+  public static <E extends Entity & HasIdentifier> Map<RoleEntity, List<E>> 
getRelationEntities(
+      int entityCnt) {
+    Map<RoleEntity, List<E>> entitiesWithRelations = Maps.newHashMap();
+    for (int i = 0; i < entityCnt; i++) {
+      RoleEntity testRoleEntity = TestUtil.getTestRoleEntity();
+      int userCnt = random.nextInt(RELATION_CNT);
+      List<E> userList = 
BaseEntityCache.convertEntities(getUserList(testRoleEntity, userCnt));
+      entitiesWithRelations.put(testRoleEntity, userList);
+    }
+
+    return entitiesWithRelations;
+  }
+
+  /**
+   * Generates a list of entities with the specified count.
+   *
+   * @param entityCnt the count of entities to generate.
+   * @return a list of model entities.
+   */
+  public static List<ModelEntity> getEntities(int entityCnt) {
+    List<ModelEntity> entities = new ArrayList<>(entityCnt);
+    for (int i = 0; i < entityCnt; i++) {
+      entities.add(TestUtil.getTestModelEntity());
+    }
+
+    return entities;
+  }
+
+  /**
+   * Returns a randomly selected key from the given map.
+   *
+   * @param map the input map from which to select a random key
+   * @param <K> the type of keys in the map
+   * @param <V> the type of values in the map
+   * @return a randomly selected key from the map, or {@code null} if the map 
is null or empty
+   */
+  public static <K, V> K getRandomKey(Map<K, V> map) {
+    if (map == null || map.isEmpty()) {
+      return null;
+    }
+
+    List<K> keys = new ArrayList<>(map.keySet());
+    Random random = new Random();

Review Comment:
   You can reuse the `random` defined in L36



##########
core/src/jmh/java/org/apache/gravitino/cache/AbstractEntityBenchmark.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * 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;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Random;
+import java.util.concurrent.TimeUnit;
+import org.apache.gravitino.Config;
+import org.apache.gravitino.Entity;
+import org.apache.gravitino.HasIdentifier;
+import org.apache.gravitino.SupportsRelationOperations;
+import org.apache.gravitino.meta.ModelEntity;
+import org.apache.gravitino.meta.RoleEntity;
+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;
+
+/**
+ * AbstractEntityBenchmark is a JMH-based benchmark base class designed to 
evaluate the performance
+ * of the {@link EntityCache} implementation in Gravitino.
+ *
+ * <p>This class sets up a controlled environment where a configurable number 
of entities and their
+ * associated relations are preloaded into the cache before each iteration. It 
supports benchmarking
+ * cache operations such as put, get, and relation-based access under varying 
data volumes.
+ *
+ * <p>Features:
+ *
+ * <ul>
+ *   <li>Supports benchmarking with different entity counts (e.g., 10, 100, 
1000)
+ *   <li>Measures both throughput and average execution time using JMH modes
+ *   <li>Preloads both simple entities and relation entities (e.g., user-role 
mappings)
+ * </ul>
+ *
+ * <p>Subclasses should implement benchmark methods to evaluate specific cache 
operations.
+ *
+ * @param <E> the type of entity under test, must implement {@link Entity} and 
{@link HasIdentifier}
+ * @see org.apache.gravitino.cache.CaffeineEntityCache
+ * @see org.openjdk.jmh.annotations.Benchmark
+ * @see org.apache.gravitino.cache.BenchmarkHelper
+ */
+@BenchmarkMode({Mode.Throughput, Mode.AverageTime})
+@OutputTimeUnit(TimeUnit.SECONDS)
+@State(Scope.Thread)
+public abstract class AbstractEntityBenchmark<E extends Entity & 
HasIdentifier> {
+  @Param({"10", "100", "1000"})

Review Comment:
   The size seems to be quite small. Is this the total number of entities 
stored in `entityStore`?



-- 
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]

Reply via email to