Nikita-Shupletsov commented on code in PR #22997:
URL: https://github.com/apache/kafka/pull/22997#discussion_r4076625118


##########
jmh-benchmarks/src/main/java/org/apache/kafka/jmh/producer/ProducerAppendPathBenchmark.java:
##########
@@ -0,0 +1,320 @@
+/*
+ * 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.kafka.jmh.producer;
+
+import org.apache.kafka.clients.MetadataSnapshot;
+import org.apache.kafka.clients.producer.RecordMetadata;
+import org.apache.kafka.clients.producer.internals.BufferPool;
+import org.apache.kafka.clients.producer.internals.ChunkedRecordAccumulator;
+import org.apache.kafka.clients.producer.internals.ProducerBatch;
+import org.apache.kafka.clients.producer.internals.RecordAccumulator;
+import org.apache.kafka.common.Cluster;
+import org.apache.kafka.common.Node;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.compress.Compression;
+import org.apache.kafka.common.metrics.Metrics;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.record.internal.Record;
+import org.apache.kafka.common.requests.MetadataResponse;
+import org.apache.kafka.common.utils.Time;
+import org.apache.kafka.common.utils.internals.LogContext;
+
+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.OperationsPerInvocation;
+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.Warmup;
+import org.openjdk.jmh.infra.Blackhole;
+
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import java.util.concurrent.TimeUnit;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+/**
+ * Measures the producer's append path: {@link RecordAccumulator#append} and 
the
+ * {@link ChunkedRecordAccumulator} override of it. Run the same source on two 
revisions to detect
+ * regressions.
+ * <p>
+ * Results are nanoseconds per record appended and, under {@code -prof gc}, 
bytes allocated per record.
+ * The accumulator is built once per trial and drained between invocations, so 
appends are measured
+ * against a warm producer: partition map populated, buffers on the pool's 
free list. {@code nowMs} is
+ * read once per invocation.
+ * <p>
+ * Modes, each loading a different part of the path:
+ * <ul>
+ * <li>{@code steadyStateAppend} — one explicit partition, so nearly every 
record lands in the open
+ *     batch. Per-append work.</li>
+ * <li>{@code newBatchAppend} — one record per partition, so every append 
creates a batch. Per-batch
+ *     work: buffer acquisition and builder construction.</li>
+ * <li>{@code builtInPartitionerAppend} — no explicit partition, as a default 
producer sends. The only
+ *     mode reaching the built-in partitioner.</li>
+ * </ul>
+ * A slower append should show in {@code steadyStateAppend} and {@code 
builtInPartitionerAppend} but hardly
+ * in {@code newBatchAppend}; slower batch creation should show in {@code 
newBatchAppend} alone.
+ * <p>
+ * Strategies: {@code full} reserves a whole {@code batch.size} buffer per 
batch; {@code full-lz4} is
+ * the same compressed; {@code incremental} is {@link 
ChunkedRecordAccumulator} over an
+ * {@link BufferPool.AllocationMode#INCREMENTAL} pool, taking chunks on 
demand. Under {@code incremental}
+ * a batch extends when a record does not fit its attached chunks; larger 
batches and values load that
+ * path more heavily.
+ * <p>
+ * {@code full-lz4} appends a zero-filled value, so it measures the compressed 
path on a highly
+ * compressible payload.
+ * <p>
+ * TODO: extend to support compression under {@code incremental}.
+ * <p>
+ * Some examples of how to run it:
+ * <pre>
+ * # everything
+ * jmh-benchmarks/jmh.sh ProducerAppendPathBenchmark
+ * # both strategies, per-append cost only
+ * jmh-benchmarks/jmh.sh -p strategy=full,incremental 
ProducerAppendPathBenchmark.steadyStateAppend
+ * # load the incremental extension path
+ * jmh-benchmarks/jmh.sh -p strategy=incremental -p batchSize=262144 -p 
valueSize=8192 ProducerAppendPathBenchmark
+ * # allocation per record
+ * jmh-benchmarks/jmh.sh -prof gc ProducerAppendPathBenchmark
+ * </pre>
+ */
+@State(Scope.Benchmark)
+@Fork(value = 3, jvmArgs = {"-Xmx3g"})
+@Warmup(iterations = 5, time = 1)
+@Measurement(iterations = 10, time = 1)
+@BenchmarkMode(Mode.AverageTime)
+@OutputTimeUnit(TimeUnit.NANOSECONDS)
+public class ProducerAppendPathBenchmark {
+
+    private static final String TOPIC = "test";
+    private static final long TOTAL_MEMORY = 512 * 1024 * 1024L;
+    private static final int STEADY_STATE_RECORDS = 10_000;
+    /** One append per partition in {@code newBatchAppend}, so this is also 
that mode's record count. */
+    private static final int NUM_PARTITIONS = 500;
+    /** The single partition {@code steadyStateAppend} writes to. */
+    private static final int STEADY_STATE_PARTITION = 0;
+    private static final String FULL = "full";
+    private static final String FULL_LZ4 = "full-lz4";
+    private static final String INCREMENTAL = "incremental";
+
+    @Param({FULL, FULL_LZ4, INCREMENTAL})

Review Comment:
   as we are testing compression, would it make sense to leave a todo or 
something here to add compression for incremental strategy>



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