|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +package dev.vortex.spark.bench; |
| 5 | + |
| 6 | +import dev.vortex.api.Session; |
| 7 | +import dev.vortex.api.VortexWriter; |
| 8 | +import dev.vortex.relocated.org.apache.arrow.memory.BufferAllocator; |
| 9 | +import dev.vortex.relocated.org.apache.arrow.memory.RootAllocator; |
| 10 | +import dev.vortex.relocated.org.apache.arrow.vector.BigIntVector; |
| 11 | +import dev.vortex.relocated.org.apache.arrow.vector.Float8Vector; |
| 12 | +import dev.vortex.relocated.org.apache.arrow.vector.VarBinaryVector; |
| 13 | +import dev.vortex.relocated.org.apache.arrow.vector.VectorSchemaRoot; |
| 14 | +import dev.vortex.relocated.org.apache.arrow.vector.types.FloatingPointPrecision; |
| 15 | +import dev.vortex.relocated.org.apache.arrow.vector.types.pojo.ArrowType; |
| 16 | +import dev.vortex.relocated.org.apache.arrow.vector.types.pojo.Field; |
| 17 | +import dev.vortex.relocated.org.apache.arrow.vector.types.pojo.FieldType; |
| 18 | +import dev.vortex.relocated.org.apache.arrow.vector.types.pojo.Schema; |
| 19 | +import dev.vortex.spark.io.HadoopWritable; |
| 20 | +import java.io.IOException; |
| 21 | +import java.lang.management.ManagementFactory; |
| 22 | +import java.lang.management.MemoryPoolMXBean; |
| 23 | +import java.nio.file.Files; |
| 24 | +import java.nio.file.Path; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Random; |
| 27 | +import java.util.concurrent.TimeUnit; |
| 28 | +import org.apache.hadoop.conf.Configuration; |
| 29 | +import org.openjdk.jmh.annotations.AuxCounters; |
| 30 | +import org.openjdk.jmh.annotations.Benchmark; |
| 31 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 32 | +import org.openjdk.jmh.annotations.Fork; |
| 33 | +import org.openjdk.jmh.annotations.Level; |
| 34 | +import org.openjdk.jmh.annotations.Measurement; |
| 35 | +import org.openjdk.jmh.annotations.Mode; |
| 36 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 37 | +import org.openjdk.jmh.annotations.Param; |
| 38 | +import org.openjdk.jmh.annotations.Scope; |
| 39 | +import org.openjdk.jmh.annotations.Setup; |
| 40 | +import org.openjdk.jmh.annotations.State; |
| 41 | +import org.openjdk.jmh.annotations.TearDown; |
| 42 | +import org.openjdk.jmh.annotations.Warmup; |
| 43 | + |
| 44 | +/** |
| 45 | + * Cost of writing a Vortex file through {@link HadoopWritable}, the sink Spark's file-format writer streams native |
| 46 | + * output into. |
| 47 | + * |
| 48 | + * <p>Run with {@code -prof gc} for heap allocated per file. {@code oldGenPeakMiB} is the peak old-generation use in an |
| 49 | + * iteration, where G1 places arrays larger than half a region. JMH sums it over iterations in the summary, so read the |
| 50 | + * per-iteration values, for example from {@code -rf json}. |
| 51 | + */ |
| 52 | +@State(Scope.Benchmark) |
| 53 | +@BenchmarkMode(Mode.AverageTime) |
| 54 | +@OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 55 | +@Warmup(iterations = 5) |
| 56 | +@Measurement(iterations = 10) |
| 57 | +@Fork( |
| 58 | + value = 2, |
| 59 | + jvmArgsAppend = {"-Xmx2g", "-XX:+UseG1GC"}) |
| 60 | +public class HadoopWriteBenchmark { |
| 61 | + private static final int BATCH_ROWS = 8192; |
| 62 | + |
| 63 | + /** Rows per file: the larger file has bigger native output buffers. */ |
| 64 | + @Param({"262144", "4194304"}) |
| 65 | + private int rows; |
| 66 | + |
| 67 | + private Path directory; |
| 68 | + private Configuration conf; |
| 69 | + private Session session; |
| 70 | + private BufferAllocator allocator; |
| 71 | + private Schema schema; |
| 72 | + private VectorSchemaRoot batch; |
| 73 | + private String path; |
| 74 | + |
| 75 | + @Setup(Level.Trial) |
| 76 | + public void setUp() throws IOException { |
| 77 | + directory = Files.createTempDirectory("vortex-hadoop-write-bench"); |
| 78 | + path = directory.resolve("file.vortex").toString(); |
| 79 | + conf = new Configuration(); |
| 80 | + session = Session.create(); |
| 81 | + allocator = new RootAllocator(); |
| 82 | + schema = new Schema(List.of( |
| 83 | + new Field("id", FieldType.notNullable(new ArrowType.Int(64, true)), null), |
| 84 | + new Field("payload", FieldType.notNullable(new ArrowType.Binary()), null), |
| 85 | + new Field( |
| 86 | + "measure", |
| 87 | + FieldType.notNullable(new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE)), |
| 88 | + null))); |
| 89 | + batch = VectorSchemaRoot.create(schema, allocator); |
| 90 | + // Random values, so encoded output is close to the input size rather than compressed away. |
| 91 | + Random random = new Random(0); |
| 92 | + BigIntVector id = (BigIntVector) batch.getVector("id"); |
| 93 | + VarBinaryVector payload = (VarBinaryVector) batch.getVector("payload"); |
| 94 | + Float8Vector measure = (Float8Vector) batch.getVector("measure"); |
| 95 | + byte[] value = new byte[32]; |
| 96 | + for (int i = 0; i < BATCH_ROWS; i++) { |
| 97 | + id.setSafe(i, random.nextLong()); |
| 98 | + random.nextBytes(value); |
| 99 | + payload.setSafe(i, value); |
| 100 | + measure.setSafe(i, random.nextDouble()); |
| 101 | + } |
| 102 | + batch.setRowCount(BATCH_ROWS); |
| 103 | + } |
| 104 | + |
| 105 | + @TearDown(Level.Trial) |
| 106 | + public void tearDown() throws IOException { |
| 107 | + batch.close(); |
| 108 | + allocator.close(); |
| 109 | + SparkScanBenchmark.deleteRecursively(directory); |
| 110 | + } |
| 111 | + |
| 112 | + /** Peak G1 old-generation use per iteration, reset before each one. */ |
| 113 | + @State(Scope.Thread) |
| 114 | + @AuxCounters(AuxCounters.Type.EVENTS) |
| 115 | + public static class OldGenPeak { |
| 116 | + private MemoryPoolMXBean oldGen; |
| 117 | + public double oldGenPeakMiB; |
| 118 | + |
| 119 | + @Setup(Level.Trial) |
| 120 | + public void findPool() { |
| 121 | + oldGen = ManagementFactory.getMemoryPoolMXBeans().stream() |
| 122 | + .filter(pool -> pool.getName().equals("G1 Old Gen")) |
| 123 | + .findFirst() |
| 124 | + .orElseThrow(); |
| 125 | + } |
| 126 | + |
| 127 | + @Setup(Level.Iteration) |
| 128 | + public void reset() { |
| 129 | + System.gc(); |
| 130 | + oldGen.resetPeakUsage(); |
| 131 | + oldGenPeakMiB = 0; |
| 132 | + } |
| 133 | + |
| 134 | + void record() { |
| 135 | + oldGenPeakMiB = Math.max(oldGenPeakMiB, oldGen.getPeakUsage().getUsed() / (1024.0 * 1024.0)); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + @Benchmark |
| 140 | + public long writeFile(OldGenPeak peak) throws IOException { |
| 141 | + long size; |
| 142 | + try (HadoopWritable sink = HadoopWritable.create(conf, path); |
| 143 | + VortexWriter writer = |
| 144 | + VortexWriter.builder(session, sink, schema, allocator).build()) { |
| 145 | + for (int written = 0; written < rows; written += BATCH_ROWS) { |
| 146 | + writer.writeBatch(batch, allocator); |
| 147 | + } |
| 148 | + size = writer.finish().fileSize(); |
| 149 | + } |
| 150 | + peak.record(); |
| 151 | + return size; |
| 152 | + } |
| 153 | +} |
0 commit comments