channel warm up per thread
- Vorherrschende Sprache
- Java
- Sterne
- 12.1k
- Forks
- 4k
- Ø Merge
- 2 T. 17 Std.
- Gemergte PRs (30 T.)
- 37
Beschreibung
Hi, all. I'm doing an experiment with grpc and find out that the first usage of grpc in a thread costs much more time than the subsequent ones doing the same thing. I tested it on a 2-core laptop and 24-core machines, and the same phenomena occurred on all of them. So I'm wondering if I'm using grpc wrong or something in grpc-java could be improved to avoid such situation. Below is my code and part of the report.
```
import java.text.SimpleDateFormat;
import java.util.Date;
import me.gdbc.mocker.MockServer;
import me.proto.query.QueryProtocol.QueryRequest;
import me.proto.query.QueryProtocol.QueryResponse;
import me.proto.query.QueryServiceInterfaceGrpc.METHOD_QUERY;
import io.grpc.CallOptions;
import io.grpc.ManagedChannel;
import io.grpc.netty.NettyChannelBuilder;
public class WarmUp {
public static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static QueryRequest request = QueryRequest.newBuilder().setQueryString("").build();
public static class QueryThread extends Thread {
private String name;
private ManagedChannel client;
public QueryThread(String name, ManagedChannel client) {
this.name = name;
this.client = client;
}
public void run() {
for (int i = 0 ; i < 300; i++) {
long start = System.currentTimeMillis();
try {
//ClientCall call = client.newCall(METHOD_QUERY, CallOptions.DEFAULT);
QueryResponse ret = io.grpc.stub.ClientCalls.blockingUnaryCall(client, METHOD_QUERY, CallOptions.DEFAULT, request);
//QueryResponse ret = io.grpc.stub.ClientCalls.blockingUnaryCall(call, request);
long duration = System.currentTimeMillis() - start;
String show = "" + ret + "\n" + df.format(new Date()) + "\n" + "cost " + duration + "ms in thread " + name;
System.out.println(show);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
int port = 9012;
MockServer server = new MockServer(port);
try {
server.start();
ManagedChannel channel = NettyChannelBuilder.forAddress("127.0.0.1", port).usePlaintext(true).build();
io.grpc.stub.ClientCalls.blockingUnaryCall(channel, METHOD_QUERY, CallOptions.DEFAULT, request);
for (int i = 0; i < 4; i++) {
QueryThread th = new QueryThread(String.valueOf(i), channel);
th.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
```
May 01, 2016 2:28:09 PM me.gdbc.mocker.MockServer start
INFO: Server started, listenning on 9012
May 01, 2016 2:28:09 PM io.grpc.internal.ManagedChannelImpl
INFO: [ManagedChannelImpl@326de728] Created with target 127.0.0.1:9012
result {
props {
intVal: 1
}
}
2016-05-01 14:28:10
cost 76ms in thread 0
result {
props {
intVal: 1
}
}
2016-05-01 14:28:10
cost 67ms in thread 1
result {
props {
intVal: 1
}
}
2016-05-01 14:28:10
cost 68ms in thread 3
result {
props {
intVal: 1
}
}
2016-05-01 14:28:10
cost 66ms in thread 2
result {
props {
intVal: 1
}
}
2016-05-01 14:28:10
cost 8ms in thread 0
result {
props {
intVal: 1
}
}
2016-05-01 14:28:10
cost 8ms in thread 1
result {
props {
intVal: 1
}
}
2016-05-01 14:28:10
cost 10ms in thread 3
result {
props {
intVal: 1
}
}
2016-05-01 14:28:10
cost 10ms in thread 2
result {
props {
intVal: 1
}
}
2016-05-01 14:28:10
cost 7ms in thread 1
result {
props {
intVal: 1
}
}
2016-05-01 14:28:10
cost 7ms in thread 0
result {
props {
intVal: 1
}
}
2016-05-01 14:28:10
cost 8ms in thread 3
result {
props {
intVal: 1
}
}
2016-05-01 14:28:10
cost 10ms in thread 2
result {
props {
intVal: 1
}
}
2016-05-01 14:28:10
cost 7ms in thread 1
result {
props {
intVal: 1
}
}
2016-05-01 14:28:10
cost 7ms in thread 0
result {
props {
intVal: 1
}
}
2016-05-01 14:28:10
cost 7ms in thread 3
result {
props {
intVal: 1
}
}
2016-05-01 14:28:10
cost 4ms in thread 1
result {
props {
intVal: 1
}
}
2016-05-01 14:28:10
cost 6ms in thread 0
result {
props {
intVal: 1
}
}
2016-05-01 14:28:10
cost 7ms in thread 3
result {
props {
intVal: 1
}
}
2016-05-01 14:28:10
cost 7ms in thread 1
result {
props {
intVal: 1
}
}
2016-05-01 14:28:10
cost 7ms in thread 0
result {
props {
intVal: 1
}
}
2016-05-01 14:28:10
cost 6ms in thread 3
result {
props {
intVal: 1
}
}
2016-05-01 14:28:10
cost 6ms in thread 1
result {
props {
intVal: 1
}
}
2016-05-01 14:28:10
cost 6ms in thread 0
result {
props {
intVal: 1
}
}
2016-05-01 14:28:10
cost 6ms in thread 3
result {
props {
intVal: 1
}
}
```
Beitragsleitfaden
Rechercherichtung
Beginne damit, das mitgelieferte WarmUp-Beispiel mit NettyChannelBuilder, ManagedChannel und ClientCalls.blockingUnaryCall auszuführen, und untersuche anschließend die Channel- und Call-Pfade, die an der ersten Anfrage auf jedem Thread beteiligt sind. Als erledigt gilt die Aufgabe, wenn das Latenzverhalten pro Thread erklärt ist und jede Verbesserung durch eine reproduzierbare Regressionprüfung abgedeckt ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- java
- Bereich
- backend-api-design, networking, performance
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Muss geklärt werden
- Anfängerfreundlichkeit
- 32/100