szetszwo commented on code in PR #10438: URL: https://github.com/apache/ozone/pull/10438#discussion_r3383215308
########## hadoop-hdds/common/src/test/java/org/apache/hadoop/ozone/common/ChunkBufferPutBenchmark.java: ########## @@ -0,0 +1,346 @@ +/* + * 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.hadoop.ozone.common; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.text.DecimalFormat; +import java.util.concurrent.ThreadLocalRandom; +import org.apache.hadoop.ozone.common.JfrByteBufferAllocations.AllocationStats; + +/** + * Microbenchmark for ChunkBuffer.put(byte[]) direct copy vs ByteBuffer.wrap path. + * + * <p>Focused on the scenarios where HDDS-15485 shows the clearest benefit: + * <ul> + * <li>Throughput: 4KB stream fill with incremental buffer (64KB increment)</li> + * <li>Allocations: same 4KB / 64KB-increment incremental buffer path (JFR + wrap calls)</li> + * </ul> + * + * <p>Run from the repo root: + * <pre> + * mvn -pl hadoop-hdds/common -q test-compile exec:java \ + * -Dexec.mainClass=org.apache.hadoop.ozone.common.ChunkBufferPutBenchmark \ + * -Dexec.classpathScope=test \ + * -Dexec.args="--add-opens jdk.jfr/jdk.jfr=ALL-UNNAMED --add-opens jdk.jfr/jdk.jfr.consumer=ALL-UNNAMED" + * </pre> + * JFR ByteBuffer counts are sampled; put-op count reports exact wrap calls. + * Wrap-path timings use a blackhole on each {@code ByteBuffer.wrap} so the JVM + * cannot eliminate short-lived wrapper allocations via escape analysis. + */ +public final class ChunkBufferPutBenchmark { + + /** Prevents escape analysis from removing ByteBuffer.wrap allocations in the wrap path. */ + private static volatile Object blackhole; Review Comment: @yandrey321 , I am really appreciated you working the improvement. As you already known, Java has [Escape Analysis](https://medium.com/@houssyahya/escape-analysis-in-java-04039ee1dc8b) so the ByteBuffer objects actually are not allocated. Thus, there is no performance improvement here. I guess we should not add more code to avoid more bugs. Below is the result obtained by removing the blackhole: ``` ChunkBuffer.put(byte[]) microbenchmark (pre-allocated buffer, put-only) JVM: 17.0.19 on aarch64 === Throughput showcase === ... === Allocation showcase === --- Incremental buffer showcase --- Config: ozone.client.stream.buffer.size=4MB, ozone.client.stream.buffer.increment=64KB, io.file.buffer.size=4KB Pattern: 4KB stream fill into IncrementalChunkBuffer (64KB steps) Chunk=4096KB increment=64KB write=4KB direct put(byte[]): 46,555,136 put ops | 0 ByteBuffer TLAB allocs | 0 alloc bytes wrap put(ByteBuffer): 48,808,960 put ops | 900 ByteBuffer TLAB allocs | 50,400 alloc bytes ByteBuffer.wrap calls on wrap path (1 per put): 48,808,960 direct path avoids 48,808,960 ByteBuffer.wrap calls per run JFR confirms zero ByteBuffer TLAB allocations on direct path ⚠️⚠️⚠️ JFR sampled ByteBuffer TLAB allocations avoided on direct path: 900 ⚠️⚠️⚠️ (JFR samples TLAB events; put-op count is the exact wrap-call metric) ``` -- 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]
