简体   繁体   English

使用 ab 和 wrk 通过 unix socket vs tcp 对 PHP 进行基准测试

[英]Benchmarking PHP through unix socket vs tcp with ab and wrk

When benchmarking regular PHP 5.6 through unix socket the results are many order of magnitues better than tcp port.通过 unix socket 对常规 PHP 5.6 进行基准测试时,结果比 tcp 端口好很多数量级。

When I run a command like this:当我运行这样的命令时:

$ ab -k -n 10000 -c 1000 http://127.0.0.1/api/user/1

I get an avg 3272 reqs per second.我每秒平均收到 3272 个请求。

But with tcp port instead of unix socket I get 6.5 reqs per second.但是使用 tcp 端口而不是 unix 套接字,我每秒得到 6.5 个请求。

With wrk与 wrk

$ wrk -t1 -c1000 -d5s http://127.0.0.1:80/api/user/1

on unix socket: 6500 req per second在 unix 套接字上:每秒 6500 个请求

on tcp port: 300 req per second在 tcp 端口上:每秒 300 个请求

How am I supposed to use these benchmarks to get a feel of how my server and code can handle load when I get these kinds of results?当我得到这些结果时,我应该如何使用这些基准来了解我的服务器和代码如何处理负载?

Should I trust the tcp port or unix socket one?我应该信任 tcp 端口还是 unix 套接字一?

How are you using a Unix socket in your example? 你在例子中如何使用Unix套接字? The -k option is http keep alive. -k选项是http keep alive。

Unix sockets are used for inter-process communication, so I'm almost certain that the primary access method used to access your webserver is via tcp. Unix套接字用于进程间通信,所以我几乎可以肯定用于访问您的Web服务器的主要访问方法是通过tcp。

Really this is testing the efficiency of your code AND webserver. 真的,这是测试你的代码和网络服务器的效率。 If you care most about the efficiency of your code, you should consider inspecting xdebug output also. 如果您最关心代码的效率,则还应考虑检查xdebug输出。

You can trust the ab and wrk numbers.您可以信任abwrk数字。

You should therefore use Unix Sockets on production:因此,您应该在生产中使用 Unix 套接字:

  1. Unix Domain Sockets make nginx faster to communicate with php-fpm, and use less resource, because TCP has an overhead, as protocol, over Unix sockets, even on the loopback. Unix Domain Sockets 使 nginx 更快地与 php-fpm 通信,并且使用更少的资源,因为 TCP 作为协议在 Unix 套接字上具有开销,即使在环回上也是如此。 Your numbers show it.你的数字表明了这一点。

  2. Unix Domain Sockets are not routable, so they are not accessible from the outside, so they are usually considered safer than TCP for local communication. Unix Domain Sockets 不可路由,因此无法从外部访问,因此通常认为它们比 TCP 更安全用于本地通信。 Disabling the firewall may allow external process to access directly php-fpm, whereas with Unix sockets this is never possible, but they are local by definition.禁用防火墙可能会允许外部进程直接访问 php-fpm,而对于 Unix 套接字,这是不可能的,但根据定义它们是本地的。

Of course, the main bottleneck will be the communication between the clients and nginx, using HTTP/TCP, but at least you can be confident that everything is as good as possible within your server, by using Unix sockets for php-fpm.当然,主要的瓶颈将是客户端和 nginx 之间的通信,使用 HTTP/TCP,但至少你可以确信你的服务器中的一切都尽可能好,通过使用 Unix 套接字为 php-fpm。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM