90
77

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

俺のLaravelがこんなに遅いわけがない

Last updated at Posted at 2020-04-21

Laravel

環境(ベース)

  • PHP 7.4.5
  • Laravel 7.5.1

Docker for Macでnginxとphp-fpmコンテナをunixソケットで繋いだ環境で試してます。

環境の差異

  1. OPcache なし
  2. OPcache あり、プリロードなし
  3. OPcache あり、プリロードあり

比較方法

  • Laravelのwelcome画面をabコマンドの結果で比較します。

Requests per second(1秒間に捌けるリクエスト数)、Time per request(1リクエストあたりの処理時間)に着目します。

以前こんな記事も書いてます。
Apache Bench を初めて使ってみた

OPcacheなし

php.ini(設定例)
[opcache]
opcache.enable = 0
$ ab -n 1000 -c 100 http://127.0.0.1/
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
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)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:        nginx/1.17.8
Server Hostname:        127.0.0.1
Server Port:            80

Document Path:          /
Document Length:        2426 bytes

Concurrency Level:      100
Time taken for tests:   18.432 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      3446000 bytes
HTML transferred:       2426000 bytes
Requests per second:    54.25 [#/sec] (mean)
Time per request:       1843.175 [ms] (mean)
Time per request:       18.432 [ms] (mean, across all concurrent requests)
Transfer rate:          182.58 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   2.5      0      22
Processing:    79 1744 326.5   1836    2038
Waiting:       69 1743 326.6   1834    2038
Total:         83 1745 324.6   1836    2039

Percentage of the requests served within a certain time (ms)
  50%   1836
  66%   1865
  75%   1883
  80%   1894
  90%   1926
  95%   1948
  98%   1988
  99%   2009
 100%   2039 (longest request)
  • 1秒間に捌けるリクエスト数: 54.25
  • 1リクエストあたりの処理時間: 18.432 (ms)

俺のLaravelがこんなに遅いわけがない...

OPcacheあり、プリロードなし

php.ini(設定例)
[opcache]
opcache.enable = 1
opcache.memory_consumption = 128
opcache.interned_strings_buffer = 8
opcache.max_accelerated_files = 4000
opcache.validate_timestamps = 0
opcache.huge_code_pages = 0
$ ab -n 1000 -c 100 http://127.0.0.1/
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
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)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:        nginx/1.17.8
Server Hostname:        127.0.0.1
Server Port:            80

Document Path:          /
Document Length:        2426 bytes

Concurrency Level:      100
Time taken for tests:   3.318 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      3446000 bytes
HTML transferred:       2426000 bytes
Requests per second:    301.41 [#/sec] (mean)
Time per request:       331.772 [ms] (mean)
Time per request:       3.318 [ms] (mean, across all concurrent requests)
Transfer rate:          1014.32 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    2   4.4      1      24
Processing:    31  316  60.1    319     481
Waiting:        9  311  59.3    316     461
Total:         34  318  57.9    320     483

Percentage of the requests served within a certain time (ms)
  50%    320
  66%    335
  75%    346
  80%    354
  90%    381
  95%    404
  98%    413
  99%    421
 100%    483 (longest request)
  • 1秒間に捌けるリクエスト数: 301.41
  • 1リクエストあたりの処理時間: 3.318 (ms)

54.25 から 301.41 へおよそ5.55倍の高速化されました!!
圧倒的すぎる速さ!!キャッシュ効果恐るべし!!

OPcacheあり、プリロードあり

php.ini(設定例)
[opcache]
opcache.enable = 1
opcache.memory_consumption = 128
opcache.interned_strings_buffer = 8
opcache.max_accelerated_files = 4000
opcache.validate_timestamps = 0
opcache.huge_code_pages = 0
opcache.preload = /var/www/preload.php
opcache.preload_user = www-data

このpreload.phpを参考にしてます。ignoreにいくつか追加してます。
ただお試し的に使ってるので、また内容まとまったら別記事にしたいと思います。

$ ab -n 1000 -c 100 http://127.0.0.1/
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
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)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:        nginx/1.17.8
Server Hostname:        127.0.0.1
Server Port:            80

Document Path:          /
Document Length:        2426 bytes

Concurrency Level:      100
Time taken for tests:   2.878 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      3446000 bytes
HTML transferred:       2426000 bytes
Requests per second:    347.40 [#/sec] (mean)
Time per request:       287.850 [ms] (mean)
Time per request:       2.878 [ms] (mean, across all concurrent requests)
Transfer rate:          1169.09 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   1.3      0       7
Processing:    49  268  46.5    275     344
Waiting:       35  267  46.5    274     342
Total:         54  269  45.5    276     344

Percentage of the requests served within a certain time (ms)
  50%    276
  66%    287
  75%    294
  80%    298
  90%    312
  95%    322
  98%    331
  99%    335
 100%    344 (longest request)
  • 1秒間に捌けるリクエスト数: 347.40
  • 1リクエストあたりの処理時間: 2.878 (ms)

301.41 から 347.40 へおよそ1.15倍の高速化されました!!

もう俺のLaravelが遅いなんて言わせない...!!

まとめ

# OPcacheなし
Requests per second:    54.25 [#/sec] (mean)
Time per request:       18.432 [ms] (mean, across all concurrent requests)

# OPcacheあり、プリロードなし
Requests per second:    301.41 [#/sec] (mean)
Time per request:       3.318 [ms] (mean, across all concurrent requests)

# OPcacheあり、プリロードあり
Requests per second:    347.40 [#/sec] (mean)
Time per request:       2.878 [ms] (mean, across all concurrent requests)

何も設定してない 54.25 の状態から 347.40 へおよそ6.4倍と劇的な高速化を遂げました!!!
PHPのポテンシャル半端ないですね!!!

プリロード自体は初めてなので諸々問題出てくるかもしれませんが、何か問題あったらまた記事にしていきたいと思います😃

90
77
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
90
77

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?