grpc / grpc/grpc-java

channel warm up per thread

Abierto
#1,758 23 comentarios 1 reacción 0 asignados Ver en GitHub
performance
Lenguaje dominante
Java
Estrellas
12.1k
Forks
4k
Merge medio
2 d 17 h
PR fusionados (30 d)
37

Descripción

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
}
}
```

Guía de contribución

Abrir la guía de contribución

Línea de trabajo

Empieza ejecutando el ejemplo WarmUp proporcionado con NettyChannelBuilder, ManagedChannel y ClientCalls.blockingUnaryCall; después, inspecciona las rutas del canal y de la llamada involucradas en la primera solicitud de cada thread. El trabajo estará terminado cuando se explique el comportamiento de la latencia por thread y cualquier mejora esté cubierta por una comprobación de regresión reproducible.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
java
Área
backend-api-design, networking, performance
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Necesita aclaración
Aptitud para principiantes
32/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.