c++ http服务器之Apache工具ab压力测试(nginx与brpc)

这篇具有很好参考价值的文章主要介绍了c++ http服务器之Apache工具ab压力测试(nginx与brpc)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

系列服务器开发



前言


一、ab是什么?

ab全称为:apache bench,官方注释为:
Apache超文本传输协议(HTTP)的性能测试工具。其设计意图是描绘当前所安装的Apache的执行性能,主要是显示你安装的Apache每秒可以处理多少个请求。apache自带的压力测试工具。ab非常实用,它不仅可以对apache服务器进行网站访问压力测试,也可以对或其它类型的服务器进行压力测试。比如nginx、tomcat、IIS等。

其他几款轻量级的压测工具如siege http_load ab webbench.siege等都太吃内存(在相同的请求数与并发数下,ab相对而言耗资源较少)。

centos安装ab

安装:
centos安装:
yum install httpd-tools
linux安装:
apt-get install apache2-utils

测试方法
./ab [options] [url]

ab常用参数的介绍:
-n:总共的请求执行数,缺省是1-c:并发数,缺省是1-t:测试所进行的总时间,秒为单位,缺省50000s
-p:POST时的数据文件(本地存储json文件等)
-w: 以HTML表的格式输出结果
-T:payload数据格式'application/json':
ab -n 200 -c 10 "http://jd.com/":
ab -n 200 -c 100 http://localhost/index.html

c++ http服务器之Apache工具ab压力测试(nginx与brpc)

ab测试返回结果字段解析

Server Software:        web服务器软件及版本
Server Hostname:        请求的地址
Server Port:            请求的端口

Document Path:          请求的页面路径
Document Length:        页面大小

Concurrency Level:      并发数
Time taken for tests:   测试总共花费的时间
Complete requests:      完成的请求数
Failed requests:        失败的请求数
Write errors:           写入错误
Total transferred:      总共传输字节数,包含http的头信息等
HTML transferred:       html字节数,实际的页面传递字节数
Requests per second:    每秒处理的请求数,服务器的吞吐量(重要)
Time per request:       平均数,用户平均请求等待时间
Time per request:       服务器平均处理时间
Transfer rate:          平均传输速率(每秒收到的速率)

二、ab测试实例nginx

1.nginx环境准备与安装

1、安装nginx
2、登陆http://192.168.31.91或者http://localhost/index.html, 页面如下表示安装成功
c++ http服务器之Apache工具ab压力测试(nginx与brpc)
3、nginx conf内容如下
默认:
1 worker_processes
1024 worker_connections

[root@localhost conf]# cat nginx.conf

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

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"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

2.ab测试nginx本身的性能

服务器上本机测试:连接数200000 ,请求数1000
ab -n 200000 -c 1000 http://localhost/index.html
测试结果如下(后面不在贴所有信息):

[root@localhost conf]# ab -n 200000 -c 1000 http://localhost/index.html
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 localhost (be patient)
Completed 20000 requests
Completed 40000 requests
Completed 60000 requests
Completed 80000 requests
Completed 100000 requests
Completed 120000 requests
Completed 140000 requests
Completed 160000 requests
Completed 180000 requests
Completed 200000 requests
Finished 200000 requests


Server Software:        nginx/1.20.0
Server Hostname:        localhost
Server Port:            80

Document Path:          /index.html
Document Length:        612 bytes

Concurrency Level:      1000
Time taken for tests:   8.144 seconds
Complete requests:      200000
Failed requests:        128
   (Connect: 0, Receive: 0, Length: 64, Exceptions: 64)
Write errors:           0
Total transferred:      168945920 bytes
HTML transferred:       122360832 bytes
Requests per second:    24557.34 [#/sec] (mean)
Time per request:       40.721 [ms] (mean)
Time per request:       0.041 [ms] (mean, across all concurrent requests)
Transfer rate:          20258.11 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   30 193.1      5    3013
Processing:     2    7   9.5      7     412
Waiting:        0    6   9.4      5     411
Total:          4   37 195.5     12    3415

Percentage of the requests served within a certain time (ms)
  50%     12
  66%     13
  75%     13
  80%     14
  90%     15
  95%     17
  98%   1013
  99%   1017
 100%   3415 (longest request)

nginx,默认worker_connections =1024,因此并发只能支持到1000左右
并发1000(Concurrency Level: 1000) 发送并完成200000请求(Complete requests: 200000) 失败的次数很少(Failed requests: 128),发送200000个请求总耗时20s(Time taken for tests: 8.144 seconds),那么qps大概为 24557

Concurrency Level:      1000
Time taken for tests:   8.144 seconds
Complete requests:      200000
Failed requests:        128
   (Connect: 0, Receive: 0, Length: 64, Exceptions: 64)
Write errors:           0
Requests per second:    24557.34 [#/sec] (mean)
Time per request:       40.721 [ms] (mean)
Time per request:       0.041 [ms] (mean, across all concurrent requests)

服务器上本机测试:连接数200000 ,请求数5000
ab -n 200000 -c 5000 http://localhost/index.html
测试结果如下:
nginx,默认worker_connections =1024,因此并发只能支持到1000左右
并发5000(Concurrency Level: 5000) 发送并完成200000请求(Complete requests: 200000) 失败的次数很少(Failed requests: 8128),发送200000个请求总耗时10s(Time taken for tests: 10.126 seconds),那么qps大概为 19751。
发现并发增大失败的请求增多(128->8128),且qps有所降低。

Concurrency Level:      5000
Time taken for tests:   10.126 seconds
Complete requests:      200000
Failed requests:        8128
   (Connect: 0, Receive: 0, Length: 4064, Exceptions: 4064)
Write errors:           0
Requests per second:    19751.38 [#/sec] (mean)
Time per request:       253.147 [ms] (mean)
Time per request:       0.051 [ms] (mean, across all concurrent requests)

通过ab对nginx的默认页面测试,tps可以支持到20000左右。并发目前由于并发设置的为1024.此时cpu已经满负荷,理论上nginx并发可以支持到10w个,设置更高.
关于Nginx的一些优化(突破十万并发)

3.ab测试基于brpc的http服务器性能

::vss::ResultCode StreamAdd(const ::vss::StreamAddReq* request,::vss::StreamAddRsp* response)
{
    ::vss::ResultCode res = ::vss::ResultCode::success;
    //bthread_usleep(1000*1000);
    //sleep(1);
    ++count;
    if(time(NULL) - timestart > 1){
        LOG(INFO)<< "every sec average: "<< count/1;
        count = 0;
        timestart = time(NULL);
    }    
    return res;
}

ab -n 1000000 -kc 1000 http://192.168.31.91:8001/app/vss/streamAdd
发现qps为10w左右,

Concurrency Level:      1000
Time taken for tests:   10.981 seconds
Complete requests:      1000000
Failed requests:        0
Write errors:           0
Keep-Alive requests:    1000000
Total transferred:      87000000 bytes
HTML transferred:       24000000 bytes
Requests per second:    91068.25 [#/sec] (mean)
Time per request:       10.981 [ms] (mean)
Time per request:       0.011 [ms] (mean, across all concurrent requests)
Transfer rate:          7737.24 [Kbytes/sec] received

在接口中加入1s的延时,且必须使用bthread_usleep,不会阻塞bthread。tps为800+

::vss::ResultCode StreamAdd(const ::vss::StreamAddReq* request,::vss::StreamAddRsp* response)
{
    ::vss::ResultCode res = ::vss::ResultCode::success;
    bthread_usleep(1000*1000);
    //sleep(1);
    return res;
}

ab -n 10000 -kc 1000 http://192.168.31.91:8001/app/vss/streamAdd

Concurrency Level:      1000
Time taken for tests:   12.160 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Keep-Alive requests:    10000
Requests per second:    822.36 [#/sec] (mean)
Time per request:       1216.009 [ms] (mean)
Time per request:       1.216 [ms] (mean, across all concurrent requests)

在接口中加入2s的延时,且必须使用bthread_usleep,不会阻塞bthread。tps为400+

::vss::ResultCode StreamAdd(const ::vss::StreamAddReq* request,::vss::StreamAddRsp* response)
{
    ::vss::ResultCode res = ::vss::ResultCode::success;
    bthread_usleep(2*1000*1000);
    //sleep(1);
    return res;
}

ab -n 10000 -kc 1000 http://192.168.31.91:8001/app/vss/streamAdd

Concurrency Level:      1000
Time taken for tests:   24.106 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Keep-Alive requests:    10000
Requests per second:    414.83 [#/sec] (mean)
Time per request:       2410.631 [ms] (mean)
Time per request:       2.411 [ms] (mean, across all concurrent requests)
Transfer rate:          35.24 [Kbytes/sec] received

在接口中加入1s的延时,且必须使用sleep,会阻塞bthread。tps为8+

::vss::ResultCode StreamAdd(const ::vss::StreamAddReq* request,::vss::StreamAddRsp* response)
{
    ::vss::ResultCode res = ::vss::ResultCode::success;
    sleep(1);
    return res;
}
ab -n 100 -kc 10 http://192.168.31.91:8001/app/vss/streamAdd
```cpp
Concurrency Level:      10
Time taken for tests:   12.032 seconds
Complete requests:      100
Failed requests:        0
Write errors:           0
Keep-Alive requests:    100
Requests per second:    8.31 [#/sec] (mean)
Time per request:       1203.191 [ms] (mean)
Time per request:       120.319 [ms] (mean, across all concurrent requests)
Transfer rate:          0.71 [Kbytes/sec] received

三、ab实战之常见问题解决

测试中出现too many open files,提供一种临时的解决版本,如需设置生效的话,请自行查询资料。
ulimit -n 可以发现系统默认是1024,可以将文件句柄增大。
ulimit -n 65535 可以将系统最大打开文件数临时修改为65535,退出登录失效。
c++ http服务器之Apache工具ab压力测试(nginx与brpc)
测试中出现apr_socket_recv: Connection reset by peer 提供一种临时方案
解释:查看应用服务器和数据库均未报错,连接被重置,apr_socket_recv是OS内核的一个参数,高并发情况下,内核会认为系统受到了SYN flood***,会发送cookies(possible SYN flooding on port 80. Sending cookies),这样会减慢影响请求的速度,所以在应用服务器上设置下该参数为0 禁用系统保护就可进行大并发测试了。

进入/etc/sysctl.conf
net.ipv4.tcp_syncookies = 0 ##禁用系统保护
sysctl -p ##查看是否成功
c++ http服务器之Apache工具ab压力测试(nginx与brpc)


总结

通过本文对ab的实战练习,应该对ab有了基本的了解,希望能够对你的工作有所帮助。文章来源地址https://www.toymoban.com/news/detail-466468.html

到了这里,关于c++ http服务器之Apache工具ab压力测试(nginx与brpc)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Nginx代理服务器、HTTP调度、TCP/UDP调度、Nginx优化、HTTP错误代码、状态页面、压力测试

    Top 案例1:Nginx反向代理 案例2:Nginx的TCP/UDP调度器 案例3:Nginx常见问题处理 1.1 问题 使用Nginx实现Web反向代理功能,实现如下功能: 后端Web服务器两台,可以使用httpd实现 Nginx采用轮询的方式调用后端Web服务器 两台Web服务器的权重要求设置为不同的值 最大失败次数为2,失败

    2024年01月21日
    浏览(33)
  • [AIGC] Apache HTTP服务器:历史与使用

    Apache HTTP服务器,通常我们简称为Apache,是一款流行且强大的开源Web服务器软件。它伴随互联网的快速成长,并承载了许多网站服务的运行和访问。通过最新的科技和广阔的功能,Apache服务器已经成为了全球最流行的Web服务器软件之一。在本文中,我们将介绍Apache HTTP服务器的

    2024年04月10日
    浏览(31)
  • 在Linux中使用Apache HTTP服务器

    Apache HTTP服务器,也被称为Apache,是全球使用最广泛的Web服务器软件之一。它以其稳定性、强大的功能和灵活性而闻名,尤其在Linux操作系统上表现得尤为出色。以下是关于如何在Linux中使用Apache HTTP服务器的详细指南。 1. 安装Apache 首先,你需要安装Apache。在大多数Linux发行版

    2024年02月02日
    浏览(44)
  • 在Linux下配置Apache HTTP服务器

    在Linux的世界里,如果说有什么比解决各种“神秘”的故障更让人头疼,那一定就是配置Apache HTTP服务器了。这不是因为Apache有什么问题,而是因为配置它简直就像解谜游戏,一不留神就会让你陷入无尽的纠结。 首先,你需要知道的是,Apache并不是一个“喂我配置文件,我就

    2024年02月02日
    浏览(43)
  • Windows系统中Apache Http服务器简单使用

    1 简介         Apache HTTP服务器是一个开源的、跨平台的Web服务器软件。它由Apache软件基金会开发和维护。Apache HTTP服务器可以在多种操作系统上运行,如Windows、Linux、Unix等,并且支持多种编程语言和技术,如PHP、Perl、Python、Java等。     Apache HTTP服务器提供了高度可定制

    2024年02月10日
    浏览(32)
  • 如何使用CentOS系统中的Apache服务器提供静态HTTP服务

    在CentOS系统中,Apache服务器是一个常用的Web服务器软件,它可以高效地提供静态HTTP服务。以下是在CentOS中使用Apache提供静态HTTP服务的步骤: 1. 安装Apache服务器 首先,您需要确保已安装Apache服务器。可以使用以下命令安装Apache: bash 复制代码 sudo yum install httpd 2. 配置Apache服务

    2024年02月02日
    浏览(28)
  • 服务器稳定性测试-LTP压力测试方法及工具下载

    LTP(LinuxTest Project)是SGI、IBM、OSDL和Bull合作的项目,目的是为开源社区提供一个测试套件,用来验证Linux系统可靠性、健壮性和稳定性。LTP测试套件是测试Linux内核和内核相关特性的工具的集合。 该工具的目的是通过把测试自动化引入到Linux内核测试,提高Linux的内核质量。

    2024年02月02日
    浏览(43)
  • 【httpd】 Apache http服务器目录显示不全解决

    可通过find查找文件所在位置 其中 FancyIndexing 支持美化显示; HTMLTable 允许底层代码把文件列表生成在一个table元素里面; VersionSort 安装版本排序; NameWidth=* 页面自动匹配文件名宽度; FoldersFirst 安装文件夹优先排列; Charset=UTF-8 支持中文显示; SuppressDescription 不显示文件描述

    2024年02月08日
    浏览(31)
  • Apache HTTP Server(简称Apache)是Apache软件基金会的一个开放源代码的网页服务器

    Apache HTTP Server(简称Apache)是Apache软件基金会的一个开放源代码的网页服务器,可以在大多数电脑操作系统中运行,由于其具有的跨平台性和安全性,被广泛使用,是最流行的Web服务器端软件之一。它快速、可靠并且可通过简单的API扩展,Perl/Python解释器可被编译到服务器中

    2024年02月19日
    浏览(32)
  • Apache安装教程及使用ab命令进行压力测试

    1.官网下载https://www.apachehaus.com/cgi-bin/download.plx 2.选择要下载的版本(比如64位:Apache 2.4.46 x64) 3.解压到电脑任意盘根目录下(非根目录需注意不能有中文文件名) 4.已解压目录中在 conf文件夹找到httpd.conf文件右键使用记事本打开进行编辑 5.Ctrl+F查找Define SRVROOT修改它的值为你

    2024年02月04日
    浏览(26)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包