gRPC性能优化

背景介绍

我们在项目中逐渐切换使用了gRPC作为服务间调用的主要手段,逐步替换RESTful API在目前我们项目中的使用。在使用过程中,gRPC的效率是我们想对比较关心并且从目前来看相对难以优化的组件,所以本文就是探讨如何能够在不修改gRPC源码的前提下尽量提升gRPC的性能。

grpc官方提供了性能benchmark可以供大家查看,具体的链接可以在 gRPC Performance Dashboard 中查看。

性能测试

性能测试采用我们的线上标配,目前由于业务量原因,其实性能并不高,这里只是对比参考,建议你在后续处理过程中自行测试。其实最简单的处理方式就是创建多个gRPC client。

单Client处理:

1
2
3
4
5
6
7
8
9
10
11
12
13
➜  client ./client -n 100000 -c 1
2017/08/04 16:49:39 concurrency: 1
requests per client: 100000

2017/08/04 15:59:39 message size: 581 bytes

2017/08/04 16:00:04 took 24467 ms for 100000 requests
2017/08/04 16:00:04 sent requests : 100000
2017/08/04 16:00:04 received requests : 100000
2017/08/04 16:00:04 received requests_OK : 100000
2017/08/04 16:00:04 throughput (TPS) : 4087
2017/08/04 16:00:04 mean: 244283 ns, median: 241106 ns, max: 6107692 ns, min: 138633 ns, p99: 678374 ns
2017/08/04 16:00:04 mean: 0 ms, median: 0 ms, max: 6 ms, min: 0 ms, p99: 0 ms

修改Client数目为10:

1
2
3
4
5
6
7
8
9
10
11
12
13
➜  client ./client -n 100000 -c 10
2017/08/04 16:02:55 concurrency: 10
requests per client: 10000

2017/08/04 16:02:55 message size: 581 bytes

2017/08/04 16:03:01 took 6264 ms for 100000 requests
2017/08/04 16:03:01 sent requests : 100000
2017/08/04 16:03:01 received requests : 100000
2017/08/04 16:03:01 received requests_OK : 100000
2017/08/04 16:03:01 throughput (TPS) : 15964
2017/08/04 16:03:01 mean: 623814 ns, median: 550812 ns, max: 15572759 ns, min: 98979 ns, p99: 5868493 ns
2017/08/04 16:03:01 mean: 0 ms, median: 0 ms, max: 15 ms, min: 0 ms, p99: 5 ms

修改Client数目为20:

1
2
3
4
5
6
7
8
9
10
11
12
13
➜  client ./client -n 100000 -c 20
2017/08/04 16:03:48 concurrency: 20
requests per client: 5000

2017/08/04 16:03:48 message size: 581 bytes

2017/08/04 16:03:54 took 6438 ms for 100000 requests
2017/08/04 16:03:54 sent requests : 100000
2017/08/04 16:03:54 received requests : 100000
2017/08/04 16:03:54 received requests_OK : 100000
2017/08/04 16:03:54 throughput (TPS) : 15532
2017/08/04 16:03:54 mean: 1277405 ns, median: 1132744 ns, max: 16286260 ns, min: 99258 ns, p99: 8126886 ns
2017/08/04 16:03:54 mean: 1 ms, median: 1 ms, max: 16 ms, min: 0 ms, p99: 8 ms

修改Client数目为15:

1
2
3
4
5
6
7
8
9
10
11
12
13
➜  client ./client -n 100000 -c 15
2017/08/04 16:06:57 concurrency: 15
requests per client: 6666

2017/08/04 16:06:57 message size: 581 bytes

2017/08/04 16:07:02 took 5528 ms for 99990 requests
2017/08/04 16:07:02 sent requests : 99990
2017/08/04 16:07:02 received requests : 99990
2017/08/04 16:07:02 received requests_OK : 99990
2017/08/04 16:07:02 throughput (TPS) : 18087
2017/08/04 16:07:02 mean: 823191 ns, median: 741738 ns, max: 17466607 ns, min: 93208 ns, p99: 5787005 ns
2017/08/04 16:07:02 mean: 0 ms, median: 0 ms, max: 17 ms, min: 0 ms, p99: 5 ms