nginx || 压力测试(python、ab命令)

1.问题描述

编写一个爬虫程序,模拟1000~2000个人同时访问nginx服务器,用来测试我们设置的并发连接数和服务器的配置是否能够满足同时1000~2000个人同时访问。

2.需求分析

需要用到的模块(requests等)

模块里的函数去实现访问功能

3.编写脚本

[root@nginx ~]# cat test_nginx.py 
#!/usr/bin/python3

import requests
import urllib3
from multiprocessing.dummy import Pool

# 设置session
urllib3.disable_warnings()
requests.adapters.DEFAULT_RETRIES = 10000000000000  # 增加重连次数
session = requests.session()
session.keep_alive = False  # 关闭多余连接,以免链接过多 request报错


def Req(dic):
    try:
        idx = dic["idx"]

        url = 'http://www.feng.com'
        # 发送get请求
        r = session.get(url=url, verify=False)
        # 递归再次访问,如此循环,所有线程都不会闲着,都会一直运行
        print(f'线程{idx}访问成功,继续递归访问')
        Req(dic)
    except Exception as e:
        print('出错了')
        # print(e)
        # traceback.print_exc()
        # 出错了,也继续递归访问
        print(f'线程{idx}访问失败,继续递归访问')
        Req(dic)


def Main():
    threadLists = []  # 线程用的列表
    threadcount = 500  # 线程大小3000个,看自己电脑性能分配

    pool1 = Pool(threadcount)  # 线程池,3000个线程
    # 给列表初始化共有多少个请求 20w个
    for i in range(1, 20000):
        threadLists.append({"idx": i})

    pool1.map(Req, threadLists)
    pool1.close()
    pool1.join()


if __name__ == "__main__":
    Main();

编辑配置文件nginx.conf

[root@nginx conf]# vim nginx.conf

worker_processes  2;

events {
    worker_connections  2048;


[root@nginx conf]# nginx -t
nginx: the configuration file /usr/local/scnginx99/conf/nginx.conf syntax is ok
nginx: [warn] 2048 worker_connections exceed open file resource limit: 1024
nginx: configuration file /usr/local/scnginx99/conf/nginx.conf test is successful

[root@nginx conf]# ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 7183
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 7183
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

# 临时修改文件描述符的限制
[root@nginx conf]# ulimit -n 65535

[root@nginx conf]# ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 7183
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 65535
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 7183
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

# 永久修改文件描述符的限制
[root@nginx ~]# vim /etc/security/limits.conf
#        - nofile - max number of open file descriptors
* soft nofile  1000000
* hard nofile  1000000

# 需要重启系统

# 默认情况下linux内核允许一个进程打开的文件数量是1024(默认的范围0~1023)

[root@nginx conf]# nginx -t
nginx: the configuration file /usr/local/scnginx99/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/scnginx99/conf/nginx.conf test is successful

[root@nginx conf]# nginx -s reload

4.执行脚本

[root@nginx ~]# python3 test_nginx.py 
线程3701访问成功,继续递归访问
线程3741访问成功,继续递归访问
线程3751访问成功,继续递归访问
线程681访问成功,继续递归访问
线程1181访问成功,继续递归访问
线程1211访问成功,继续递归访问
线程3791访问成功,继续递归访问
线程671访问成功,继续递归访问
线程3771访问成功,继续递归访问
线程2101访问成功,继续递归访问
线程701访问成功,继续递归访问
线程3821访问成功,继续递归访问
线程2111访问成功,继续递归访问
线程3831访问成功,继续递归访问
线程81访问成功,继续递归访问
线程651访问成功,继续递归访问
线程721访问成功,继续递归访问
线程721访问成功,继续递归访问
线程1261访问成功,继续递归访问
线程151访问成功,继续递归访问
线程3861访问成功,继续递归访问
线程3891访问成功,继续递归访问
线程2141访问成功,继续递归访问
线程3931访问成功,继续递归访问
线程2121访问成功,继续递归访问
线程3981访问成功,继续递归访问
线程3971访问成功,继续递归访问
线程3971访问成功,继续递归访问
线程4001访问成功,继续递归访问
线程1251访问成功,继续递归访问
线程2171访问成功,继续递归访问
线程2171访问成功,继续递归访问
线程3941访问成功,继续递归访问
线程4031访问成功,继续递归访问
线程4061访问成功,继续递归访问
线程2161访问成功,继续递归访问
线程3921访问成功,继续递归访问
线程1231访问成功,继续递归访问
线程1281访问成功,继续递归访问
线程761访问成功,继续递归访问
线程4101访问成功,继续递归访问
线程4151访问成功,继续递归访问
线程1291访问成功,继续递归访问
线程4131访问成功,继续递归访问
线程2181访问成功,继续递归访问
线程191访问成功,继续递归访问
线程4171访问成功,继续递归访问
线程4161访问成功,继续递归访问
线程61访问成功,继续递归访问
线程4191访问成功,继续递归访问
线程4041访问成功,继续递归访问
线程1321访问成功,继续递归访问
线程4201访问成功,继续递归访问
线程4231访问成功,继续递归访问
线程2211访问成功,继续递归访问
线程2221访问成功,继续递归访问
线程4251访问成功,继续递归访问
线程4281访问成功,继续递归访问
线程1371访问成功,继续递归访问
线程1访问成功,继续递归访问
线程1访问成功,继续递归访问
线程4241访问成功,继续递归访问
线程451访问成功,继续递归访问
线程1341访问成功,继续递归访问
线程251访问成功,继续递归访问
线程1361访问成功,继续递归访问
线程4301访问成功,继续递归访问
线程1381访问成功,继续递归访问
线程4321访问成功,继续递归访问
线程811访问成功,继续递归访问
线程471访问成功,继续递归访问
线程4331访问成功,继续递归访问
线程4361访问成功,继续递归访问
线程1351访问成功,继续递归访问
线程2291访问成功,继续递归访问
线程4391访问成功,继续递归访问
线程4441访问成功,继续递归访问
线程4431访问成功,继续递归访问
线程4421访问成功,继续递归访问
线程2311访问成功,继续递归访问
线程1431访问成功,继续递归访问
线程4481访问成功,继续递归访问
线程851访问成功,继续递归访问
.......

5.使用top命令查看cpu、内存消耗情况

[root@nginx ~]# top
top - 22:42:03 up 24 min,  2 users,  load average: 1.39, 0.55, 0.24
Tasks: 120 total,   3 running, 116 sleeping,   1 stopped,   0 zombie
%Cpu(s): 24.0 us, 31.2 sy,  0.0 ni, 43.7 id,  0.0 wa,  0.0 hi,  1.0 si,  0.0 st
KiB Mem :  1862820 total,   996140 free,   664180 used,   202500 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.  1047892 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                 
  2833 root      20   0 5483440 163696   5040 S 112.0  8.8   0:28.90 python3                 
  1738 root      20   0  161820   6100   4712 S   7.7  0.3   0:06.69 sshd                    
  1791 hanwei    20   0   47168   2740    884 S   3.0  0.1   0:02.35 nginx                   
  1790 hanwei    20   0   47168   2736    880 R   1.7  0.1   0:01.83 nginx                   
     9 root      20   0       0      0      0 R   0.7  0.0   0:01.90 rcu_sched               
   415 root      20   0       0      0      0 S   0.3  0.0   0:00.49 xfsaild/dm-0            
   681 root      20   0   21684   1316   1012 S   0.3  0.1   0:00.30 irqbalance              
  1386 mysql     20   0 1546564 232636  11724 S   0.3 12.5   0:03.69 mysqld                  
  3337 root      20   0  162100   2256   1568 R   0.3  0.1   0:00.02 top                     
     1 root      20   0  125356   3904   2588 S   0.0  0.2   0:01.33 systemd                 
     2 root      20   0       0      0      0 S   0.0  0.0   0:00.01 kthreadd                
     4 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/0:0H            
     5 root      20   0       0      0      0 S   0.0  0.0   0:00.01 kworker/u256:0          
     6 root      20   0       0      0      0 S   0.0  0.0   0:00.10 ksoftirqd/0             
     7 root      rt   0       0      0      0 S   0.0  0.0   0:00.05 migration/0             
     8 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcu_bh                  
    10 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 lru-add-drain           
    11 root      rt   0       0      0      0 S   0.0  0.0   0:00.01 watchdog/0              
    12 root      rt   0       0      0      0 S   0.0  0.0   0:00.01 watchdog/1              
    13 root      rt   0       0      0      0 S   0.0  0.0   0:00.07 migration/1             
    14 root      20   0       0      0      0 S   0.0  0.0   0:00.04 ksoftirqd/1             
    16 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/1:0H            
    18 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kdevtmpfs               
    19 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 netns                   
    20 root      20   0       0      0      0 S   0.0  0.0   0:00.00 khungtaskd              
    21 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 writeback               
    22 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kintegrityd             
    23 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 bioset                  
    24 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 bioset  

使用压力测试软件ab对nginx的web集群进行压力测试

ab(apache bench)是Apache超文本传输协议(HTTP)的性能测试工具。主要是显示你安装的Apache每秒可以处理多少个请求,也可以对其它类型的服务器进行压力测试(例如 nginx、tomcat、IIS等)。

1.下载ab软件

[root@scmaster ~]# yum install ab -y

2.进行压力测试
ab -n 100 -c 10 http://test.com/
其中-n表示请求数,-c表示并发数

[root@scmaster ~]# ab -c 1000  -n 20000  http://192.168.2.188/
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.2.188 (be patient)
Completed 2000 requests
Completed 4000 requests
Completed 6000 requests
Completed 8000 requests
Completed 10000 requests
Completed 12000 requests
Completed 14000 requests
Completed 16000 requests
Completed 18000 requests
Completed 20000 requests
Finished 20000 requests


Server Software:        nginx/1.25.2
Server Hostname:        192.168.2.188
Server Port:            80

Document Path:          /
Document Length:        620 bytes

Concurrency Level:      1000
Time taken for tests:   8.049 seconds
Complete requests:      20000
Failed requests:        2535
   (Connect: 0, Receive: 0, Length: 2535, Exceptions: 0)
Write errors:           0
Non-2xx responses:      35
Total transferred:      17039510 bytes
HTML transferred:       12381995 bytes
Requests per second:    2484.72 [#/sec] (mean)   # 目前测试的最大并发数(吞吐量)
Time per request:       402.460 [ms] (mean)
Time per request:       0.402 [ms] (mean, across all concurrent requests)
Transfer rate:          2067.30 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0  198 482.9     46    7035
Processing:    20  165 156.7    118    1822
Waiting:        3  151 152.9    104    1797
Total:         31  363 502.2    183    7194

Percentage of the requests served within a certain time (ms)
  50%    183
  66%    254
  75%    315
  80%    395
  90%   1124
  95%   1209
  98%   1498
  99%   3120
 100%   7194 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

韩未零

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值