ubuntu 上的 wrk 访问接口延迟很高怎么办?

我有一个 api 服务,http://192.168.38.223:9100/

使用 docker 部署在 192.168.38.223 机器上

╰─➤  docker ps -a | grep proxy         
12b8b5801e73   ponponon/proxy-king:2023.09.21.1           "python api.py"          38 minutes ago   Up 38 minutes         0.0.0.0:9100->9100/tcp, :::9100->9100/tcp                                                                                                                                                      proxy-king

然后我使用 mac 上的 wrk 对 http://192.168.38.223:9100/ 接口做压力测试

这个 wrk 是通过 brew install wrk 安装的

╰─➤  wrk --version                                  
wrk 4.2.0 [kqueue] Copyright (C) 2012 Will Glozer
Usage: wrk <options> <url>                            
  Options:                                            
    -c, --connections <N>  Connections to keep open   
    -d, --duration    <T>  Duration of test           
    -t, --threads     <N>  Number of threads to use   
                                                      
    -s, --script      <S>  Load Lua script file       
    -H, --header      <H>  Add header to request      
        --latency          Print latency statistics   
        --timeout     <T>  Socket/request timeout     
    -v, --version          Print version details      
                                                      
  Numeric arguments may include a SI unit (1k, 1M, 1G)
  Time arguments may include a time unit (2s, 2m, 2h)
╰─➤  wrk http://192.168.38.223:9100/ -t32 -c64 -d 30                                                                                                                     130 ↵
Running 30s test @ http://192.168.38.223:9100/
  32 threads and 64 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     3.51ms    2.99ms  64.81ms   91.05%
    Req/Sec   644.17    204.94     1.16k    66.50%
  615969 requests in 30.04s, 92.72MB read
Requests/sec:  20503.44
Transfer/sec:      3.09MB

结果非常不错,RPS 可以到 2w+

此时 223 机器 CPU 使用率很高,CPU 都在权利输出

192.168.38.223 机器是 32core 的志强、128GB 的 RAM、千兆网卡

图片.png

图片.png

然后我又使用 192.168.38.223 上面的 wrk 工具对 http://192.168.38.223:9100/ 接口做压力测试

192.168.38.223 机器使用的是 ubuntu22.04

╰─➤  uname -a
Linux T4GPU 5.15.0-84-generic #93-Ubuntu SMP Tue Sep 5 17:16:10 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

安装的 wrk 是 apt 安装的 apt install wrk

╰─➤  wrk --version                                                                                                                                                         1 ↵
wrk debian/4.1.0-3build1 [epoll] Copyright (C) 2012 Will Glozer
Usage: wrk <options> <url>                            
  Options:                                            
    -c, --connections <N>  Connections to keep open   
    -d, --duration    <T>  Duration of test           
    -t, --threads     <N>  Number of threads to use   
                                                      
    -s, --script      <S>  Load Lua script file       
    -H, --header      <H>  Add header to request      
        --latency          Print latency statistics   
        --timeout     <T>  Socket/request timeout     
    -v, --version          Print version details      
                                                      
  Numeric arguments may include a SI unit (1k, 1M, 1G)
  Time arguments may include a time unit (2s, 2m, 2h)

但是出现了意外的情况,RPS 变得很低, 只有 1.4k 了,而且延迟也变得很大,居然有 45ms !这不寻常!

╰─➤  ./wrk http://192.168.38.223:9100/ -t32 -c64 -d 30
Running 30s test @ http://192.168.38.223:9100/
  32 threads and 64 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    44.67ms    2.24ms  52.34ms   81.85%
    Req/Sec    44.78      6.01    60.00     94.41%
  43079 requests in 30.09s, 6.49MB read
Requests/sec:   1431.51
Transfer/sec:    220.89KB

我在 192.168.38.223 机器上跑了一个 python 脚本

import time
import requests


s=time.time()
response=requests.get('http://192.168.38.223:9100/')
e=time.time()

print(f'{round((e-s)*1000,2)} ms')

输出结果

4.34 ms

可以看到,使用 python 脚本访问,并没有延迟问题,但是为什么 wrk 的访问延迟这么高?有什么排查方式?


我又找了一个另一台机器(192.168.38.191)加入压力测试

192.168.38.191 安装的是 ubuntu20.04
╰─➤  ./wrk http://192.168.38.223:9100/ -t32 -c64 -d 30                                                                                                                                                                                                                                              130 ↵
Running 30s test @ http://192.168.38.223:9100/
  32 threads and 64 connections


  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    16.62ms   16.44ms  55.50ms   78.89%
    Req/Sec   164.54     25.46   320.00     69.06%
  157550 requests in 30.04s, 23.72MB read
Requests/sec:   5245.07
Transfer/sec:    808.67KB

我又找了一个另一台机器(192.168.38.233)加入压力测试

192.168.38.233 安装的是 ubuntu22.04
注意:是 233 不是 223,223 是被测试的机器,而 233 是我新找的机器
╰─➤  wrk http://192.168.38.223:9100/ -t32 -c64 -d 30
Running 30s test @ http://192.168.38.223:9100/
  32 threads and 64 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    44.11ms    1.71ms  52.21ms   95.40%
    Req/Sec    45.31      6.09    60.00     93.87%
  43503 requests in 30.10s, 6.56MB read
Requests/sec:   1445.32
Transfer/sec:    223.03KB

此时 223 机器 CPU 使用率很低,CPU 都在打酱油

192.168.38.223 机器是 32core 的志强、128GB 的 RAM、千兆网卡

图片.png


不知道为什么会有这么大的区别

尤其是 223 自己压测 223 居然不如 mac 压测 223

阅读 374
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题