This is an automated email from the ASF dual-hosted git repository.
RussellSpitzer pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/iceberg.git
The following commit(s) were added to refs/heads/main by this push:
new 4bdb2c2586 Spark: Add compaction only benchmark - rewrite data files
(#16219)
4bdb2c2586 is described below
commit 4bdb2c25867435b6acc90bb8b0b226e28c20ac53
Author: Varun Lakhyani <[email protected]>
AuthorDate: Mon May 18 20:33:40 2026 +0530
Spark: Add compaction only benchmark - rewrite data files (#16219)
---
.../spark/action/IcebergCompactionBenchmark.java | 150 +++++++++++++++++++++
.../action/IcebergDataCompactionBenchmark.java | 114 ++++++++++++++++
2 files changed, 264 insertions(+)
diff --git
a/spark/v4.1/spark/src/jmh/java/org/apache/iceberg/spark/action/IcebergCompactionBenchmark.java
b/spark/v4.1/spark/src/jmh/java/org/apache/iceberg/spark/action/IcebergCompactionBenchmark.java
new file mode 100644
index 0000000000..1da85e0c59
--- /dev/null
+++
b/spark/v4.1/spark/src/jmh/java/org/apache/iceberg/spark/action/IcebergCompactionBenchmark.java
@@ -0,0 +1,150 @@
+/*
+ * 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.spark.action;
+
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.nio.file.Files;
+import java.util.Map;
+import java.util.UUID;
+import java.util.concurrent.TimeUnit;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.spark.Spark3Util;
+import org.apache.iceberg.spark.TestBase;
+import org.apache.spark.sql.Dataset;
+import org.apache.spark.sql.Row;
+import org.apache.spark.sql.SaveMode;
+import org.apache.spark.sql.SparkSession;
+import org.openjdk.jmh.annotations.BenchmarkMode;
+import org.openjdk.jmh.annotations.Fork;
+import org.openjdk.jmh.annotations.Level;
+import org.openjdk.jmh.annotations.Mode;
+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.Timeout;
+
+@Fork(1)
+@State(Scope.Benchmark)
+@BenchmarkMode(Mode.SingleShotTime)
+@Timeout(time = 1, timeUnit = TimeUnit.HOURS)
+public abstract class IcebergCompactionBenchmark {
+
+ private final Configuration hadoopConf = initHadoopConf();
+ private SparkSession spark;
+
+ protected abstract String tableName();
+
+ protected abstract void initTable();
+
+ protected abstract void appendData();
+
+ @Setup
+ public void setupBench() {
+ setupSpark();
+ }
+
+ @TearDown
+ public void teardownBench() {
+ tearDownSpark();
+ }
+
+ @Setup(Level.Iteration)
+ public void setupIteration() {
+ initTable();
+ appendData();
+ }
+
+ @TearDown(Level.Iteration)
+ public void cleanUpIteration() throws IOException {
+ cleanupFiles();
+ }
+
+ protected Configuration initHadoopConf() {
+ return new Configuration();
+ }
+
+ protected final Table table() {
+ try {
+ return Spark3Util.loadIcebergTable(spark(), tableName());
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ protected final SparkSession spark() {
+ return spark;
+ }
+
+ protected String getCatalogWarehouse() {
+ try {
+ return Files.createTempDirectory("benchmark-").toAbsolutePath()
+ + "/"
+ + UUID.randomUUID()
+ + "/";
+ } catch (IOException e) {
+ throw new UncheckedIOException(e);
+ }
+ }
+
+ protected void cleanupFiles() throws IOException {
+ spark.sql("DROP TABLE IF EXISTS " + tableName());
+ }
+
+ /**
+ * Returns extra properties added to the Spark catalog during session setup.
+ *
+ * <p>The default implementation returns {@code type=hadoop} for a local
Hadoop catalog. Override
+ * to switch the catalog implementation or FileIO, e.g., with {@code
catalog-impl}, {@code
+ * io-impl}, and {@code warehouse} for an S3-backed run.
+ *
+ * @return a map of catalog properties to apply
+ */
+ protected Map<String, String> extraCatalogProperties() {
+ return Map.of("type", "hadoop");
+ }
+
+ protected String sparkMaster() {
+ return "local[*]";
+ }
+
+ protected void setupSpark() {
+ SparkSession.Builder builder =
+ SparkSession.builder()
+ .config(
+ "spark.sql.catalog.spark_catalog",
"org.apache.iceberg.spark.SparkSessionCatalog")
+ .config("spark.sql.catalog.spark_catalog.warehouse",
getCatalogWarehouse())
+ .config(TestBase.DISABLE_UI)
+ .master(sparkMaster());
+ extraCatalogProperties()
+ .forEach((key, value) ->
builder.config("spark.sql.catalog.spark_catalog." + key, value));
+ hadoopConf.forEach(entry -> builder.config("spark.hadoop." +
entry.getKey(), entry.getValue()));
+ spark = builder.getOrCreate();
+ }
+
+ protected void tearDownSpark() {
+ spark.stop();
+ }
+
+ protected void writeData(Dataset<Row> df) {
+ df.write().format("iceberg").mode(SaveMode.Append).save(tableName());
+ }
+}
diff --git
a/spark/v4.1/spark/src/jmh/java/org/apache/iceberg/spark/action/IcebergDataCompactionBenchmark.java
b/spark/v4.1/spark/src/jmh/java/org/apache/iceberg/spark/action/IcebergDataCompactionBenchmark.java
new file mode 100644
index 0000000000..1afd315a06
--- /dev/null
+++
b/spark/v4.1/spark/src/jmh/java/org/apache/iceberg/spark/action/IcebergDataCompactionBenchmark.java
@@ -0,0 +1,114 @@
+/*
+ * 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.spark.action;
+
+import static org.apache.iceberg.types.Types.NestedField.optional;
+import static org.apache.iceberg.types.Types.NestedField.required;
+import static org.apache.spark.sql.functions.col;
+import static org.apache.spark.sql.functions.concat;
+import static org.apache.spark.sql.functions.lit;
+
+import java.util.Collections;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.actions.SizeBasedFileRewritePlanner;
+import org.apache.iceberg.spark.Spark3Util;
+import org.apache.iceberg.spark.SparkSchemaUtil;
+import org.apache.iceberg.spark.SparkSessionCatalog;
+import org.apache.iceberg.spark.actions.SparkActions;
+import org.apache.iceberg.types.Types;
+import org.apache.spark.sql.Dataset;
+import org.apache.spark.sql.Row;
+import org.apache.spark.sql.connector.catalog.Identifier;
+import org.apache.spark.sql.connector.expressions.Transform;
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.Measurement;
+import org.openjdk.jmh.annotations.Param;
+import org.openjdk.jmh.annotations.Threads;
+import org.openjdk.jmh.annotations.Warmup;
+
+/**
+ * A benchmark that evaluates the performance of the rewrite data files action
in Spark.
+ *
+ * <p>To run this benchmark for spark-4.1: <code>
+ * ./gradlew :iceberg-spark:iceberg-spark-4.1_2.13:jmh
+ * -PjmhIncludeRegex=IcebergDataCompactionBenchmark.rewriteDataFiles
+ * -PjmhOutputPath=benchmark/data-compaction-benchmark-results.txt
+ * -PjmhJsonOutputPath=benchmark/data-compaction-benchmark-results.json
+ * </code>
+ */
+@Warmup(iterations = 3)
+@Measurement(iterations = 10)
+public class IcebergDataCompactionBenchmark extends IcebergCompactionBenchmark
{
+
+ private static final String[] NAMESPACE = new String[] {"default"};
+ private static final String NAME = "compactbench";
+ private static final Identifier IDENT = Identifier.of(NAMESPACE, NAME);
+ private static final long TOTAL_ROWS = 2_000_000L;
+
+ @Param({"250", "500", "1000", "2000"})
+ private int numFiles;
+
+ @Override
+ protected String tableName() {
+ return NAME;
+ }
+
+ @Benchmark
+ @Threads(1)
+ public void rewriteDataFiles() {
+ SparkActions.get()
+ .rewriteDataFiles(table())
+ .option(SizeBasedFileRewritePlanner.REWRITE_ALL, "true")
+ .execute();
+ }
+
+ @Override
+ protected final void initTable() {
+ Schema schema =
+ new Schema(
+ required(1, "intCol", Types.IntegerType.get()),
+ required(2, "stringCol", Types.StringType.get()),
+ optional(3, "nullCol", Types.StringType.get()));
+
+ SparkSessionCatalog<?> catalog;
+ try {
+ catalog =
+ (SparkSessionCatalog<?>)
+ Spark3Util.catalogAndIdentifier(spark(),
"spark_catalog").catalog();
+ catalog.dropTable(IDENT);
+ catalog.createTable(
+ IDENT, SparkSchemaUtil.convert(schema), new Transform[0],
Collections.emptyMap());
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Override
+ protected void appendData() {
+ Dataset<Row> df =
+ spark()
+ .range(0, TOTAL_ROWS)
+ .withColumn("intCol", col("id").cast("int"))
+ .withColumn("stringCol", concat(lit("foo_"), col("id")))
+ .withColumn("nullCol", lit(null).cast("string"))
+ .drop("id")
+ .repartition(numFiles);
+ writeData(df);
+ }
+}