読者です 読者をやめる 読者になる 読者になる

nginxのパフォーマンスを向上させてみる

前回のエントリーでパフォーマンスの測定ができるようになりました。

今回はnginxのパフォーマンス向上に努めます。

なお、サイトの事前調査では最大リクエスト数の処理数が70なので、限界までリクエストするようにします。

最初に設定前のファイルを見てみましょう。

nginx.conf

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
        worker_connections 1024;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        # keepalive_timeout 10;
        # client_header_timeout 10;
        # client_body_timeout 10;
        # reset_timedout_connection on;
        send_timeout 10;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;
        gzip_disable "msie6";

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}

worker数の設定

worker_processes 4;

結果

Concurrency Level:      69
Time taken for tests:   4.444 seconds
Complete requests:      69
Failed requests:        0
Total transferred:      2385261 bytes
HTML transferred:       2334546 bytes
Requests per second:    15.53 [#/sec] (mean)
Time per request:       4443.779 [ms] (mean)
Time per request:       64.403 [ms] (mean, across all concurrent requests)
Transfer rate:          524.18 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       20   32   5.5     32      39
Processing:   359 2457 1250.9   2577    4403
Waiting:      315 2410 1250.7   2529    4356
Total:        379 2489 1256.1   2608    4442

Percentage of the requests served within a certain time (ms)
  50%   2597
  66%   3101
  75%   3579
  80%   3835
  90%   4261
  95%   4327
  98%   4442
  99%   4442
 100%   4442 (longest request)

私の初期設定では4になっていました。

こちらをautoに切り替えて、nginxに判断させます。

autoの場合は、自動でコア数に応じてくれます。

lscpuでコア数がわかります。

lscpu
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                2
On-line CPU(s) list:   0,1
Thread(s) per core:    1
Core(s) per socket:    1
Socket(s):             2
NUMA node(s):          1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 45
Model name:            Intel(R) Xeon(R) CPU E5-2640 0 @ 2.50GHz
Stepping:              7
CPU MHz:               2499.998
BogoMIPS:              4999.99
Hypervisor vendor:     KVM
Virtualization type:   full
L1d cache:             32K
L1i cache:             32K
L2 cache:              4096K
NUMA node0 CPU(s):     0,1

今回の場合は2ですね。

なぜ、4にしていたのだろうか・・・orz

結果

Concurrency Level:      69
Time taken for tests:   4.041 seconds
Complete requests:      69
Failed requests:        0
Total transferred:      2385261 bytes
HTML transferred:       2334546 bytes
Requests per second:    17.07 [#/sec] (mean)
Time per request:       4041.382 [ms] (mean)
Time per request:       58.571 [ms] (mean, across all concurrent requests)
Transfer rate:          576.38 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       21   33   5.7     33      46
Processing:   346 2222 1104.8   2259    3993
Waiting:      303 2175 1104.9   2215    3944
Total:        371 2255 1110.2   2292    4039

Percentage of the requests served within a certain time (ms)
  50%   2287
  66%   2794
  75%   3232
  80%   3401
  90%   3784
  95%   3933
  98%   4039
  99%   4039
 100%   4039 (longest request)

比較

Time per request:       4443.779 [ms] (mean)
Time per request:       64.403 [ms] (mean, across all concurrent requests)
Time per request:       4041.382 [ms] (mean)
Time per request:       58.571 [ms] (mean, across all concurrent requests)

わずかながらですが、早くなりました。

mulit accept

同時に受け入れるかどうかでの判定です。 こちらもonにしてあげましょう。

        multi_accept on;

結果

Concurrency Level:      69
Time taken for tests:   3.443 seconds
Complete requests:      69
Failed requests:        0
Total transferred:      2385261 bytes
HTML transferred:       2334546 bytes
Requests per second:    20.04 [#/sec] (mean)
Time per request:       3443.017 [ms] (mean)
Time per request:       49.899 [ms] (mean, across all concurrent requests)
Transfer rate:          676.55 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       22   33   5.8     36      39
Processing:   247 1815 979.8   1883    3402
Waiting:      200 1766 977.1   1836    3359
Total:        270 1848 985.3   1920    3441

Percentage of the requests served within a certain time (ms)
  50%   1919
  66%   2331
  75%   2694
  80%   2841
  90%   3207
  95%   3368
  98%   3441
  99%   3441
 100%   3441 (longest request)

どんどん早くなっていく。

Time per request:       3443.017 [ms] (mean)
Time per request:       49.899 [ms] (mean, across all concurrent requests)

こんな設定するだけでも、トータルのレスポンスの処理速度が1,000msも短縮できました。

わずかな設定でここまで差が出るとは・・・

ちゃんとサーバーの設定を見直すことは大事ですね。