Add a benchmark for Buf.send()

Motivation:
This will likely be a somewhat common operation, as buffers move between eventloop and worker threads, so it's important to have an understanding of how it performs.

Modification:
Add a benchmark that specifically targets the send() operation on buffers.

Result:
We got benchmark numbers that clearly show the cost of confinement transfer
This commit is contained in:
Chris Vest 2020-12-04 16:27:08 +01:00
parent b0da25d888
commit 80185abec4

View File

@ -0,0 +1,68 @@
/*
* Copyright 2019 The Netty Project
*
* The Netty Project 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:
*
* https://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 io.netty.buffer.api.benchmarks;
import io.netty.buffer.api.Allocator;
import io.netty.buffer.api.Buf;
import io.netty.buffer.api.Send;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import static java.util.concurrent.CompletableFuture.completedFuture;
@Warmup(iterations = 10, time = 1)
@Measurement(iterations = 10, time = 1)
@Fork(value = 5, jvmArgsAppend = { "-XX:+UnlockDiagnosticVMOptions", "-XX:+DebugNonSafepoints" })
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@State(Scope.Benchmark)
public class SendBenchmark {
private static final Allocator nonPooled = Allocator.heap();
private static final Allocator pooled = Allocator.pooledHeap();
private static final Function<Send<Buf>, Send<Buf>> bounce = send -> {
try (Buf buf = send.receive()) {
return buf.send();
}
};
@Benchmark
public Buf sendNonPooled() throws Exception {
try (Buf buf = nonPooled.allocate(8)) {
try (Buf receive = completedFuture(buf.send()).thenApplyAsync(bounce).get().receive()) {
return receive;
}
}
}
@Benchmark
public Buf sendPooled() throws Exception {
try (Buf buf = pooled.allocate(8)) {
try (Buf receive = completedFuture(buf.send()).thenApplyAsync(bounce).get().receive()) {
return receive;
}
}
}
}