2017年9月20日 星期三

Slim 2 與 Laravel 5.0,小比一下

使用 Laravel 一陣子了,好像只用到其中一小部分,如 Query builder、Request、View 等。然後,可以使用 Composer 將一些東西包進 Slim,程式就可以跑了。

環境是 Windwos server 2003+fast-cgi,PHP 為 PHP 5.4.45+wincache 及 PHP 5.6.24+opcache。

 $ ab -n 100 -c 4 http://10.161.81.188/test/sdic_qry/index.php/sql_qry?dep=P020

hello-world,只是單純送出 "Hello, world!" 的字串,用來測試空的 framework 的性能。Slim 2 有加裝一些 package,也設定一些習慣使用的 Laravel 的靜態介面。

sql_qry 則進行資料庫的 query,用以測試資料庫存取的影響。其中,後端資料庫為安裝在 VM 的 SQL 2000 server。

PHP 5.6.24

Slim 2,debug off,hello-world,,Requests per second:    189.16 [#/sec]
Slim 2,debug on,hello-world,Requests per second:    187.35 [#/sec] (mean)
Slim 2,debug off,sql_qry,Requests per second:    143.17 [#/sec] (mean)
Slim 2,debug on,sql_qry,Requests per second:    129.30 [#/sec] (mean)

Slim 3,debug on,hello-world,Requests per second:    143.44 [#/sec] (mean)
Slim 3,debug off,sql_qry,Requests per second:    118.56 [#/sec] (mean)

Laravel 5.0,hello-world, 66.33 [#/sec] (mean)
Laravel 5.0,sql_qry,5.6.24,Requests per second:    55.74 [#/sec] (mean)

Slim 2,debug off,laravel-session (file/database),Requests per second:    60 [#/sec] (mean)

PHP 5.4.45

Slim 2,debug on,hello-world,Requests per second:    80.37 [#/sec] (mean)
Slim 2,debug off,hello-world,Requests per second:    110.83 [#/sec] (mean)
Slim 2,debug off,sql_qry,Requests per second:    87.60 [#/sec] (mean)
Slim 2,debug on,sql_qry,Requests per second:    72.85 [#/sec] (mean)
L5,sql_qry,5.6.24,Requests per second:    25.22 [#/sec] (mean)
L5,hello-world, 29.75 [#/sec] (mean)

測試中,Tracy/Debuger 會造成 Failed requests。
在 Slim 2 中,加上 Laravel 的 session 後,效能降到和 Laravel 差不多。
其中,session 的垃圾收集,會大幅的拖慢效能。垃圾收集的時機,由設定中的 'lottery' => [2, 100] 來決定,此代表 50 次,會做一次垃圾收集。減少垃圾收集的機率,可提升效能。
請參考 PHP Session Garbage Collection: The unknown performance bottleneck
所以,是否使用 Laravel 的 session,會是一個決擇。然而,其可以選擇不同的 drvier,實在是非常方便的作法。

以下為使用下列指令測試的結果

$ ab -n 100 -c 10 http://10.161.81.188/test/sdic_qry/index.php/cou_cur/introduction?cur=P020

Slim 2 的結果如下

Server Software:        Microsoft-IIS/6.0
Server Hostname:        10.161.81.188
Server Port:            80

Document Path:          /test/sdic_qry/index.php/cou_cur/introduction?cur=P020
Document Length:        18064 bytes

Concurrency Level:      10
Time taken for tests:   1.373 seconds
Complete requests:      100
Failed requests:        26
   (Connect: 0, Receive: 0, Length: 26, Exceptions: 0)
Write errors:           0
Total transferred:      1829852 bytes
HTML transferred:       1806452 bytes
Requests per second:    72.85 [#/sec] (mean)
Time per request:       137.275 [ms] (mean)
Time per request:       13.727 [ms] (mean, across all concurrent requests)
Transfer rate:          1301.74 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.7      0       4
Processing:    52  129  41.4    119     330
Waiting:       50  128  41.4    118     329
Total:         52  130  41.5    120     331

Percentage of the requests served within a certain time (ms)
  50%    120
  66%    131
  75%    140
  80%    145
  90%    178
  95%    206
  98%    308
  99%    331
 100%    331 (longest request)

Laravel 5.0 的結果如下

Server Software:        Microsoft-IIS/6.0
Server Hostname:        10.161.81.188
Server Port:            80

Document Path:          /test/sdic_for_reg/index.php/cou_cur?cur=P020
Document Length:        14987 bytes

Concurrency Level:      10
Time taken for tests:   3.927 seconds
Complete requests:      100
Failed requests:        0
Write errors:           0
Total transferred:      1606344 bytes
HTML transferred:       1498700 bytes
Requests per second:    25.46 [#/sec] (mean)
Time per request:       392.702 [ms] (mean)
Time per request:       39.270 [ms] (mean, across all concurrent requests)
Transfer rate:          399.46 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   1.1      0       5
Processing:   146  372  95.2    346     738
Waiting:      143  370  95.0    345     737
Total:        147  373  95.4    347     739

Percentage of the requests served within a certain time (ms)
  50%    347
  66%    400
  75%    436
  80%    463
  90%    495
  95%    527
  98%    626
  99%    739
 100%    739 (longest request)

結果是 Slim 2 大概是 Laravel 5.0 的 3 倍。然後,隨著程式成長,使用 Slim 的環境,也由每秒完成的 request 也由 72 個降到 65 個左右。

再 Optimizing Composer's autoloader 一下,指令 composer  dumpautoload -o,又稍微提升到每秒完成 75 個 request。

2018-01-29 測試

應用程式的條件,使用 Laravel 的 DB 和 View,關掉 debug。在 optimize 以前,大約 100 [#/sec],optimize 後,約 118 [#/sec]。
不過,結果不是很穩定,後來再測,降到約 70 [#/sec]。

沒有留言: