Skip to content
This repository was archived by the owner on Dec 12, 2022. It is now read-only.

Commit 45074e4

Browse files
committed
Try to speed up BufferTest with more parameter memoization and parallel execution
1 parent 56bfa22 commit 45074e4

File tree

3 files changed

+84
-30
lines changed

3 files changed

+84
-30
lines changed

src/test/java/io/netty/buffer/api/BufferTest.java

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -59,54 +59,54 @@
5959
import static org.junit.jupiter.api.Assertions.fail;
6060

6161
public class BufferTest {
62-
private static volatile Fixture[] fixtures;
6362
private static ExecutorService executor;
6463

65-
static Fixture[] allocators() {
66-
Fixture[] fxs = fixtures;
67-
if (fxs != null) {
68-
return fxs;
69-
}
70-
return fixtures = fixtureCombinations().toArray(Fixture[]::new);
71-
}
64+
private static final Memoize<Fixture[]> ALL_COMBINATIONS = new Memoize<>(
65+
() -> fixtureCombinations().toArray(Fixture[]::new));
66+
private static final Memoize<Fixture[]> NON_SLICED = new Memoize<>(
67+
() -> Arrays.stream(ALL_COMBINATIONS.get()).filter(f -> !f.isSlice()).toArray(Fixture[]::new));
68+
private static final Memoize<Fixture[]> NON_COMPOSITE = new Memoize<>(
69+
() -> Arrays.stream(ALL_COMBINATIONS.get()).filter(f -> !f.isComposite()).toArray(Fixture[]::new));
70+
private static final Memoize<Fixture[]> HEAP_ALLOCS = new Memoize<>(
71+
() -> Arrays.stream(ALL_COMBINATIONS.get()).filter(f -> f.isHeap()).toArray(Fixture[]::new));
72+
private static final Memoize<Fixture[]> DIRECT_ALLOCS = new Memoize<>(
73+
() -> Arrays.stream(ALL_COMBINATIONS.get()).filter(f -> f.isDirect()).toArray(Fixture[]::new));
74+
private static final Memoize<Fixture[]> POOLED_ALLOCS = new Memoize<>(
75+
() -> Arrays.stream(ALL_COMBINATIONS.get()).filter(f -> f.isPooled()).toArray(Fixture[]::new));
7276

73-
static List<Fixture> initialAllocators() {
74-
return List.of(
75-
new Fixture("heap", BufferAllocator::heap, HEAP),
76-
new Fixture("direct", BufferAllocator::direct, DIRECT, CLEANER),
77-
new Fixture("pooledHeap", BufferAllocator::pooledHeap, POOLED, HEAP),
78-
new Fixture("pooledDirect", BufferAllocator::pooledDirect, POOLED, DIRECT, CLEANER));
77+
static Fixture[] allocators() {
78+
return ALL_COMBINATIONS.get();
7979
}
8080

81-
static Stream<Fixture> nonSliceAllocators() {
82-
return fixtureCombinations().filter(f -> !f.isSlice());
81+
static Fixture[] nonSliceAllocators() {
82+
return NON_SLICED.get();
8383
}
8484

85-
static Stream<Fixture> nonCompositeAllocators() {
86-
return fixtureCombinations().filter(f -> !f.isComposite());
85+
static Fixture[] nonCompositeAllocators() {
86+
return NON_COMPOSITE.get();
8787
}
8888

89-
static Stream<Fixture> heapAllocators() {
90-
return fixtureCombinations().filter(Fixture::isHeap);
89+
static Fixture[] heapAllocators() {
90+
return HEAP_ALLOCS.get();
9191
}
9292

93-
static Stream<Fixture> directAllocators() {
94-
return fixtureCombinations().filter(Fixture::isDirect);
93+
static Fixture[] directAllocators() {
94+
return DIRECT_ALLOCS.get();
9595
}
9696

97-
static Stream<Fixture> directPooledAllocators() {
98-
return fixtureCombinations().filter(f -> f.isDirect() && f.isCleaner() && f.isPooled());
97+
static Fixture[] pooledAllocators() {
98+
return POOLED_ALLOCS.get();
9999
}
100100

101-
static Stream<Fixture> pooledAllocators() {
102-
return fixtureCombinations().filter(Fixture::isPooled);
101+
static List<Fixture> initialAllocators() {
102+
return List.of(
103+
new Fixture("heap", BufferAllocator::heap, HEAP),
104+
new Fixture("direct", BufferAllocator::direct, DIRECT, CLEANER),
105+
new Fixture("pooledHeap", BufferAllocator::pooledHeap, POOLED, HEAP),
106+
new Fixture("pooledDirect", BufferAllocator::pooledDirect, POOLED, DIRECT, CLEANER));
103107
}
104108

105109
private static Stream<Fixture> fixtureCombinations() {
106-
Fixture[] fxs = fixtures;
107-
if (fxs != null) {
108-
return Arrays.stream(fxs);
109-
}
110110
List<Fixture> initFixtures = initialAllocators();
111111
Builder<Fixture> builder = Stream.builder();
112112
initFixtures.forEach(builder);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2021 The Netty Project
3+
*
4+
* The Netty Project licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance
6+
* with the License. You may obtain a copy of the License at:
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
package io.netty.buffer.api;
17+
18+
import java.util.function.Supplier;
19+
20+
final class Memoize<T> implements Supplier<T> {
21+
private final Supplier<T> supplier;
22+
private volatile T memo;
23+
24+
Memoize(Supplier<T> supplier) {
25+
this.supplier = supplier;
26+
}
27+
28+
@Override
29+
public T get() {
30+
T val = memo;
31+
if (val == null) {
32+
memo = val = supplier.get();
33+
}
34+
return val;
35+
}
36+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright 2021 The Netty Project
2+
#
3+
# The Netty Project licenses this file to you under the Apache License,
4+
# version 2.0 (the "License"); you may not use this file except in compliance
5+
# with the License. You may obtain a copy of the License at:
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.
14+
junit.jupiter.execution.parallel.enabled = true
15+
junit.jupiter.execution.parallel.mode.default = concurrent
16+
junit.jupiter.testinstance.lifecycle.default = per_class
17+
junit.jupiter.execution.parallel.config.strategy = fixed
18+
junit.jupiter.execution.parallel.config.fixed.parallelism = 16

0 commit comments

Comments
 (0)