gRPC - 请求响应性能

gRPC - request response performance

gRPC 1.1.0 C# .NET 4.6 Windows 7 / Windows 10

我刚刚在C#中测试了gRPC的性能,对它在计算机之间的性能感到困惑。小消息需要一致的 200 毫秒 send/reply 时间,而较大的消息(大约 1500 个字符)是亚毫秒。请参阅下面的 client/server 代码。是否需要额外的配置来处理小消息?

我的测试遵循此处的入门指南:http://www.grpc.io/docs/quickstart/csharp.html

简而言之,有一个 Greeter 服务,其 SayHello 端点接受 HelloRequest 并以 HelloResponse.

响应
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

message HelloRequest {
  string name = 1;
}

message HelloReply {
  string message = 1;
}

客户端

同样,与示例几乎相同。注意 stringLength 变量。当它设置为 1-1200(ish) 之间的值时,接收响应的时间始终为 200ms

class Program
{
    static void Main(string[] args)
    {
        var channel = new Channel("192.168.82.254", 50051, ChannelCredentials.Insecure);

        var client = new Greeter.GreeterClient(channel);
        var stringLength = 1500;
        for (var x = 0; x < 50; x++)
        {
            var sw = Stopwatch.StartNew();
            var req = new HelloRequest { Name = new String('x', stringLength) };
            var reply = client.SayHello(req);
            sw.Stop();
            Console.WriteLine($"Greeting: {sw.ElapsedMilliseconds} ms");
        }           
        Console.ReadLine();
    }   
}

服务器

非常简单,处理请求并回复。来自样本的逐字记录。

const int Port = 50051;

static void Main(string[] args)
{
    Server server = new Server
    {
        Services = { Greeter.BindService(new GreeterImpl()) },
        Ports = { new ServerPort("192.168.82.254", Port, ServerCredentials.Insecure) }
    };
    server.Start();

    Console.WriteLine("Greeter server listening on port " + Port);
    Console.WriteLine("Press any key to stop the server...");
    Console.ReadKey();

    server.ShutdownAsync().Wait();
}

class GreeterImpl : Greeter.GreeterBase
{
    // Server side handler of the SayHello RPC
    public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
    {
        return Task.FromResult(new HelloReply { Message = "Hello " + request.Name });
    }
}

1.1.x dashboard seems to indicate that version 1.1.0 still has a problem with ping-pong timing on Windows - https://github.com/grpc/grpc/issues/8806。这似乎在当前 master 和 v1.2.x 中是固定的。请使用 1.2.0-pre1 nugets 来验证(我们现在真的很接近 1.2.0 的发布,所以官方的 1.2.0 包很快就会发布——与此同时,得到你的确认将非常有用) .

master dashboard