Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

But with tcp port instead of unix socket I get 6.5 reqs per second.

With wrk

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

on unix socket: 6500 req per second

on tcp port: 300 req per second

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?

like image 373
Alex Avatar asked Dec 13 '25 14:12

Alex


1 Answers

You can trust the ab and wrk numbers.

You should therefore use Unix Sockets on production:

  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. 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. 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.

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.

like image 179
Arnaud Bouchez Avatar answered Dec 16 '25 07:12

Arnaud Bouchez