測試用 Octane 加速 Laravel10,結果振奮人心,達到 30~50 倍提速,同時對比Hyperf3壓力測試

SimmerPan發表於2023-05-12

原文地址:yiba.fun/php/hyperf-laravel-perfor... 歡迎收藏。

Laravel 開發爽,但效能完全無法忍受。剛完成一個專案,有點時間,試試看用 octane 加持後是否能擺脫 Laravel 的效能魔咒。
Laravel 應用基本可可無縫遷移到 Hyperf,因此同時測試 Hyperf,看看 Swoole 協程非同步加持的 Hyperf 是不是比 Laravel 快很多。

測試環境

伺服器: 阿里輕量雲伺服器
CPU: 2核
記憶體: 2G
OS:  CentOS 8.5
PHP: 8.2.5
MySQL: 8.0
Swoole: 5.0.3
Laravel: 10.8
Hyperf: 3.0

Laravel 壓測

# 建立 Demo 模型、遷移、控制器
php artisan make:model Demo -mc

demos 表遷移結構:

    Schema::create('demos', function (Blueprint $table) {
        $table->id();
        $table->string('text');
        $table->timestamps();
    });

Demo 模型類:

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Demo extends Model
{
    use HasFactory;
    protected $fillable = ['text'];
}

DemoController 控制器類:

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class DemoController extends Controller
{
    public function hello()
    {
        return 'hello laravel';
    }

    public function dbRead()
    {
        $demo = \App\Models\Demo::find(1);
        return ['text' => $demo->text];
    }

    public function dbQuery()
    {
        $items = \App\Models\Demo::query()
            ->where('id', '>', 100)
            ->limit(10)
            ->get();
        return ['items' => $items];
    }

    public function dbWrite()
    {
        $demo = \App\Models\Demo::create([
            'text' => date('Y-m-d H:i:s'). '/' . microtime()
        ]);
        return ['id' => $demo->id];
    }
}

php82 artisan route:list 檢視路由:

  GET|HEAD   db-query ........................ DemoController@dbQuery
  GET|HEAD   db-read .......................... DemoController@dbRead
  GET|HEAD   db-write ........................ DemoController@dbWrite
  GET|HEAD   hello ............................. DemoController@hello

Laravel 預設啟用了檔案儲存的 Session,每次壓測前先刪除 session 檔案,否則 session 檔案多了影響效能。
換成 redis 儲存 session 可不用每次壓測前刪除 session,對測試結果影響不大。

  • 啟動 Laravel web 命令: php artisan serve
  • 啟動 Laravel octane 命令: php artisan octane:start

下面開始用 wrk 壓力測試,每個連結測 3 次。

測試返回簡單字串

Laravel 返回簡單字串

wrk -c100 -t50 http://127.0.0.1:8000/hello
------
Running 10s test @ http://127.0.0.1:8000/hello
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     1.03s   575.81ms   1.95s    60.00%
    Req/Sec     4.93      4.94    10.00     53.54%
  101 requests in 10.04s, 112.93KB read
  Socket errors: connect 0, read 101, write 0, timeout 81
Requests/sec:     10.06
Transfer/sec:     11.25KB
------
Running 10s test @ http://127.0.0.1:8000/hello
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     1.02s   567.66ms   1.92s    57.89%
    Req/Sec     4.52      4.81    10.00     57.89%
  101 requests in 10.10s, 112.93KB read
  Socket errors: connect 0, read 101, write 0, timeout 82
Requests/sec:     10.00
Transfer/sec:     11.19KB
------
Running 10s test @ http://127.0.0.1:8000/hello
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   984.59ms  553.07ms   1.89s    57.89%
    Req/Sec     4.91      4.91    10.00      7.07%
  102 requests in 10.09s, 114.05KB read
  Socket errors: connect 0, read 102, write 0, timeout 83
Requests/sec:     10.11
Transfer/sec:     11.30KB

Octane 加持 返回簡單字串

wrk -c100 -t50 http://127.0.0.1:8000/hello
------
Running 10s test @ http://127.0.0.1:8000/hello
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   176.85ms   63.66ms 486.86ms   86.20%
    Req/Sec    11.63      5.04    30.00     62.43%
  5677 requests in 10.10s, 5.88MB read
Requests/sec:    561.98
Transfer/sec:    596.00KB
------
wrk -c100 -t50 http://127.0.0.1:8000/hello
Running 10s test @ http://127.0.0.1:8000/hello
  50 threads and 100 connections
   Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   167.67ms   48.92ms 381.56ms   90.32%
    Req/Sec    12.37      5.10    40.00     63.35%
  5971 requests in 10.09s, 6.18MB read
Requests/sec:    591.68
Transfer/sec:    627.50KB
------
Running 10s test @ http://127.0.0.1:8000/hello
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   172.55ms   62.36ms 428.96ms   85.20%
    Req/Sec    11.99      5.58   121.00     59.08%
  5733 requests in 10.09s, 5.94MB read
Requests/sec:    568.07
Transfer/sec:    602.47KB

Laravel 資料庫寫入

Laravel MySQL 資料庫寫入

wrk -c100 -t50 http://127.0.0.1:8000/db-write
------
Running 10s test @ http://127.0.0.1:8000/db-write
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     1.04s   578.39ms   1.97s    62.50%
    Req/Sec     4.23      4.49    10.00     65.85%
  82 requests in 10.04s, 90.72KB read
  Socket errors: connect 0, read 82, write 0, timeout 66
Requests/sec:      8.16
Transfer/sec:      9.03KB
------
Running 10s test @ http://127.0.0.1:8000/db-write
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     1.05s   587.46ms   1.96s    62.50%
    Req/Sec     4.50      4.61    10.00     62.20%
  82 requests in 10.05s, 90.81KB read
  Socket errors: connect 0, read 82, write 0, timeout 66
Requests/sec:      8.16
Transfer/sec:      9.04KB
------
Running 10s test @ http://127.0.0.1:8000/db-write
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     1.03s   578.10ms   1.95s    62.50%
    Req/Sec     4.41      4.59    10.00     63.86%
  83 requests in 10.04s, 91.92KB read
  Socket errors: connect 0, read 83, write 0, timeout 67
Requests/sec:      8.26
Transfer/sec:      9.15KB

Laravel Octane 加持 資料庫寫入

MySQL
wrk -c100 -t50 http://127.0.0.1:8000/db-write
------
Running 10s test @ http://127.0.0.1:8000/db-write
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   416.60ms   54.46ms 600.26ms   92.54%
    Req/Sec     5.92      3.40    20.00     52.63%
  2360 requests in 10.07s, 2.42MB read
Requests/sec:    234.35
Transfer/sec:    246.19KB
------
Running 10s test @ http://127.0.0.1:8000/db-write
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   421.20ms   70.59ms 595.63ms   85.63%
    Req/Sec     5.35      3.02    20.00     64.08%
  2331 requests in 10.06s, 2.39MB read
Requests/sec:    231.82
Transfer/sec:    243.59KB
------
Running 10s test @ http://127.0.0.1:8000/db-write
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   417.45ms   72.56ms 635.48ms   87.63%
    Req/Sec     5.55      3.17    20.00     59.15%
  2352 requests in 10.05s, 2.41MB read
Requests/sec:    233.92
Transfer/sec:    245.80KB
Sqlite
wrk -c100 -t50 http://127.0.0.1:8000/db-write
------
Running 10s test @ http://127.0.0.1:8000/db-write
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   441.21ms  103.73ms 882.21ms   82.12%
    Req/Sec     4.97      2.84    20.00     65.66%
  2226 requests in 10.04s, 2.28MB read
Requests/sec:    221.63
Transfer/sec:    232.89KB
------
Running 10s test @ http://127.0.0.1:8000/db-write
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   448.64ms   98.24ms 774.23ms   81.18%
    Req/Sec     4.91      2.72    20.00     70.34%
  2189 requests in 10.06s, 2.25MB read
Requests/sec:    217.59
Transfer/sec:    228.64KB
------
Running 10s test @ http://127.0.0.1:8000/db-write
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   438.36ms   74.98ms 622.64ms   84.38%
    Req/Sec     5.72      3.45    20.00     50.08%
  2241 requests in 10.04s, 2.30MB read
Requests/sec:    223.12
Transfer/sec:    234.60KB

測試 Laravel 讀取資料庫

Laravel MySQL 資料庫讀取

wrk -c100 -t50 http://127.0.0.1:8000/db-read
----
Running 10s test @ http://127.0.0.1:8000/db-read
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     1.03s   558.31ms   1.90s    60.00%
    Req/Sec     4.44      4.61    10.00     63.41%
  82 requests in 10.06s, 93.77KB read
  Socket errors: connect 0, read 82, write 0, timeout 67
Requests/sec:      8.15
Transfer/sec:      9.32KB
------
Running 10s test @ http://127.0.0.1:8000/db-read
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     1.07s   593.05ms   2.00s    64.71%
    Req/Sec     4.80      4.79    10.00     11.90%
  84 requests in 10.05s, 96.06KB read
  Socket errors: connect 0, read 84, write 0, timeout 67
Requests/sec:      8.36
Transfer/sec:      9.56KB
------
Running 10s test @ http://127.0.0.1:8000/db-read
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     1.06s   589.02ms   1.98s    64.71%
    Req/Sec     3.95      4.30    10.00     70.24%
  84 requests in 10.05s, 96.06KB read
  Socket errors: connect 0, read 84, write 0, timeout 67
Requests/sec:      8.36
Transfer/sec:      9.56KB

Laravel + Octane 資料庫讀取

MySQL
wrk -c100 -t50 http://127.0.0.1:8000/db-read
------
Running 10s test @ http://127.0.0.1:8000/db-read
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   252.56ms   59.13ms 460.71ms   89.38%
    Req/Sec     8.35      2.56    30.00     74.44%
  3945 requests in 10.08s, 4.18MB read
Requests/sec:    391.33
Transfer/sec:    424.96KB
------
Running 10s test @ http://127.0.0.1:8000/db-read
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   258.04ms   69.85ms 588.53ms   88.38%
    Req/Sec     7.93      2.63    20.00     70.36%
  3858 requests in 10.08s, 4.09MB read
Requests/sec:    382.74
Transfer/sec:    415.63KB
------
Running 10s test @ http://127.0.0.1:8000/db-read
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   259.81ms   64.02ms 596.55ms   88.66%
    Req/Sec     7.97      2.67    20.00     70.78%
  3812 requests in 10.08s, 4.04MB read
Requests/sec:    378.12
Transfer/sec:    410.62KB
Sqlite
wrk -c100 -t50 http://127.0.0.1:8000/db-read
------
Running 10s test @ http://127.0.0.1:8000/db-read
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   196.67ms   55.13ms 429.39ms   88.82%
    Req/Sec    10.31      4.03    40.00     76.70%
  5081 requests in 10.10s, 5.42MB read
Requests/sec:    503.14
Transfer/sec:    549.33KB
------
Running 10s test @ http://127.0.0.1:8000/db-read
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   226.42ms   79.57ms 583.51ms   88.39%
    Req/Sec     9.35      3.74    40.00     74.37%
  4451 requests in 10.10s, 4.75MB read
Requests/sec:    440.62
Transfer/sec:    481.07KB
------
Running 10s test @ http://127.0.0.1:8000/db-read
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   207.83ms   69.91ms 581.83ms   87.62%
    Req/Sec    10.17      4.10    29.00     73.47%
  4863 requests in 10.10s, 5.18MB read
Requests/sec:    481.50
Transfer/sec:    525.70KB

Laravel dbQuery 壓測

MyQSL 壓測結果:

wrk -c100 -t50  http://localhost:8000/db-query
------
Running 10s test @ http://localhost:8000/db-query
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     1.03s   583.06ms   1.97s    62.50%
    Req/Sec     4.44      4.62    10.00     62.96%
  81 requests in 10.06s, 205.66KB read
  Socket errors: connect 0, read 81, write 0, timeout 65
Requests/sec:      8.05
Transfer/sec:     20.45KB
------
Running 10s test @ http://localhost:8000/db-query
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     1.07s   596.18ms   1.99s    58.82%
    Req/Sec     4.35      4.51    10.00     62.65%
  83 requests in 10.09s, 210.74KB read
  Socket errors: connect 0, read 83, write 0, timeout 66
Requests/sec:      8.22
Transfer/sec:     20.88KB
------
Running 10s test @ http://localhost:8000/db-query
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     1.01s   569.61ms   1.93s    62.50%
    Req/Sec     4.60      4.67    10.00     60.98%
  82 requests in 10.06s, 208.20KB read
  Socket errors: connect 0, read 82, write 0, timeout 66
Requests/sec:      8.15
Transfer/sec:     20.70KB

Laravel + Octane dbQuery 壓測

MyQSL 壓測結果:

wrk -c100 -t50  http://localhost:8000/db-query
------
Running 10s test @ http://127.0.0.1:8000/db-query
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   338.83ms   82.20ms 694.42ms   87.27%
    Req/Sec     6.35      2.80    20.00     62.32%
  2923 requests in 10.07s, 7.09MB read
Requests/sec:    290.16
Transfer/sec:    720.59KB
------
Running 10s test @ http://127.0.0.1:8000/db-query
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   318.90ms   58.37ms 541.45ms   89.61%
    Req/Sec     6.49      2.63    20.00     66.13%
  3110 requests in 10.08s, 7.54MB read
Requests/sec:    308.58
Transfer/sec:    766.32KB
------
Running 10s test @ http://127.0.0.1:8000/db-query
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   332.35ms   77.59ms 691.96ms   84.84%
    Req/Sec     6.55      2.96    30.00     58.93%
  2989 requests in 10.09s, 7.25MB read
Requests/sec:    296.20
Transfer/sec:    735.58KB

Sqlite 壓測結果:

Running 10s test @ http://127.0.0.1:8000/db-query
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   250.18ms   66.68ms 633.87ms   86.06%
    Req/Sec     8.25      2.68    50.00     76.08%
  4002 requests in 10.10s, 9.73MB read
Requests/sec:    396.21
Transfer/sec:      0.96MB
------
Running 10s test @ http://127.0.0.1:8000/db-query
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   266.82ms   80.43ms 640.53ms   86.86%
    Req/Sec     7.95      2.60    20.00     73.93%
  3744 requests in 10.10s, 9.10MB read
Requests/sec:    370.64
Transfer/sec:      0.90MB

Hyperf 壓測

建立表遷移、模型、控制器命令:

php bin/hyperf.php gen:migration create_demos_table
php bin/hyperf.php gen:model demos
php bin/hyperf.php gen:controller DemoController

demos 表遷移結構:

    Schema::create('demos', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('text');
        $table->datetimes();
    });

Hyperf Demo 模型類:

namespace App\Model;

class Demo extends Model
{
    protected ?string $table = 'demos';
    protected array $fillable = ['text'];
}

Hyperf DemoController 控制器類:

namespace App\Controller;

use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\HttpServer\Annotation\AutoController;

#[AutoController]
class DemoController
{
    public function hello()
    {
        return 'hello hyperf';
    }

    public function dbRead()
    {
        $demo = \App\Model\Demo::find(1);
        return ['text' => $demo->text];
    }

    public function dbQuery()
    {
        $items = \App\Model\Demo::query()
            ->where('id', '>', 100)
            ->limit(10)
            ->get();
        return ['items' => $items];
    }

    public function dbWrite()
    {
        $demo = \App\Model\Demo::create([
            'text' => date('Y-m-d H:i:s'). '/' . microtime()
        ]);
        return ['id' => $demo->id];
    }
}

php bin/hyperf.php describe:routes 檢視路由表:

+--------+---------------+---------------+----------------------------------------+------------+
| Server | Method        | URI           | Action                                 | Middleware |
+--------+---------------+---------------+----------------------------------------+------------+
| http   | GET|POST|HEAD | /demo/hello   | App\Controller\DemoController::hello   |            |
+--------+---------------+---------------+----------------------------------------+------------+
| http   | GET|POST|HEAD | /demo/dbRead  | App\Controller\DemoController::dbRead  |            |
+--------+---------------+---------------+----------------------------------------+------------+
| http   | GET|POST|HEAD | /demo/dbQuery | App\Controller\DemoController::dbQuery |            |
+--------+---------------+---------------+----------------------------------------+------------+
| http   | GET|POST|HEAD | /demo/dbWrite | App\Controller\DemoController::dbWrite |            |
+--------+---------------+---------------+----------------------------------------+------------+

Hyperf 返回簡單字串

wrk -c100 -t50 http://localhost:9501/demo/hello

Running 10s test @ http://localhost:9501/demo/hello
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     4.68ms    2.13ms  40.85ms   79.38%
    Req/Sec   436.05    107.57     4.67k    96.71%
  217542 requests in 10.10s, 31.95MB read
Requests/sec:  21538.14
Transfer/sec:      3.16MB
------
Running 10s test @ http://localhost:9501/demo/hello
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     4.73ms    2.43ms  50.35ms   81.39%
    Req/Sec   434.94     91.08     2.66k    86.01%
  217204 requests in 10.10s, 31.90MB read
Requests/sec:  21505.09
Transfer/sec:      3.16MB
------
Running 10s test @ http://localhost:9501/demo/hello
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     4.85ms    2.47ms  45.37ms   80.47%
    Req/Sec   424.21     88.95     1.69k    82.74%
  212146 requests in 10.10s, 31.16MB read
Requests/sec:  21005.48
Transfer/sec:      3.08MB

啟用 session 後:

wrk -c100 -t50 http://localhost:9501/demo/hello

Running 10s test @ http://localhost:9501/demo/hello
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    21.56ms    5.02ms  50.58ms   69.68%
    Req/Sec    92.94     13.73   131.00     62.73%
  46749 requests in 10.10s, 14.09MB read
Requests/sec:   4628.33
Transfer/sec:      1.39MB
------
Running 10s test @ http://localhost:9501/demo/hello
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    20.96ms    4.86ms  68.71ms   71.41%
    Req/Sec    95.69     12.63   141.00     65.36%
  48038 requests in 10.10s, 14.48MB read
Requests/sec:   4758.43
Transfer/sec:      1.43MB
------
Running 10s test @ http://localhost:9501/demo/hello
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    22.21ms    6.09ms  56.34ms   72.82%
    Req/Sec    90.30     16.62   222.00     67.81%
  45348 requests in 10.10s, 13.67MB read
Requests/sec:   4490.46
Transfer/sec:      1.35MB

Hyperf 讀寫 MySQL

Hyperf 不支援 Sqlite,故只測試MySQL。

Hyperf MySQL 寫入

wrk -c100 -t50 http://localhost:9501/demo/dbWrite
------
Running 10s test @ http://localhost:9501/demo/dbWrite
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   139.25ms   42.26ms 273.96ms   60.27%
    Req/Sec    14.89      5.88    40.00     85.85%
  7140 requests in 10.05s, 1.08MB read
Requests/sec:    710.16
Transfer/sec:    110.26KB
------
Running 10s test @ http://localhost:9501/demo/dbWrite
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   137.73ms   55.83ms 403.52ms   78.72%
    Req/Sec    15.57      6.42    40.00     82.73%
  7335 requests in 10.10s, 1.12MB read
Requests/sec:    726.56
Transfer/sec:    113.34KB
------
Running 10s test @ http://localhost:9501/demo/dbWrite
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   137.33ms   45.05ms 284.23ms   59.26%
    Req/Sec    15.15      6.36    40.00     92.15%
  7269 requests in 10.06s, 1.11MB read
Requests/sec:    722.54
Transfer/sec:    112.90KB

啟用 session 後:

wrk -c100 -t50 http://localhost:9501/demo/dbWrite

Running 10s test @ http://localhost:9501/demo/dbWrite
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   144.75ms   42.94ms 314.75ms   77.15%
    Req/Sec    14.32      5.32    30.00     57.96%
  6909 requests in 10.08s, 2.12MB read
Requests/sec:    685.49
Transfer/sec:    215.55KB
------
Running 10s test @ http://localhost:9501/demo/dbWrite
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   142.23ms   36.90ms 359.51ms   77.87%
    Req/Sec    14.44      5.12    30.00     49.45%
  7027 requests in 10.07s, 2.16MB read
Requests/sec:    697.91
Transfer/sec:    219.46KB
------
Running 10s test @ http://localhost:9501/demo/dbWrite
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   143.96ms   49.21ms 445.88ms   89.59%
    Req/Sec    14.74      5.25    30.00     45.12%
  7044 requests in 10.10s, 2.16MB read
Requests/sec:    697.63
Transfer/sec:    219.37KB

Hyperf MySQL 讀取

wrk -c100 -t50 http://localhost:9501/demo/dbRead
------
Running 10s test @ http://localhost:9501/demo/dbRead
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    49.83ms   11.10ms 104.91ms   68.28%
    Req/Sec    40.03      7.74    60.00     54.90%
  20138 requests in 10.07s, 3.86MB read
Requests/sec:   1999.25
Transfer/sec:    392.43KB
------
Running 10s test @ http://localhost:9501/demo/dbRead
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    48.28ms   10.99ms 127.69ms   69.88%
    Req/Sec    41.30      7.99    60.00     58.06%
  20787 requests in 10.09s, 3.98MB read
Requests/sec:   2060.64
Transfer/sec:    404.48KB
------
Running 10s test @ http://localhost:9501/demo/dbRead
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    51.42ms   11.68ms 130.67ms   71.05%
    Req/Sec    38.80      8.13    90.00     54.78%
  19529 requests in 10.09s, 3.74MB read
Requests/sec:   1935.29
Transfer/sec:    379.88KB

啟用session後:

wrk -c100 -t50 http://localhost:9501/demo/dbRead

Running 10s test @ http://localhost:9501/demo/dbRead
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    78.52ms   24.37ms 212.00ms   81.27%
    Req/Sec    25.36      8.09    49.00     79.84%
  12767 requests in 10.10s, 4.42MB read
Requests/sec:   1264.00
Transfer/sec:    448.08KB
------
Running 10s test @ http://localhost:9501/demo/dbRead
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    83.17ms   29.72ms 270.40ms   82.09%
    Req/Sec    24.27      8.84    49.00     74.57%
  12087 requests in 10.10s, 4.18MB read
Requests/sec:   1196.88
Transfer/sec:    424.29KB
------
Running 10s test @ http://localhost:9501/demo/dbRead
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    76.05ms   20.62ms 196.60ms   82.76%
    Req/Sec    26.43      7.74    50.00     81.25%
  13220 requests in 10.10s, 4.58MB read
Requests/sec:   1309.28
Transfer/sec:    464.13KB

Hyperf MySQL 查詢

(啟不啟用 session 差別不明顯)

wrk -c100 -t50 http://localhost:9501/demo/dbQuery

Running 10s test @ http://localhost:9501/demo/dbQuery
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   319.12ms  125.24ms 568.95ms   57.50%
    Req/Sec     7.62      4.55    20.00     72.17%
  3066 requests in 10.07s, 4.30MB read
Requests/sec:    304.52
Transfer/sec:    437.75KB
------
Running 10s test @ http://localhost:9501/demo/dbQuery
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   321.21ms  121.50ms 579.74ms   61.30%
    Req/Sec     7.50      4.30    40.00     76.54%
  3052 requests in 10.05s, 4.28MB read
Requests/sec:    303.68
Transfer/sec:    436.54KB
------
Running 10s test @ http://localhost:9501/demo/dbQuery
  50 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   324.07ms  125.57ms 683.32ms   63.97%
    Req/Sec     7.25      4.19    20.00     75.42%
  3033 requests in 10.10s, 4.26MB read
Requests/sec:    300.30
Transfer/sec:    431.67KB

資料對比(每秒處理次數)

- Laravel Laravel + Octane Hyperf 啟用 session Hyperf 不啟用 session
返回簡單字串 10 560 4600 20000
MySQL 表寫入 8 230 690 720
MySQL 單條讀取(WHERE id = 1) 8 380 1200 2000
MySQL 範圍查詢(WHERE id > 100 LIMIT 10) 8 300 300 300

總結

  • Laravel 效能真的非常差;
  • Laravel 10 使用 Octane 加速效果非常好,有 30~50 倍的提升,這個結果很振奮人心,完全能解決 Laravel 效能差的詬病;
  • Hyperf 資料寫入及主鍵等(=)查詢速度大約是 Laravel Octane 加持後的3倍,範圍查詢或排序查詢速度相等,用上資料庫後,Hyperf 的速度優勢不是特別大,如果頁面有多次資料庫查詢,速度就會拉得更接近。

現在你完全不必擔心 Laravel 10 的效能問題,效能達到瓶頸時用 Octane 加速可完美解決。

之前在這臺伺服器上測試 Octane 加速 Laravel9, PHP-8.0、Swoole 4.x,速度提升 3~8倍,而且沒測試資料庫,沒有這一次的結果好,不知道是 Octane 最佳化了還是 Swoole 進大步了。

註明

所有測試資料都是直接複製貼上過來,絕對真實0作假。
wrk 壓測前用已先用 ab 測試,確定 Failed requests 都為 0,如:

ab -n 2000 -c 50 http://127.0.0.1:8000/db-read
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 200 requests
Completed 400 requests
Completed 600 requests
Completed 800 requests
Completed 1000 requests
Completed 1200 requests
Completed 1400 requests
Completed 1600 requests
Completed 1800 requests
Completed 2000 requests
Finished 2000 requests


Server Software:        swoole-http-server
Server Hostname:        127.0.0.1
Server Port:            8000

Document Path:          /db-read
Document Length:        53 bytes

Concurrency Level:      50
Time taken for tests:   4.181 seconds
Complete requests:      2000
Failed requests:        0
Total transferred:      2302000 bytes
HTML transferred:       106000 bytes
Requests per second:    478.30 [#/sec] (mean)
Time per request:       104.536 [ms] (mean)
Time per request:       2.091 [ms] (mean, across all concurrent requests)
Transfer rate:          537.62 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.2      0       2
Processing:     7  103  36.4     96     318
Waiting:        5  103  36.4     96     318
Total:          7  103  36.3     96     318

Percentage of the requests served within a certain time (ms)
  50%     96
  66%    102
  75%    106
  80%    108
  90%    116
  95%    125
  98%    261
  99%    307
 100%    318 (longest request)
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章