Nginxのパフォーマンス調整用に「wrk」を入れてみる

ちょっと Nginx のパフォーマンス調整をしたくなったけれど、Apachebench(ab)使うのもなんだかなあと思って新しいのを使ってみた。

たぶん、コンカレントな性能を見るには ab よりもこの wrk のほうがいいんじゃないかと思う。中身に luaJit を使っているので軽くて信頼が置けそう。印象論。手順。

$ pwd
/usr/src/netsrc
$ git clone https://github.com/wg/wrk.git
$ cd wrk
$ make
$ sudo make -n install
make: *** No rule to make target `install'.  Stop.

げ! install ターゲットがない! なに?? 最近の流行??
しかたない。適当に。

$ sudo install -s wrk /usr/sbin/

これで、

$ wrk -c 100 -t 10 -d 10 http://localhost/
Running 10s test @ http://localhost/
  10 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    28.17ms   88.09ms   1.19s    93.88%
    Req/Sec     1.54k   555.04     2.87k    69.95%
  147794 requests in 10.10s, 53.84MB read
Requests/sec:  14632.23
Transfer/sec:      5.33MB

とかやる。14632.23 Requests/sec。まあ遅い。

で、ちょっと調整することにする。ちなみに「h2o 使えば?」とかなしで。

$ cd /etc/nginx
$ cp nginx.conf{,.org}
$ diff -u --from-file=nginx.conf.org nginx.conf
--- nginx.conf.org      2015-05-28 15:04:16.771189900 +0900
+++ nginx.conf  2015-05-28 16:02:41.068189900 +0900
@@ -1,16 +1,19 @@

 user  nginx;
-worker_processes  1;
+worker_processes  auto;
+worker_rlimit_nofile 100000;

 error_log  /var/log/nginx/error.log warn;
 pid        /var/run/nginx.pid;

 events {
-    worker_connections  1024;
+    worker_connections  4096;
+    accept_mutex_delay 100ms;
 }


 http {
+    server_tokens off;
     include       /etc/nginx/mime.types;
     default_type  application/octet-stream;

@@ -18,12 +21,15 @@
                       '$status $body_bytes_sent "$http_referer" '
                       '"$http_user_agent" "$http_x_forwarded_for"';

-    access_log  /var/log/nginx/access.log  main;
+    #access_log  /var/log/nginx/access.log  main;
+    access_log off;

     sendfile        on;
-    #tcp_nopush     on;
+    tcp_nopush     on;

-    keepalive_timeout  65;
+    keepalive_timeout  10;
+    open_file_cache max=100 inactive=30s;
+    open_file_cache_errors on;

     #gzip  on;
$ wrk -c 100 -t 10 -d 10 http://localhost/
Running 10s test @ http://localhost/
  10 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     9.21ms   15.68ms 208.49ms   91.67%
    Req/Sec     1.88k   550.65     3.97k    73.12%
  189115 requests in 10.11s, 66.73MB read
Requests/sec:  18706.75
Transfer/sec:      6.60MB

14632.23 Requests/sec → 18706.75 Requests/sec

んー。2000リクエスト/秒の改善か。今回、バックエンドとしてさらに、

nginx + php_fpm + WordPress

が入ってるので、こんなもんなんだろうな。

って、ああ。Proxyキャッシュ忘れてる。ディレクトリがなければ、

# mkdir -p /var/cache/nginx/{cache,proxy_temp}
# chown -R nginx.nginx /var/cache/nginx

として、

# diff -u --from-file=nginx.conf.org nginx.conf
--- nginx.conf.org      2015-05-28 15:04:16.771189900 +0900
+++ nginx.conf  2015-05-28 17:11:48.873189900 +0900
@@ -1,16 +1,19 @@

 user  nginx;
-worker_processes  1;
+worker_processes auto;
+worker_rlimit_nofile 100000;

 error_log  /var/log/nginx/error.log warn;
 pid        /var/run/nginx.pid;

 events {
-    worker_connections  1024;
+    worker_connections 10240;
+    accept_mutex_delay 100ms;
 }


 http {
+    server_tokens off;
     include       /etc/nginx/mime.types;
     default_type  application/octet-stream;

@@ -18,15 +21,22 @@
                       '$status $body_bytes_sent "$http_referer" '
                       '"$http_user_agent" "$http_x_forwarded_for"';

-    access_log  /var/log/nginx/access.log  main;
+    #access_log  /var/log/nginx/access.log  main;
+    access_log off;

     sendfile        on;
-    #tcp_nopush     on;
+    tcp_nopush     on;

-    keepalive_timeout  65;
+    keepalive_timeout  10;
+    open_file_cache max=100 inactive=30s;
+    open_file_cache_errors on;

     #gzip  on;

+    proxy_cache_path /var/cache/nginx/cache levels=1:2 keys_zone=czone:16m max_size=256m inactive=10m;
+    proxy_cache_valid 200 8m;
+    proxy_temp_path  /var/cache/nginx/proxy_temp;
+
     include /etc/nginx/conf.d/*.conf;
     include /etc/nginx/vhosts/*.conf;
 }

とかしておこう。ただ、キャッシュはふつうに wrk でベンチマークしても no_cache だろうから効果不明。めんどくさいし体感で見るか。