![为什么 ab 在 OSX 上出现错误:apr_socket_recv: 对等方重置连接(54)?](https://linux22.com/image/691005/%E4%B8%BA%E4%BB%80%E4%B9%88%20ab%20%E5%9C%A8%20OSX%20%E4%B8%8A%E5%87%BA%E7%8E%B0%E9%94%99%E8%AF%AF%EF%BC%9Aapr_socket_recv%3A%20%E5%AF%B9%E7%AD%89%E6%96%B9%E9%87%8D%E7%BD%AE%E8%BF%9E%E6%8E%A5%EF%BC%8854%EF%BC%89%EF%BC%9F.png)
我正在尝试使用ab
基准测试运行 4 个 ubuntu 机器的集群,nginx
这些机器由另一个运行 ubuntu 机器进行负载平衡haproxy
。对于那些感兴趣的人,我正在关注系统管理员广播教程系列。我无法从运行 osx 的笔记本电脑上成功对集群进行基准测试:
➜ ~ uname -a
Darwin mbp 15.6.0 Darwin Kernel Version 15.6.0: Mon Aug 29 20:21:34 PDT 2016; root:xnu-3248.60.11~1/RELEASE_X86_64 x86_64
➜ ~ system_profiler SPSoftwareDataType
Software:
System Software Overview:
System Version: OS X 10.11.6 (15G1004)
Kernel Version: Darwin 15.6.0
Boot Volume: Untitled
Boot Mode: Normal
Computer Name: Max’s MacBook Pro
User Name: Max Bigras (max)
Secure Virtual Memory: Enabled
System Integrity Protection: Disabled
Time since boot: 4 days 6:27
➜ ~ ab -n 10000 -c 25 http://127.0.0.1:8080/
This is ApacheBench, Version 2.3 <$Revision: 1706008 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 127.0.0.1 (be patient)
apr_socket_recv: Connection reset by peer (54)
我发现另一篇帖子提到了可能存在的问题osx 中的套接字限制。然而当我尝试提到的解决方案时它仍然不起作用:
➜ ~ sysctl kern.maxfiles
kern.maxfiles: 1048600
➜ ~ ulimit -S -n
1048576
➜ ~ ab -n 10000 -c 25 http://127.0.0.1:8080/
This is ApacheBench, Version 2.3 <$Revision: 1706008 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 127.0.0.1 (be patient)
apr_socket_recv: Connection reset by peer (54)
Total of 25 requests completed
我知道我至少对我的服务器造成了一些影响,因为我检查了正在运行的一个 Web 节点的日志文件nginx
通过 ssh 进入 web1(一个运行 nginx 的 ubuntu 机器):
vagrant@web1:~$ echo $USER
vagrant
vagrant@web1:~$ sudo tail -f /var/log/nginx/access.log
10.0.15.11 - - [02/Oct/2016:06:53:25 +0000] "GET / HTTP/1.0" 200 632 "-" "ApacheBench/2.3"
10.0.15.11 - - [02/Oct/2016:06:53:25 +0000] "GET / HTTP/1.0" 200 632 "-" "ApacheBench/2.3"
10.0.15.11 - - [02/Oct/2016:06:53:25 +0000] "GET / HTTP/1.0" 200 632 "-" "ApacheBench/2.3"
10.0.15.11 - - [02/Oct/2016:06:53:25 +0000] "GET / HTTP/1.0" 200 632 "-" "ApacheBench/2.3"
10.0.15.11 - - [02/Oct/2016:06:53:25 +0000] "GET / HTTP/1.0" 200 632 "-" "ApacheBench/2.3"
10.0.15.11 - - [02/Oct/2016:06:53:25 +0000] "GET / HTTP/1.0" 200 632 "-" "ApacheBench/2.3"
10.0.15.11 - - [02/Oct/2016:06:53:25 +0000] "GET / HTTP/1.0" 200 632 "-" "ApacheBench/2.3"
10.0.15.11 - - [02/Oct/2016:06:53:25 +0000] "GET / HTTP/1.0" 200 632 "-" "ApacheBench/2.3"
10.0.15.11 - - [02/Oct/2016:06:53:25 +0000] "GET / HTTP/1.0" 200 632 "-" "ApacheBench/2.3"
10.0.15.11 - - [02/Oct/2016:06:53:25 +0000] "GET / HTTP/1.0" 200 632 "-" "ApacheBench/2.3"
10.0.15.11 - - [02/Oct/2016:06:54:59 +0000] "GET / HTTP/1.0" 200 632 "-" "ApacheBench/2.3"
10.0.15.11 - - [02/Oct/2016:06:54:59 +0000] "GET / HTTP/1.0" 200 632 "-" "ApacheBench/2.3"
10.0.15.11 - - [02/Oct/2016:06:54:59 +0000] "GET / HTTP/1.0" 200 632 "-" "ApacheBench/2.3"
10.0.15.11 - - [02/Oct/2016:06:54:59 +0000] "GET / HTTP/1.0" 200 632 "-" "ApacheBench/2.3"
10.0.15.11 - - [02/Oct/2016:06:54:59 +0000] "GET / HTTP/1.0" 200 632 "-" "ApacheBench/2.3"
10.0.15.11 - - [02/Oct/2016:06:54:59 +0000] "GET / HTTP/1.0" 200 632 "-" "ApacheBench/2.3"
10.0.15.11 - - [02/Oct/2016:06:54:59 +0000] "GET / HTTP/1.0" 200 632 "-" "ApacheBench/2.3"
10.0.15.11 - - [02/Oct/2016:06:54:59 +0000] "GET / HTTP/1.0" 200 632 "-" "ApacheBench/2.3"
10.0.15.11 - - [02/Oct/2016:06:55:09 +0000] "GET / HTTP/1.1" 200 632 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36"
在 osx 上再试ab
一次:
➜ ~ echo $USER
max
➜ ~ date
Sat Oct 1 23:54:54 PDT 2016
➜ ~ ab -n 10000 -c 25 http://127.0.0.1:8080/
This is ApacheBench, Version 2.3 <$Revision: 1706008 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 127.0.0.1 (be patient)
apr_socket_recv: Connection reset by peer (54)
Total of 41 requests completed
➜ ~ date
Sat Oct 1 23:55:02 PDT 2016
➜ ~
上面你可以看到,除了尝试对集群进行基准测试之外,我还使用浏览器对其进行了测试,如下面的图所示/var/log/nginx/access.log
。问题肯定与我可以发出的请求数有限制有关。如果我降低请求数和并发值,那么它就可以正常工作:
➜ ~ ab -n 100 -c 1 http://127.0.0.1:8080/
This is ApacheBench, Version 2.3 <$Revision: 1706008 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 127.0.0.1 (be patient).....done
Server Software: nginx
Server Hostname: 127.0.0.1
Server Port: 8080
Document Path: /
Document Length: 632 bytes
Concurrency Level: 1
Time taken for tests: 0.218 seconds
Complete requests: 100
Failed requests: 0
Total transferred: 83800 bytes
HTML transferred: 63200 bytes
Requests per second: 458.17 [#/sec] (mean)
Time per request: 2.183 [ms] (mean)
Time per request: 2.183 [ms] (mean, across all concurrent requests)
Transfer rate: 374.95 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.3 0 2
Processing: 1 2 0.8 2 5
Waiting: 1 2 0.7 1 5
Total: 1 2 0.9 2 6
WARNING: The median and mean for the waiting time are not within a normal deviation
These results are probably not that reliable.
Percentage of the requests served within a certain time (ms)
50% 2
66% 2
75% 2
80% 2
90% 3
95% 4
98% 6
99% 6
100% 6 (longest request)
那么,我该如何配置 osx 以便可以使用 25 的并发度测试 10000 个请求ab
?
编辑:添加 nginx.conf
vagrant@web1:~$ cat /etc/nginx/nginx.conf
# Ansible managed: /home/vagrant/templates/nginx.conf.j2 modified on 2016-09-11 14:17:19 by vagrant on mgmt
user www-data;
worker_processes 1;
pid /var/run/nginx.pid;
worker_rlimit_nofile 1024;
events {
worker_connections 512;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
tcp_nopush "on";
tcp_nodelay "on";
#keepalive_timeout "65";
access_log "/var/log/nginx/access.log";
error_log "/var/log/nginx/error.log";
server_tokens off;
types_hash_max_size 2048;
# https://philio.me/backend-server-host-name-as-a-custom-header-with-nginx/
add_header X-Backend-Server $hostname;
# disable cache used for testing
add_header Cache-Control private;
add_header Last-Modified "";
sendfile off;
expires off;
etag off;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
答案1
此Connection reset by peer
错误意味着您的 Web 服务器在响应您的请求之前关闭了连接。因此,负载对于您的服务器来说太大,因此它开始断开连接。
您需要研究您的服务器设置,以了解发生这种情况的原因。研究您的日志文件。
该错误不是由于您的客户端引起的。
答案2
OSX 上存在问题ab
。我明天会写一个更清晰的答案,但目前这些项目符号来源应该可以帮助任何有兴趣使用ab
sysadmincasts 教程的人。tl;dr 是在运行 ubuntu 的节点ab
上安装mgmt
,然后在 ssh 进入时使用反向端口转发,有关更多详细信息,请参阅来源。
- ab 无法在 osx 上运行
- 渡渡鸟:
- 当 SSH 进入管理节点时进行反向端口转发https://stackoverflow.com/questions/16244601/vagrant-reverse-port-forwarding
- Vagrantfile 中的端口转发管理https://www.vagrantup.com/docs/networking/forwarded_ports.html
- 查看 PRhttps://github.com/mitchellh/vagrant/pull/7035
不要做:
答案3
我放弃尝试在 macOS 中实现这个功能,而是使用 docker:
docker run --rm -v `pwd`:`pwd` -w `pwd` -p $PORT:$PORT --network="bridge" jordi/ab \
-k -n $REQUESTS -c $CONCURRENCY "http://$HOST:$PORT/ingest"
AB docker 镜像:https://hub.docker.com/r/jordi/ab