超过1000并发的apche ab测试

写个Demo,放在目录PHPThread下

<?php
//create by java-er.com  2018-7-31
for($i=0;$i< 1000;$i++){
         echo $i.'|i am java-er.com';
}

10000请求 并发120

ab -n 10000 -c 120 http://localhost/DEMO/PHPThread/a.php
This is ApacheBench, Version 2.3 <$Revision: 1826891 $>
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 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests
 
 
Server Software:        Apache/2.4.7
Server Hostname:        localhost
Server Port:            80
 
Document Path:          /DEMO/PHPThread/a.php
Document Length:        3890 bytes
 
Concurrency Level:      120
Time taken for tests:   3.718 seconds
Complete requests:      10000
Failed requests:        0
Total transferred:      41230000 bytes
HTML transferred:       38900000 bytes
Requests per second:    2689.80 [#/sec] (mean)
Time per request:       44.613 [ms] (mean)
Time per request:       0.372 [ms] (mean, across all concurrent requests)
Transfer rate:          10830.12 [Kbytes/sec] received
 
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   17   8.4     17      68
Processing:     2   27  12.4     27     174
Waiting:        1   21  11.2     19     169
Total:          9   44  13.5     41     203
 
Percentage of the requests served within a certain time (ms)
  50%     41
  66%     45
  75%     48
  80%     50
  90%     53
  95%     63
  98%     91
  99%    112
 100%    203 (longest request)

10000请求 并发200

 ab -n 10000 -c 200 http://localhost/DEMO/PHPThread/a.php

This is ApacheBench, Version 2.3 <$Revision: 1826891 $>
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)

Test aborted after 10 failures

apr_socket_connect(): Connection reset by peer (54)
Total of 1 requests completed

超过160个并发,系统开始报错,apache默认只支持150个

httpd 目前并发数量

ps -ef | grep httpd | wc -l

httpd内存占用

ps aux|grep -v grep|awk '/httpd/{sum+=$6;n++};END{print sum/n}'
  apachectl -V

Server version: Apache/2.4.33 (Unix)
Server built: Apr 3 2018 18:00:56
Server’s Module Magic Number: 20120211:76
Server loaded: APR 1.5.2, APR-UTIL 1.5.4
Compiled using: APR 1.5.2, APR-UTIL 1.5.4
Architecture: 64-bit
Server MPM: prefork
threaded: no
forked: yes (variable process count)
Server compiled with….
-D APR_HAS_SENDFILE
-D APR_HAS_MMAP
-D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
-D APR_USE_FLOCK_SERIALIZE
-D APR_USE_PTHREAD_SERIALIZE
-D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
-D APR_HAS_OTHER_CHILD
-D AP_HAVE_RELIABLE_PIPED_LOGS
-D DYNAMIC_MODULE_LIMIT=256
-D HTTPD_ROOT=”/usr”
-D SUEXEC_BIN=”/usr/bin/suexec”
-D DEFAULT_PIDLOG=”/private/var/run/httpd.pid”
-D DEFAULT_SCOREBOARD=”logs/apache_runtime_status”
-D DEFAULT_ERRORLOG=”logs/error_log”
-D AP_TYPES_CONFIG_FILE=”/private/etc/apache2/mime.types”
-D SERVER_CONFIG_FILE=”/private/etc/apache2/httpd.conf”

  httpd -V

和上述apachectl -V 输出结果一样

 httpd -l

Compiled in modules:
core.c
mod_so.c
http_core.c
prefork.c

Include conf/extra/httpd-mpm.conf

httpd-mpm.conf

# prefork MPM
# StartServers: number of server processes to start
# MinSpareServers: minimum number of server processes which are kept spare
# MaxSpareServers: maximum number of server processes which are kept spare
# MaxRequestWorkers: maximum number of server processes allowed to start
# MaxConnectionsPerChild: maximum number of connections a server process serves
#                         before terminating
 
<IfModule mpm_prefork_module>  
   StartServers             5                  #开始启动的进程  
   MinSpareServers          5                 #最小准备进程  
   MaxSpareServers         10                #最大空闲进程  
   MaxRequestWorkers      1000            #最大并发数  
   MaxConnectionsPerChild   0  
</IfModule>

vi /Applications/XAMPP/xamppfiles/etc/httpd.conf

# Server-pool management (MPM specific)
#Include etc/extra/httpd-mpm.conf
 
默认的被关闭了。
 
 
LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
 
LoadModule mpm_worker_module modules/mod_mpm_worker.so
 
LoadModule mpm_event_module modules/mod_mpm_eventr.so

去modules目录下查看这三个文件
/Applications/XAMPP/xamppfiles/modules
mod_mpm_prefork.so
mod_mpm_worker.so
mod_mpm_eventr.so

mac系统和linux系统没有

写个phpinfo页面来看一下

http://localhost/DEMO/PHPThread/phpinfo.php

<?php
//create by java-er.com  2018-7-31
phpinfo();
 
?>

查询mpm 为空说明没有加载任何mpm配置

强行打开试试

Include etc/extra/httpd-mpm.conf

重启动apache 不报错,但是看不到mpm参数

强行打开so
LoadModule mpm_prefork_module modules/mod_mpm_prefork.so

停止apache开始报错,看来真是在找modules下的so文件

Stopping Apache Web Server...
Exit code: 8
Stdout:
apache config test fails, aborting
Stderr:
httpd: Syntax error on line 158 of /Applications/XAMPP/xamppfiles/etc/httpd.conf: Cannot load modules/mod_mpm_prefork.so into server: dlopen(/Applications/XAMPP/xamppfiles/modules/mod_mpm_prefork.so, 10): image not found

屏蔽
#LoadModule mpm_prefork_module modules/mod_mpm_prefork.so

httpd -l

Compiled in modules:
core.c
mod_so.c
http_core.c
prefork.c

编译的prefork.c 说明要用的文件是prefork.c 不是mod_mpm_prefork.so

 <IfModule mpm_prefork_module>
    StartServers             5
    MinSpareServers          5
    MaxSpareServers         10
    MaxRequestWorkers      150
    MaxConnectionsPerChild   0
</IfModule>

$ vi extra/httpd-mpm.conf

IfModule 自然就没有什么用处了

<IfModule prefork.c>          
ServerLimit   20000
StartServers   5
MinSpareServers   5
MaxSpareServers   10
MaxClients   1000
MaxRequestsPerChild 0
</IfModule>

显示加载了prefork,但是并发200个依然出错
apr_socket_connect(): Connection reset by peer (54)

https://java-er.com/blog/512m-vps-memory-opt/

之前的一片博客提到了一个要点

但是lampp的不在httpd.conf里,而是在extra/http-default.conf里面。

#
# Timeout: The number of seconds before receives and sends time out.
#
Timeout 300
 
#
# KeepAlive: Whether or not to allow persistent connections (more than
# one request per connection). Set to "Off" to deactivate.
#
KeepAlive On
 
#
# MaxKeepAliveRequests: The maximum number of requests to allow
# during a persistent connection. Set to 0 to allow an unlimited amount.
# We recommend you leave this number high, for maximum performance.
#
MaxKeepAliveRequests 100
 
#
# KeepAliveTimeout: Number of seconds to wait for the next request from the
# same client on the same connection.
#
KeepAliveTimeout 5
 
#
# UseCanonicalName: Determines how Apache constructs self-referencing
# URLs and the SERVER_NAME and SERVER_PORT variables.
# When set "Off", Apache will use the Hostname and Port supplied
# by the client.  When set "On", Apache will use the value of the
# ServerName directive.
#
UseCanonicalName Off

MaxKeepAliveRequests 100

改成

MaxKeepAliveRequests 1000

10000请求 并发200
ab -n 10000 -c 200 http://localhost/DEMO/PHPThread/a.php

依然无效

 httpd -M

Loaded Modules:
core_module (static)
so_module (static)
http_module (static)
mpm_prefork_module (static)
authn_file_module (shared)
authn_core_module (shared)
authz_host_module (shared)
authz_groupfile_module (shared)
authz_user_module (shared)
authz_core_module (shared)
access_compat_module (shared)
auth_basic_module (shared)
reqtimeout_module (shared)
filter_module (shared)
mime_module (shared)
log_config_module (shared)
env_module (shared)
headers_module (shared)
setenvif_module (shared)
version_module (shared)
slotmem_shm_module (shared)
unixd_module (shared)
status_module (shared)
autoindex_module (shared)
negotiation_module (shared)
dir_module (shared)
alias_module (shared)
hfs_apple_module (shared)

mpm_prefork_module (static) 静态加载,列出编译过的模块。同时能看到哪些模块是static静态编译,哪一些是shared动态加载的。

查看应用服务器和数据库均未报错,连接被重置,bingyi了以下,apr_socket_recv这个是操作系统内核的一个参数,在高并发的情况下,内核会认为系统受到了SYN flood攻击,会发送cookies(possible SYN flooding on port 80. Sending cookies),这样会减慢影响请求的速度,所以在应用服务武器上设置下这个参数为0禁用系统保护就可以进行大并发测试了:
# vim /etc/sysctl.conf
加一个

net.ipv4.tcp_syncookies = 0
# sysctl -p
然后就可以超过1000个并发测试了。

另附其他系统内核参数说明:

net.ipv4.tcp_syncookies = 0
#此参数是为了防止洪水攻击的,但对于大并发系统,要禁用此设置

net.ipv4.tcp_max_syn_backlog
#参数决定了SYN_RECV状态队列的数量,一般默认值为512或者1024,即超过这个数量,系统将不再接受新的TCP连接请求,一定程度上可以防止系统资源耗尽。可根据情况增加该值以接受更多的连接请求。

net.ipv4.tcp_tw_recycle
#参数决定是否加速TIME_WAIT的sockets的回收,默认为0。

net.ipv4.tcp_tw_reuse
#参数决定是否可将TIME_WAIT状态的sockets用于新的TCP连接,默认为0。

net.ipv4.tcp_max_tw_buckets
#参数决定TIME_WAIT状态的sockets总数量,可根据连接数和系统资源需要进行设置。

mac系统不知道这个配置,换到服务器上测试

ab -n 10000 -c 200 https://java-er.com/PHPThread/a.php

ps -ef | grep httpd | wc -l
187

187个并发占用

ps -ef | grep httpd | wc -l
201

轻松突破200

ab -n 10000 -c 1000 https://java-er.com/PHPThread/a.php

ps -ef | grep httpd | wc -l
259

 ab -n 10000 -c 1000 https://java-er.com/PHPThread/a.php

This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking java-er.com (be patient)
SSL handshake failed (5).
SSL handshake failed (5).
SSL handshake failed (5).
SSL handshake failed (5).
SSL handshake failed (5).

ab -n 10000 -c 1000 http://leof.yj09.com/PHPThread/a.php

并发请求量稍微高一点(200,300以上)就会报apr_socket_recv: Connection reset by peer 的错.

刚开始以为apache服务器设置有问题. 网上一顿找, 结果你也懂的.

后来发现是ab测试不对

 ab -n 10000 -c 1000 https://www.baidu.com/

即使我去搞baidu也报错。

ab -n 10000 -c 1000 https://www.baidu.com/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking www.baidu.com (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
SSL handshake failed (5).
SSL handshake failed (5).
Completed 7000 requests
SSL handshake failed (5).

因为自己的mac是Mac OS X Yosemite,里面正好有这个的解决方案,试了一下,可以修改open files数目成功!

解决方案如下:

输入命令:launchctl limit

可以看到类似这样:

maxproc表示的是开启的最大进程数,第一个1064表示的是soft限制,第二个1064表示的是hard限制,即硬件最大的限制数,自己设置的不能高于hard限制。所以 我soft也改成了1064最大的。

maxfiles表示的是开启的最大文件数(句柄),意义同上……

如何设置呢?下面讲重点:

打开或者新建一个文件:/Library/LaunchDaemons/limit.maxfiles.plist

输入:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  <plist version="1.0">
    <dict>
      <key>Label</key>
        <string>limit.maxfiles</string>
      <key>ProgramArguments</key>
        <array>
          <string>launchctl</string>
          <string>limit</string>
          <string>maxfiles</string>
          <string>65536</string>
          <string>65536</string>
        </array>
      <key>RunAtLoad</key>
        <true/>
      <key>ServiceIPC</key>
        <false/>
    </dict>
  </plist>

保存并重启mac即可设置最大文件数。

打开或者新建一个文件:/Library/LaunchDaemons/limit.maxproc.plist

输入:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple/DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  <plist version="1.0">
    <dict>
      <key>Label</key>
        <string>limit.maxproc</string>
      <key>ProgramArguments</key>
        <array>
          <string>launchctl</string>
          <string>limit</string>
          <string>maxproc</string>
          <string>1064</string>
          <string>1064</string>
        </array>
      <key>RunAtLoad</key>
        <true />
      <key>ServiceIPC</key>
        <false />
    </dict>
  </plist>

保存并重启mac即可设置最大进程数。

 ab -n 10000 -c 1000 https://www.baidu.com/

可以并发1000了。

参考资料

http://httpd.apache.org/docs/2.4/mod/prefork.html

https://blog.csdn.net/gufengdianying/article/details/5829512

https://blog.csdn.net/forest_boy/article/details/8909551

https://www.cnblogs.com/archoncap/p/5883723.html

https://blog.csdn.net/u011466175/article/details/48002179


This entry was posted in Linux, PHP, 高并发与大数据 and tagged , . Bookmark the permalink.
月小升QQ 2651044202, 技术交流QQ群 178491360
首发地址:月小升博客https://java-er.com/blog/apache-ab-test-1000/
无特殊说明,文章均为月小升原创,欢迎转载,转载请注明本文地址,谢谢
您的评论是我写作的动力

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

*

  

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

About Me

静水流深,水滴石穿