From eb7717b00a7226eead96e99c1fe95760036393d1 Mon Sep 17 00:00:00 2001 From: Chris Vest Date: Mon, 23 Nov 2020 18:10:27 +0100 Subject: [PATCH 1/4] Move benchmarks to their own directory --- .../buffer/api/{ => benchmarks}/MemSegBufAccessBenchmark.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) rename src/test/java/io/netty/buffer/api/{ => benchmarks}/MemSegBufAccessBenchmark.java (96%) diff --git a/src/test/java/io/netty/buffer/api/MemSegBufAccessBenchmark.java b/src/test/java/io/netty/buffer/api/benchmarks/MemSegBufAccessBenchmark.java similarity index 96% rename from src/test/java/io/netty/buffer/api/MemSegBufAccessBenchmark.java rename to src/test/java/io/netty/buffer/api/benchmarks/MemSegBufAccessBenchmark.java index 73307fc..e64b136 100644 --- a/src/test/java/io/netty/buffer/api/MemSegBufAccessBenchmark.java +++ b/src/test/java/io/netty/buffer/api/benchmarks/MemSegBufAccessBenchmark.java @@ -13,8 +13,10 @@ * License for the specific language governing permissions and limitations * under the License. */ -package io.netty.buffer.api; +package io.netty.buffer.api.benchmarks; +import io.netty.buffer.api.Allocator; +import io.netty.buffer.api.Buf; import org.openjdk.jmh.annotations.Benchmark; import org.openjdk.jmh.annotations.BenchmarkMode; import org.openjdk.jmh.annotations.Fork; From 92c178ceb9947529bccbb1536f0f0e4945ca5bad Mon Sep 17 00:00:00 2001 From: Chris Vest Date: Mon, 23 Nov 2020 18:10:58 +0100 Subject: [PATCH 2/4] The BufTest.pooledBuffersMustResetStateBeforeReuse should run for all allocators --- src/test/java/io/netty/buffer/api/BufTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/io/netty/buffer/api/BufTest.java b/src/test/java/io/netty/buffer/api/BufTest.java index 927f638..1add288 100644 --- a/src/test/java/io/netty/buffer/api/BufTest.java +++ b/src/test/java/io/netty/buffer/api/BufTest.java @@ -1513,7 +1513,7 @@ public class BufTest { } @ParameterizedTest - @MethodSource("poolingAllocators") + @MethodSource("allocators") public void pooledBuffersMustResetStateBeforeReuse(Fixture fixture) { try (Allocator allocator = fixture.createAllocator(); Buf expected = allocator.allocate(8)) { From cd9f84e85675c408da96717feecc695fcf8c57b0 Mon Sep 17 00:00:00 2001 From: Chris Vest Date: Mon, 23 Nov 2020 18:11:22 +0100 Subject: [PATCH 3/4] The assertj-core dependency should only be available in test scope --- pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/pom.xml b/pom.xml index 3fa799a..a3321b0 100644 --- a/pom.xml +++ b/pom.xml @@ -386,6 +386,7 @@ org.assertj assertj-core 3.18.0 + test io.netty From 607846572110796cf9fb5544c5e7a9f23dfb934f Mon Sep 17 00:00:00 2001 From: Chris Vest Date: Tue, 24 Nov 2020 10:56:22 +0100 Subject: [PATCH 4/4] Add a benchmark for opening and closing shared/confined native/heap memory segments --- .../MemorySegmentCloseBenchmark.java | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 src/test/java/io/netty/buffer/api/benchmarks/MemorySegmentCloseBenchmark.java diff --git a/src/test/java/io/netty/buffer/api/benchmarks/MemorySegmentCloseBenchmark.java b/src/test/java/io/netty/buffer/api/benchmarks/MemorySegmentCloseBenchmark.java new file mode 100644 index 0000000..03fa577 --- /dev/null +++ b/src/test/java/io/netty/buffer/api/benchmarks/MemorySegmentCloseBenchmark.java @@ -0,0 +1,94 @@ +/* + * 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 jdk.incubator.foreign.MemorySegment; +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.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 java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; + +@Warmup(iterations = 10, time = 1) +@Measurement(iterations = 10, time = 1) +@Fork(value = 5, jvmArgsAppend = { "-XX:+UnlockDiagnosticVMOptions", "-XX:+DebugNonSafepoints" }) +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.NANOSECONDS) +@State(Scope.Benchmark) +public class MemorySegmentCloseBenchmark { + @Param({"0", "10", "100"}) + public int unrelatedThreads; + + @Param({"16"/*, "124", "1024"*/}) + public int size; + + public ExecutorService unrelatedThreadPool; + public byte[] array; + + @Setup + public void setUp() { + unrelatedThreadPool = unrelatedThreads > 0? Executors.newFixedThreadPool(unrelatedThreads) : null; + array = new byte[size]; + } + + @TearDown + public void tearDown() throws InterruptedException { + if (unrelatedThreadPool != null) { + unrelatedThreadPool.shutdown(); + unrelatedThreadPool.awaitTermination(1, TimeUnit.MINUTES); + unrelatedThreadPool = null; + } + } + + @Benchmark + public MemorySegment heapConfined() { + try (MemorySegment segment = MemorySegment.ofArray(array)) { + return segment; + } + } + + @Benchmark + public MemorySegment heapShared() { + try (MemorySegment segment = MemorySegment.ofArray(array).share()) { + return segment; + } + } + + @Benchmark + public MemorySegment nativeConfined() { + try (MemorySegment segment = MemorySegment.allocateNative(size)) { + return segment; + } + } + + @Benchmark + public MemorySegment nativeShared() { + try (MemorySegment segment = MemorySegment.allocateNative(size).share()) { + return segment; + } + } +}