网关性能测试

注意:记得关闭限流功能

测试环境

  • 测试工具:wrk安装教程
  • 服务器:CentOS7(虚拟机,宿主机:macbookpro),内存:2G,CPU:1,核数:2核
  • 运行环境:Java8、Mysql-5.7、Nacos-1.1.3
  • 网关启动参数:
  1. -verbose:gc -XX:+PrintGCDetails -XX:+PrintHeapAtGC -Xloggc:gc-zuul.log \
  2. -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=128m -Xms1024m -Xmx1024m -Xmn512m -Xss256k -XX:SurvivorRatio=8 \
  3. -XX:+UseConcMarkSweepGC
  • zuul配置(仅针对zuul,Spring Cloud Gateway没有做优化配置):
  1. # 不校验时间,这样一个链接可以一直进行测试
  2. sop.api-config.timeout-seconds=0
  3. sop.restful.enable=true
  4. logging.level.com.gitee=info
  5. # zuul调优
  6. zuul.host.max-per-route-connections=5000
  7. zuul.host.max-total-connections=5000
  8. zuul.semaphore.max-semaphores=5000
  9. ribbon.ReadTimeout=5000
  10. hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=13000
  11. logging.file=sop-gateway.log

CentOS允许最大连接数

  1. $ ulimit -n
  2. 65535

调用开放接口

  • wrk命令:
  1. wrk -t8 -c200 -d30s "http://10.1.31.227:8081/?charset=utf-8&biz_content=%7B%22name%22%3A%22%E8%91%AB%E8%8A%A6%E5%A8%83%22%2C%22id%22%3A%221%22%7D&method=alipay.story.get&format=json&sign=RjK%2FThnzAJQOw%2BfoVLS18PgWZAR%2B25SI2XdONFhS%2BmS8vsv2jNT3rygFoh%2ByX1AJbMgIEfcBzkQyqrs29jjd5dcwHVkcf7vxXshyfcEgl0fbMF6Ihycnz7rqSqkW3lzAWx4NuWUfkPnTX8Ffuf%2BhYRaI0NCpNv%2FV300HvsvmUjS6ZzS4YHaT1peSq0agfUhwRPd97aYMnUwRZDzxNfc5wuXA7OQ1o%2FPYIuIb%2FajVfwNP5ysitc%2FKtYEqt9rNAuzkcFmsw71d2RRnrPLsDN%2BuBXnIEh482f%2FbMj2Rj4%2FMq%2B0PEtlTRbg3rYnxyfowymfX%2BNmI4gNRUt70D4a%2FL3Qiug%3D%3D&app_id=2019032617262200001&sign_type=RSA2&version=1.0&timestamp=2020-01-19+13%3A34%3A12"
  • Spring Cloud Gateway测试结果
  1. 8 threads and 200 connections
  2. Thread Stats Avg Stdev Max +/- Stdev
  3. Latency 139.74ms 69.39ms 617.14ms 69.82%
  4. Req/Sec 182.12 55.74 343.00 66.24%
  5. 43391 requests in 30.09s, 11.96MB read
  6. Requests/sec: 1441.96
  7. Transfer/sec: 406.96KB

结果说明,下同:

  1. 8 threads and 200 connections (共8个测试线程,200个连接)
  2. (平均值) (标准差) (最大值)(正负一个标准差所占比例)
  3. Thread Stats Avg Stdev Max +/- Stdev
  4. (延迟)
  5. Latency 139.74ms 69.39ms 617.14ms 69.82%
  6. (每秒请求数)
  7. Req/Sec 182.12 55.74 343.00 66.24%
  8. 43391 requests in 30.09s, 11.96MB read (30.09秒内处理了43391个请求,耗费流量11.96MB)
  9. Requests/sec: 1441.96 (QPS 1441.96,即平均每秒处理请求数为1441.96)
  10. Transfer/sec: 406.96KB (平均每秒流量406.96KB)
  • Spring Cloud Zuul测试结果
  1. 8 threads and 200 connections
  2. Thread Stats Avg Stdev Max +/- Stdev
  3. Latency 230.14ms 331.27ms 2.00s 86.98%
  4. Req/Sec 141.69 51.04 323.00 66.99%
  5. 33945 requests in 30.09s, 9.88MB read
  6. Socket errors: connect 0, read 0, write 0, timeout 385
  7. Requests/sec: 1128.05
  8. Transfer/sec: 336.15KB

调用restful请求

  • wrk命令:
  1. wrk -t8 -c200 -d30s "http://10.1.31.227:8081/rest/story-service/food/getFoodById?id=2"

线程数为 8,模拟 200 个并发请求,持续 30 秒

  • Spring Cloud Gateway测试结果
  1. 8 threads and 200 connections
  2. Thread Stats Avg Stdev Max +/- Stdev
  3. Latency 120.14ms 58.30ms 513.85ms 67.47%
  4. Req/Sec 210.47 54.26 770.00 69.37%
  5. 50301 requests in 30.10s, 7.53MB read
  6. Requests/sec: 1670.97
  7. Transfer/sec: 256.21KB
  • Spring Cloud Zuul测试结果
  1. 8 threads and 200 connections
  2. Thread Stats Avg Stdev Max +/- Stdev
  3. Latency 185.86ms 285.65ms 1.99s 88.55%
  4. Req/Sec 167.75 55.60 460.00 68.05%
  5. 40070 requests in 30.09s, 6.65MB read
  6. Socket errors: connect 0, read 0, write 0, timeout 466
  7. Requests/sec: 1331.81
  8. Transfer/sec: 226.50KB

综上所述,Spring Cloud Gateway在没有优化的情况下,压测表现比zuul好,但zuul的数据表现也不差,但是出现超时现象,总的来说还是Spring Cloud Gateway具有优势。

附启动脚本:

restart.sh

  1. echo "Stopping sop-gateway-1.0.0-SNAPSHOT.jar"
  2. pid=`ps -ef | grep sop-gateway-1.0.0-SNAPSHOT.jar | grep -v grep | awk '{print $2}'`
  3. if [ -n "$pid" ]
  4. then
  5. echo "kill -9 的id:" $pid
  6. kill -9 $pid
  7. fi
  8. nohup java -jar -verbose:gc -XX:+PrintGCDetails -XX:+PrintHeapAtGC -Xloggc:gc.log \
  9. -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=128m -Xms1024m -Xmx1024m -Xmn256m -Xss256k -XX:SurvivorRatio=8\
  10. -XX:+UseConcMarkSweepGC sop-gateway-1.0.0-SNAPSHOT.jar\
  11. --spring.profiles.active=dev --server.port=8081 &
  12. tail -f nohup.out