Nginx功能篇09—基准测试

概述

本章,你将学习如何对 Nginx 进行基准测试。

相关的基准测试工具有很多,常见的有:

  • ab - Apache httpd 当中的一个简单基准测试命令,适合快速测试
  • wrk - 一个现代化的 HTTP 基准测试工具,支持多线程和Lua脚本

前置条件

对 Nginx 进行了基础的性能优化,参阅 前面的文档

目前主配置文件 nginx.conf 的内容如下:

Shell > cat /usr/local/nginx/conf/nginx.conf
user  nginx;
worker_processes  4;

error_log  logs/error.log  error;

pid        logs/nginx.pid;

worker_rlimit_nofile 7400;

events {
    use epoll;
    worker_connections  1024;
    multi_accept on;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    sendfile       on;
    tcp_nopush     on;

    keepalive_timeout  30;
    server_tokens off;

    gzip on;
    gzip_vary on;
    gzip_buffers 32 4K;
    gzip_comp_level 5;
    gzip_types text/plain text/css text/xml application/json application/javascript application/xml application/xhtml+xml image/svg+xml application/x-font-ttf application/x-font-opentype application/vnd.ms-fontobject font/woff;
    gzip_min_length 256;

    server {
        listen  192.168.100.20:80 backlog=8000 reuseport;
        server_name  www.games.com;
        access_log  logs/games-access.log  main;
        charset utf-8;

        location / {
            root   /html/games/;
            index  index.html index.htm;
            location ~* \.(jpg|css|jpeg|xml|js|gif) {
                expires 4h;
            }
        }

        location /images/ {
            alias /var/www/attachment/;
            autoindex on;
            autoindex_exact_size off;
            autoindex_localtime off;
            autoindex_format html;
        }
    }
}

内核参数如下:

Shell > sysctl -p
net.core.somaxconn = 8000
net.ipv4.tcp_max_syn_backlog = 8000
net.ipv4.tcp_abort_on_overflow = 1

简单来说,目前 Nginx 的性能为:

  • 采用 epoll 连接处理方法
  • 单个 worker 进程可对待处理的连接进行批处理
  • 4 个 worker 进程,单个 worker 进程可处理 7400 的连接数,最大能处理 29600 的连接数
  • Nginx 程序的半连接队列与全连接队列均为 8000 ,正如 前面的文档 所述的那样,Nginx 程序最终的全连接队列大小由两者(Linux 内核参数值和传入 listen 函数的 backlog 值)中的最小值决定
  • 当 Linux 内核的全连接队列出现问题时,Linux 内核会断开连接并给客户端发送 RST 包,客户端会收到诸如 "104 Connection reset by peer" 的错误

启动 Nginx:

Shell > /usr/local/nginx/sbin/nginx -t && /usr/local/nginx/sbin/nginx

ab 命令

其语法为 ab [options] [http[s]://]hostname[:port]/path,常见选项有:

  • -c concurrency - 用户并发数,并发数不能大于请求数
  • -n requests - 发出的请求数
  • -k - 使用 HTTP KeepAlive 功能

测试的输出信息如下:

Shell > /usr/local/apache2/bin/ab -c 1000 -n 10000 -k http://192.168.100.20/
...
Server Software:        nginx
Server Hostname:        192.168.100.20
Server Port:            80

Document Path:          /
Document Length:        23 bytes

Concurrency Level:      1000
Time taken for tests:   0.313 seconds
Complete requests:      10000
Failed requests:        0
Keep-Alive requests:    10000
Total transferred:      2670000 bytes
HTML transferred:       230000 bytes
Requests per second:    31962.36 [#/sec] (mean)
Time per request:       31.287 [ms] (mean)
Time per request:       0.031 [ms] (mean, across all concurrent requests)
Transfer rate:          8333.94 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    5  15.5      0      84
Processing:     3   21  12.7     22      86
Waiting:        3   21  12.7     22      73
Total:          3   26  18.8     23     107

Percentage of the requests served within a certain time (ms)
  50%     23
  66%     27
  75%     32
  80%     36
  90%     43
  95%     73
  98%     85
  99%     92
 100%    107 (longest request)

其中关键的指标有:

  • Failed requests - 请求的失败数,是一个稳定性指标,理想状态下通常为 0 。若大于 0 ,说明服务器在处理高负载时出现了错误、超时或连接重置

  • RPS(Requests Per Second) - 每秒处理的请求数,即吞吐量。数值越大,说明服务器处理能力越强。这是衡量服务器性能最直观的指标之一。

  • Time per request - 每个请求的平均耗时,这里有两行

    • 第一行 - 用户视角的平均等待时间‌,即从发起请求到收到响应所需的平均时间。数值越小,用户等待的时间越短,体验越好
    • 第二行 - 服务器视角的平均处理时间‌,即服务器处理单个请求所花费的时间。
  • Transfer rate - 网络传输速率,单位为 KB/s(千字节每秒)。若该值接近服务器网卡硬件的速率上限或运营商带宽上限,说明性能瓶颈出在网络问题上而非 Nginx 程序上

  • 百分位响应时间分布 - 如 50% 的请求数都在 23ms 内完成。通常情况下都重点关注 P95 或 P99 的情况,因为它们能反映极端情况下的用户体验

Avatar photo

关于 陸風睿

GNU/Linux 从业者、开源爱好者、技术钻研者,撰写文档既是兴趣也是工作内容之一。Q - "281957576";WeChat - "jiulongxiaotianci",Github - https://github.com/jimcat8
用一杯咖啡支持我们,我们的每一篇[文档]都经过实际操作和精心打磨,而不是简单地从网上复制粘贴。期间投入了大量心血,只为能够真正帮助到您。
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇