Postman和Jmeter的并发测试Nginx配置

100人浏览   2024-08-14 16:30:02

用 Spring Boot 搭建完 Spring Cloud 微服务项目后,又用 Nginx 为 Spring Gateway 做了负载均衡,其中做了并发限制和每秒连接数限制,Nginx 的配置如下:


worker_processes  1;

events {
    worker_connections  1024;
}

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

    sendfile        on;

    keepalive_timeout  65;

    gzip  on;
    
    # 开启令牌桶 - 每秒新增 100 个令牌
    limit_req_zone $binary_remote_addr zone=req_one:10m rate=100r/s;
    # 开启每个 IP 的并发设置
    limit_conn_zone $binary_remote_addr zone=perip:10m;
    # 开启服务器的总并发设置
    limit_conn_zone $server_name zone=perserver:10m;
    # 定义当服务器由于被限制或缓存时,配置写入日志
    limit_conn_log_level error;

    ## 配置 Spring Cloud Gateway 测试
    
    # 负载均衡
    upstream renda {
        server 127.0.0.1:9000;
        server 127.0.0.1:9001;
    }
    
    server {
        listen               80;
        server_name          www.springcloudgateway.com;
        # 限制每个 IP 的并发数
        limit_conn           perip 10;
        # 限制并发连接数
        limit_conn           perserver 200;
        # 限制总令牌数,令牌发完后,多出来的请求就会返回 503
        limit_req zone=req_one burst=500 nodelay;
        location / {
            proxy_pass http://renda;
            index index.html index.htm;
        }
    }

}

然后,我使用 Postman 的压力测试 Collection Runner 对配置进行并发测试,选择了 Delay 为 0,Iterations 为 20,对
www.springcloudgateway.com
发起 GET 请求。

然而,Postman 竟然全部测试用例通过了,但是我明明在 Nginx 中限制了每个 IP 的并发数 最多为 10 呀。怎么会与预测不符合呢?

接着,我调大了 Postman 的 Iterations 为 1000,这时候我发现,它是串行执行的;Postman 的 Delay 为 0 不能保证所有请求真的可以同时发起。

于是,我换了测试工具,使用 Jmeter 进行压力测试。

打开 Jmeter,先创建一个 Thread Group,然后创建一个 HTTP Request 和 View Results Tree;在 Thread Group 中设置 Number of Threads 为 20,Ramp-up period 设置为 0;同样地,对
www.springcloudgateway.com
发起 GET 请求。

这时候,一共 10 个测试用例通过了,10 个没有通过,而且测试用例发起得时间都一样,精确到毫秒,符合预测结果。

之后再使用 Jmeter 进行每秒连接数测试,发现也符合预测。

由此可见,Jmeter 虽然配置比较多,但是比 Postman 要可靠一些。

相关推荐