liuliquan-marshal commented on code in PR #16864:
URL: https://github.com/apache/iceberg/pull/16864#discussion_r3486413517


##########
core/src/jmh/java/org/apache/iceberg/io/FileIOBenchmark.java:
##########
@@ -0,0 +1,373 @@
+/*
+ * 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.iceberg.io;
+
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.UncheckedIOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Random;
+import java.util.UUID;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicLong;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.LocalFileSystem;
+import org.apache.iceberg.hadoop.HadoopConfigurable;
+import org.apache.iceberg.hadoop.HadoopFileIO;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.BenchmarkMode;
+import org.openjdk.jmh.annotations.Fork;
+import org.openjdk.jmh.annotations.Level;
+import org.openjdk.jmh.annotations.Measurement;
+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.openjdk.jmh.annotations.Threads;
+import org.openjdk.jmh.annotations.Warmup;
+
+/**
+ * A benchmark that evaluates the raw read/write performance of FileIO 
implementations.
+ *
+ * <p>To run this benchmark with HadoopFileIO on local disk (default):
+ *
+ * <pre>{@code
+ * ./gradlew :iceberg-core:jmh -PjmhIncludeRegex=FileIOBenchmark
+ * }</pre>
+ *
+ * <p>To run with OSSFileIO:
+ *
+ * <pre>{@code
+ * ./gradlew :iceberg-core:jmh \
+ *   -Dbenchmark.fileIOClass=org.apache.iceberg.aliyun.oss.OSSFileIO \
+ *   -Dbenchmark.base.path=oss://bucket/benchmark-tmp/ \
+ *   -Doss.endpoint=https://oss-cn-hangzhou.aliyuncs.com \
+ *   -Dclient.access-key-id=xxx \
+ *   -Dclient.access-key-secret=xxx \
+ *   -PjmhIncludeRegex=FileIOBenchmark
+ * }</pre>
+ *
+ * <p>To run with S3FileIO:
+ *
+ * <pre>{@code
+ * ./gradlew :iceberg-core:jmh \
+ *   -Dbenchmark.fileIOClass=org.apache.iceberg.aws.s3.S3FileIO \
+ *   -Dbenchmark.base.path=s3://bucket/benchmark-tmp/ \
+ *   -Ds3.endpoint=https://s3.amazonaws.com \
+ *   -Ds3.access-key-id=xxx \
+ *   -Ds3.secret-access-key=xxx \
+ *   -PjmhIncludeRegex=FileIOBenchmark
+ * }</pre>
+ */
+@Fork(1)
+@Warmup(iterations = 3)
+@Measurement(iterations = 5)
+@BenchmarkMode({Mode.AverageTime, Mode.Throughput})
+@OutputTimeUnit(TimeUnit.MILLISECONDS)
+@State(Scope.Thread)
+public class FileIOBenchmark {
+
+  private static final String PROPERTIES_FILE = "benchmark-fileio.properties";
+
+  private static final String[] JVM_PROPERTY_PREFIXES = {
+    "java.", "sun.", "jdk.", "os.", "user.", "file.", "line.", "path.", "awt."
+  };
+
+  @Param("org.apache.iceberg.hadoop.HadoopFileIO")
+  private String fileIOClass;
+
+  @Param({"1", "64", "1024", "16384", "131072"})
+  private int fileSizeKB;
+
+  @Param({"4", "64", "256", "1024"})
+  private int bufferSizeKB;
+
+  private FileIO fileIO;
+  private String runDir;
+  private List<String> createdFiles;
+  private byte[] writeBuffer;
+  private byte[] readBuffer;
+  private String readFilePath;
+  private AtomicLong writeCounter;
+  private Random random;
+
+  @Setup(Level.Trial)
+  public void before() {
+    // allow system properties to override @Param values (e.g. 
-Dbenchmark.fileIOClass=...)
+    String fileIOOverride = System.getProperty("benchmark.fileIOClass");
+    if (fileIOOverride != null && !fileIOOverride.isEmpty()) {
+      fileIOClass = fileIOOverride;
+    }
+
+    Map<String, String> properties = loadProperties();
+
+    String basePath = properties.remove("benchmark.base.path");
+    if (basePath == null || basePath.isEmpty()) {
+      // default to local temp directory for HadoopFileIO
+      try {
+        basePath = 
Files.createTempDirectory("fileio-benchmark-").toAbsolutePath().toString();
+      } catch (IOException e) {
+        throw new UncheckedIOException("Failed to create temp directory", e);
+      }
+    }
+    // remove trailing slash
+    if (basePath.endsWith("/")) {
+      basePath = basePath.substring(0, basePath.length() - 1);
+    }
+
+    if (HadoopFileIO.class.getName().equals(fileIOClass)) {
+      Configuration conf = new Configuration();
+      conf.set("fs.file.impl", LocalFileSystem.class.getName());
+      conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
+      fileIO = new HadoopFileIO(conf);
+    } else {
+      try {
+        fileIO =
+            Class.forName(fileIOClass)
+                .asSubclass(FileIO.class)
+                .getDeclaredConstructor()
+                .newInstance();
+      } catch (ReflectiveOperationException e) {
+        throw new RuntimeException("Failed to create FileIO instance: " + 
fileIOClass, e);
+      }
+
+      if (fileIO instanceof HadoopConfigurable) {
+        ((HadoopConfigurable) fileIO).setConf(new Configuration());
+      }
+
+      fileIO.initialize(properties);
+    }
+
+    runDir = basePath + "/bench-" + UUID.randomUUID();
+    createdFiles = Lists.newArrayList();
+    writeCounter = new AtomicLong(0);
+    random = new Random(42);
+
+    // pre-allocate buffers
+    int bufSize = bufferSizeKB * 1024;
+    writeBuffer = new byte[bufSize];
+    random.nextBytes(writeBuffer);
+    readBuffer = new byte[bufSize];
+
+    // prepare test file for read benchmarks
+    readFilePath = runDir + "/read-test-file";
+    writeTestFile(readFilePath, fileSizeKB * 1024L);
+    createdFiles.add(readFilePath);
+  }
+
+  @TearDown(Level.Trial)
+  public void after() {
+    if (fileIO == null) {
+      return;
+    }
+
+    try {
+      // Try batch delete first if supported (e.g. native S3).
+      // Falls through to single-file cleanup on failure or if unsupported.
+      boolean batchDeleted = false;
+      if (fileIO instanceof SupportsPrefixOperations) {
+        try {
+          ((SupportsPrefixOperations) fileIO).deletePrefix(runDir);
+          batchDeleted = true;
+        } catch (Exception e) {
+          // Batch delete may fail on S3-compatible stores (e.g. Alibaba Cloud 
OSS),
+          // fall through to single-file deletion below.
+        }
+      }
+
+      if (!batchDeleted) {

Review Comment:
   Thanks for reviewing, addressed this comment in 
6e29bf897dc5ba236915593d948a9bafa2ab3af6.
   BTW, there are two another things:
   1. Support removing testing directory when testing with local-file 
HadoopFileIO. 
   2. Some s3-compatible storage(such as minio/Aliyun OSS) may occur errors 
when s3 bulk deleting because of no body MD5(s3 new feature). But concerning 
good compatibility should be assured by their own FileIO. So I removed the 
back-off effort when bulk delete fails. 
   PTAL.
   



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to