Merge pull request #3 from netty/benchmarks

Add benchmarks for opening/closing shared/confined native/heap memory segments
This commit is contained in:
Chris Vest 2020-11-26 10:19:39 +01:00 committed by GitHub
commit f611d58a6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 99 additions and 2 deletions

View File

@ -386,6 +386,7 @@
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.18.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.netty</groupId>

View File

@ -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)) {

View File

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

View File

@ -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;
}
}
}