Try to speed up BufferTest with more parameter memoization and parallel execution

This commit is contained in:
Chris Vest 2021-03-09 12:03:33 +01:00
parent 56bfa22d4a
commit 45074e4749
3 changed files with 88 additions and 34 deletions

View File

@ -59,15 +59,43 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
public class BufferTest {
private static volatile Fixture[] fixtures;
private static ExecutorService executor;
private static final Memoize<Fixture[]> ALL_COMBINATIONS = new Memoize<>(
() -> fixtureCombinations().toArray(Fixture[]::new));
private static final Memoize<Fixture[]> NON_SLICED = new Memoize<>(
() -> Arrays.stream(ALL_COMBINATIONS.get()).filter(f -> !f.isSlice()).toArray(Fixture[]::new));
private static final Memoize<Fixture[]> NON_COMPOSITE = new Memoize<>(
() -> Arrays.stream(ALL_COMBINATIONS.get()).filter(f -> !f.isComposite()).toArray(Fixture[]::new));
private static final Memoize<Fixture[]> HEAP_ALLOCS = new Memoize<>(
() -> Arrays.stream(ALL_COMBINATIONS.get()).filter(f -> f.isHeap()).toArray(Fixture[]::new));
private static final Memoize<Fixture[]> DIRECT_ALLOCS = new Memoize<>(
() -> Arrays.stream(ALL_COMBINATIONS.get()).filter(f -> f.isDirect()).toArray(Fixture[]::new));
private static final Memoize<Fixture[]> POOLED_ALLOCS = new Memoize<>(
() -> Arrays.stream(ALL_COMBINATIONS.get()).filter(f -> f.isPooled()).toArray(Fixture[]::new));
static Fixture[] allocators() {
Fixture[] fxs = fixtures;
if (fxs != null) {
return fxs;
}
return fixtures = fixtureCombinations().toArray(Fixture[]::new);
return ALL_COMBINATIONS.get();
}
static Fixture[] nonSliceAllocators() {
return NON_SLICED.get();
}
static Fixture[] nonCompositeAllocators() {
return NON_COMPOSITE.get();
}
static Fixture[] heapAllocators() {
return HEAP_ALLOCS.get();
}
static Fixture[] directAllocators() {
return DIRECT_ALLOCS.get();
}
static Fixture[] pooledAllocators() {
return POOLED_ALLOCS.get();
}
static List<Fixture> initialAllocators() {
@ -78,35 +106,7 @@ public class BufferTest {
new Fixture("pooledDirect", BufferAllocator::pooledDirect, POOLED, DIRECT, CLEANER));
}
static Stream<Fixture> nonSliceAllocators() {
return fixtureCombinations().filter(f -> !f.isSlice());
}
static Stream<Fixture> nonCompositeAllocators() {
return fixtureCombinations().filter(f -> !f.isComposite());
}
static Stream<Fixture> heapAllocators() {
return fixtureCombinations().filter(Fixture::isHeap);
}
static Stream<Fixture> directAllocators() {
return fixtureCombinations().filter(Fixture::isDirect);
}
static Stream<Fixture> directPooledAllocators() {
return fixtureCombinations().filter(f -> f.isDirect() && f.isCleaner() && f.isPooled());
}
static Stream<Fixture> pooledAllocators() {
return fixtureCombinations().filter(Fixture::isPooled);
}
private static Stream<Fixture> fixtureCombinations() {
Fixture[] fxs = fixtures;
if (fxs != null) {
return Arrays.stream(fxs);
}
List<Fixture> initFixtures = initialAllocators();
Builder<Fixture> builder = Stream.builder();
initFixtures.forEach(builder);

View File

@ -0,0 +1,36 @@
/*
* Copyright 2021 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;
import java.util.function.Supplier;
final class Memoize<T> implements Supplier<T> {
private final Supplier<T> supplier;
private volatile T memo;
Memoize(Supplier<T> supplier) {
this.supplier = supplier;
}
@Override
public T get() {
T val = memo;
if (val == null) {
memo = val = supplier.get();
}
return val;
}
}

View File

@ -0,0 +1,18 @@
# Copyright 2021 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.
junit.jupiter.execution.parallel.enabled = true
junit.jupiter.execution.parallel.mode.default = concurrent
junit.jupiter.testinstance.lifecycle.default = per_class
junit.jupiter.execution.parallel.config.strategy = fixed
junit.jupiter.execution.parallel.config.fixed.parallelism = 16