Skip to content

Commit 130d381

Browse files
benchmarks: use a fork-join pool to reduce executor contention
1 parent 09d7a41 commit 130d381

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

benchmarks/src/main/java/io/grpc/benchmarks/Utils.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@
3131

3232
package io.grpc.benchmarks;
3333

34+
import static java.util.concurrent.ForkJoinPool.defaultForkJoinWorkerThreadFactory;
35+
3436
import com.google.common.util.concurrent.ThreadFactoryBuilder;
37+
import com.google.common.util.concurrent.UncaughtExceptionHandlers;
3538
import com.google.protobuf.ByteString;
3639

3740
import io.grpc.ManagedChannel;
@@ -67,7 +70,11 @@
6770
import java.net.InetSocketAddress;
6871
import java.net.ServerSocket;
6972
import java.net.SocketAddress;
73+
import java.util.concurrent.ForkJoinPool;
74+
import java.util.concurrent.ForkJoinPool.ForkJoinWorkerThreadFactory;
75+
import java.util.concurrent.ForkJoinWorkerThread;
7076
import java.util.concurrent.ThreadFactory;
77+
import java.util.concurrent.atomic.AtomicInteger;
7178

7279
import javax.annotation.Nullable;
7380
import javax.net.ssl.SSLSocketFactory;
@@ -223,6 +230,21 @@ public static ManagedChannel newClientChannel(Transport transport, SocketAddress
223230
}
224231
if (directExecutor) {
225232
builder.directExecutor();
233+
} else {
234+
// TODO(carl-mastrangelo): This should not be necessary. I don't know where this should be
235+
// put. Move it somewhere else, or remove it if no longer necessary.
236+
// See: https://github.com/grpc/grpc-java/issues/2119
237+
builder.executor(new ForkJoinPool(Runtime.getRuntime().availableProcessors(),
238+
new ForkJoinWorkerThreadFactory() {
239+
final AtomicInteger num = new AtomicInteger();
240+
@Override
241+
public ForkJoinWorkerThread newThread(ForkJoinPool pool) {
242+
ForkJoinWorkerThread thread = defaultForkJoinWorkerThreadFactory.newThread(pool);
243+
thread.setDaemon(true);
244+
thread.setName("grpc-server-app-" + "-" + num.getAndIncrement());
245+
return thread;
246+
}
247+
}, UncaughtExceptionHandlers.systemExit(), true /* async */));
226248
}
227249
return builder.build();
228250
}

benchmarks/src/main/java/io/grpc/benchmarks/qps/AsyncServer.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131

3232
package io.grpc.benchmarks.qps;
3333

34+
import com.google.common.util.concurrent.UncaughtExceptionHandlers;
35+
3436
import io.grpc.Server;
3537
import io.grpc.benchmarks.Utils;
3638
import io.grpc.benchmarks.proto.BenchmarkServiceGrpc;
@@ -50,7 +52,11 @@
5052

5153
import java.io.File;
5254
import java.io.IOException;
55+
import java.util.concurrent.ForkJoinPool;
56+
import java.util.concurrent.ForkJoinPool.ForkJoinWorkerThreadFactory;
57+
import java.util.concurrent.ForkJoinWorkerThread;
5358
import java.util.concurrent.TimeUnit;
59+
import java.util.concurrent.atomic.AtomicInteger;
5460

5561
/**
5662
* QPS server using the non-blocking API.
@@ -172,6 +178,22 @@ static Server newServer(ServerConfiguration config) throws IOException {
172178
.flowControlWindow(config.flowControlWindow);
173179
if (config.directExecutor) {
174180
builder.directExecutor();
181+
} else {
182+
// TODO(carl-mastrangelo): This should not be necessary. I don't know where this should be
183+
// put. Move it somewhere else, or remove it if no longer necessary.
184+
// See: https://github.com/grpc/grpc-java/issues/2119
185+
builder.executor(new ForkJoinPool(Runtime.getRuntime().availableProcessors(),
186+
new ForkJoinWorkerThreadFactory() {
187+
final AtomicInteger num = new AtomicInteger();
188+
@Override
189+
public ForkJoinWorkerThread newThread(ForkJoinPool pool) {
190+
ForkJoinWorkerThread thread =
191+
ForkJoinPool.defaultForkJoinWorkerThreadFactory.newThread(pool);
192+
thread.setDaemon(true);
193+
thread.setName("grpc-server-app-" + "-" + num.getAndIncrement());
194+
return thread;
195+
}
196+
}, UncaughtExceptionHandlers.systemExit(), true /* async */));
175197
}
176198

177199
return builder.build();

0 commit comments

Comments
 (0)