本文咱们主要是想测试和研究几点:nginx
本文测试使用的代码在: github.com/JosephZhu19…git
在代码里咱们实现了两套代理程序: github
测试使用的机器配置是(阿里云ECS): spring
nginx 配置的就是默认的测试页(删了点内容,减小内网带宽): 后端
四层的代理,咱们仅仅是使用Netty来转发ByteBuf。 七层的代理,会有更多额外的开销,主要是Http请求的编码解码以及Http请求的聚合,服务端:性能优化
客户端: 服务器
这里咱们能够想到,四层代理由于少了Http数据的编解码过程,性能确定比七层好不少,好多少咱们能够看看测试结果。网络
咱们知道做为一个代理,咱们须要开启服务端从上游来获取请求,而后再做为客户端把请求转发到下游,从下游获取到响应后,返回给上游。咱们的服务端和客户端都须要Worker线程来处理IO请求,有三种作法;app
以七层代理的代码为例: oop
接下去的测试咱们会来测试这三种线程模型,这里想固然的猜想是方案A的性能是最好的,由于独立了线程池不相互影响,咱们接下去看看结果
Layer4ProxyServer Started with config: ServerConfig(type=Layer4ProxyServer, serverIp=172.26.5.213, serverPort=8888, backendIp=172.26.5.214, backendPort=80, backendThreadModel=ReuseServerThread, receiveBuffer=10240, sendBuffer=10240, allocatorType=Unpooled, maxContentLength=2000)
Layer4ProxyServer Started with config: ServerConfig(type=Layer4ProxyServer, serverIp=172.26.5.213, serverPort=8888, backendIp=172.26.5.214, backendPort=80, backendThreadModel=IndividualGroup, receiveBuffer=10240, sendBuffer=10240, allocatorType=Unpooled, maxContentLength=2000)
Layer4ProxyServer Started with config: ServerConfig(type=Layer4ProxyServer, serverIp=172.26.5.213, serverPort=8888, backendIp=172.26.5.214, backendPort=80, backendThreadModel=ReuseServerGroup, receiveBuffer=10240, sendBuffer=10240, allocatorType=Unpooled, maxContentLength=2000)
看到这里其实已经有结果了,ReuseServerThread性能是最好的,其次是ReuseServerGroup,最差是IndividualGroup,和咱们猜的不一致。
从网络带宽上能够看到,先测试的ReuseServerThread跑到了最大的带宽(后面三个高峰分别表明了三次测试):
Layer7ProxyServer Started with config: ServerConfig(type=Layer7ProxyServer, serverIp=172.26.5.213, serverPort=8888, backendIp=172.26.5.214, backendPort=80, backendThreadModel=ReuseServerThread, receiveBuffer=10240, sendBuffer=10240, allocatorType=Unpooled, maxContentLength=2000)
Layer7ProxyServer Started with config: ServerConfig(type=Layer7ProxyServer, serverIp=172.26.5.213, serverPort=8888, backendIp=172.26.5.214, backendPort=80, backendThreadModel=IndividualGroup, receiveBuffer=10240, sendBuffer=10240, allocatorType=Unpooled, maxContentLength=2000)
Layer7ProxyServer Started with config: ServerConfig(type=Layer7ProxyServer, serverIp=172.26.5.213, serverPort=8888, backendIp=172.26.5.214, backendPort=80, backendThreadModel=ReuseServerGroup, receiveBuffer=10240, sendBuffer=10240, allocatorType=Unpooled, maxContentLength=2000)
结论同样,ReuseServerThread性能是最好的,其次是ReuseServerGroup,最差是IndividualGroup。我以为是这么一个道理:
下面分别是网络带宽和CPU监控图:
Layer7ProxyServer Started with config: ServerConfig(type=Layer7ProxyServer, serverIp=172.26.5.213, serverPort=8888, backendIp=172.26.5.214, backendPort=80, backendThreadModel=ReuseServerThread, receiveBuffer=10240, sendBuffer=10240, allocatorType=Pooled, maxContentLength=100000000)
Layer7ProxyServer Started with config: ServerConfig(type=Layer7ProxyServer, serverIp=172.26.5.213, serverPort=8888, backendIp=172.26.5.214, backendPort=80, backendThreadModel=ReuseServerThread, receiveBuffer=10240, sendBuffer=10240, allocatorType=Pooled, maxContentLength=2000)
能够看到Netty 4.1中已经把默认的分配器设置为了PooledByteBufAllocator
这里总结了一个表格,性能损失比例都以第一行直接压Nginx为参照:
结论是:
之因此写这个文章作这个分析的缘由是由于最近在作咱们自研网关的性能优化和压力测试 github.com/spring-aven… 。 我发现有一些其它开源的基于Netty的代理项目并非复用链接的,可能做者没有意识到这个问题,我看了下Zuul的代码,它也是复用的。