官方测试地址:官方测试

最近在看tp5.1的think-swoole支持,作为替代php-fpm的一种方案,现在简单的用swoole做为http服务器和用传统的php-fpm分别测试下接口的性能。

测试机器:

  • mac:MacBook Pro (13-inch, 2017, Four Thunderbolt 3 Ports)

  • cpu:3.1 GHz Intel Core i5

  • memory: 8 GB 2133 MHz LPDDR3

  • 数据库:mysql5.7 本地服务器

  • php: php7.2

默认开启了1个worker进程,经测试接口的性能根据开启worker的进程数成倍增加。

api php代码

<?php
namespace app\api\controller;

use app\common\controller\ApiBaseController;
use think\Db;
class User extends ApiBaseController{

    public function __construct()
    {
    }

    public function get_users(){

        $users = Db::name('admins')->limit(50)->select();
        return $this->responseJson('1','success',$users);
    }

}

查询50条用户数据测试、

1.先测试传统的php-fpm,100个客户端并发1000次
$ ab -c 100 -n 1000 http://test.tp51.com/api/user/get_users

############## result
Server Software:        nginx/1.13.2
Server Hostname:        test.tp51.com
Server Port:            80

Document Path:          /api/user/get_users
Document Length:        13002 bytes

Concurrency Level:      100
Time taken for tests:   16.389 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      13171000 bytes
HTML transferred:       13002000 bytes
Requests per second:    61.02 [#/sec] (mean)
Time per request:       1638.850 [ms] (mean)
Time per request:       16.389 [ms] (mean, across all concurrent requests)
Transfer rate:          784.84 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.7      0       4
Processing:    63 1550 282.5   1620    1744
Waiting:       63 1550 282.6   1620    1744
Total:         67 1551 281.9   1620    1744

Percentage of the requests served within a certain time (ms)
  50%   1620
  66%   1628
  75%   1633
  80%   1637
  90%   1663
  95%   1705
  98%   1721
  99%   1726
 100%   1744 (longest request)
2.使用swoole做服务器
$ ab -c 100 -n 1000 http://127.0.0.1:9501/api/user/get_users

########### result

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

Document Path:          /api/user/get_users
Document Length:        13015 bytes

Concurrency Level:      100
Time taken for tests:   3.910 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      13193000 bytes
HTML transferred:       13015000 bytes
Requests per second:    255.73 [#/sec] (mean)
Time per request:       391.034 [ms] (mean)
Time per request:       3.910 [ms] (mean, across all concurrent requests)
Transfer rate:          3294.80 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.9      0       5
Processing:     5  370  68.3    381     432
Waiting:        4  370  68.3    381     432
Total:          9  371  67.5    381     432

Percentage of the requests served within a certain time (ms)
  50%    381
  66%    387
  75%    399
  80%    404
  90%    422
  95%    429
  98%    430
  99%    430
 100%    432 (longest request)
3.结果分析

性能大概提升了3~4倍

使用传统php-fpm
Requests per second:    61.02 [#/sec] (mean)
Time per request:       1638.850 [ms] (mean)
Time per request:       16.389 [ms] (mean, across all
concurrent requests)
使用swoole
Requests per second:    255.73 [#/sec] (mean)
Time per request:       391.034 [ms] (mean)
Time per request:       3.910 [ms] (mean, across all concurrent requests)
4. 使用redis测试

api代码

<?php
namespace app\api\controller;

use app\common\controller\ApiBaseController;
use think\Db;
use app\common\services\Predis;

class User extends ApiBaseController{

    public function __construct()
    {
    }

    public function get_users(){

        $predis = new Predis();
        $cache = $predis->get('users');
        if (!empty($cache)){
            return $this->responseJson('1','success',$cache);
        }
        $users = Db::name('admins')->limit(50)->select();
        $predis->set('users',$users);
        return $this->responseJson('1','success',$users);
    }
}

5.性能测试

1.php-fpm + redis
Server Software:        nginx/1.13.2
Server Hostname:        test.tp51.com
Server Port:            80

Document Path:          /api/user/get_users
Document Length:        13002 bytes

Concurrency Level:      100
Time taken for tests:   13.548 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      13171000 bytes
HTML transferred:       13002000 bytes
Requests per second:    73.81 [#/sec] (mean)
Time per request:       1354.764 [ms] (mean)
Time per request:       13.548 [ms] (mean, across all concurrent requests)
Transfer rate:          949.41 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.9      0       5
Processing:    42 1287 228.6   1334    1461
Waiting:       36 1287 228.7   1334    1461
Total:         42 1288 227.9   1334    1461

Percentage of the requests served within a certain time (ms)
  50%   1334
  66%   1352
  75%   1363
  80%   1378
  90%   1409
  95%   1426
  98%   1441
  99%   1449
 100%   1461 (longest request)
2.使用swoole+redis
Server Software:        swoole-http-server
Server Hostname:        127.0.0.1
Server Port:            9501

Document Path:          /api/user/get_users
Document Length:        13015 bytes

Concurrency Level:      100
Time taken for tests:   3.189 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      13193000 bytes
HTML transferred:       13015000 bytes
Requests per second:    313.60 [#/sec] (mean)
Time per request:       318.881 [ms] (mean)
Time per request:       3.189 [ms] (mean, across all concurrent requests)
Transfer rate:          4040.31 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.9      0       5
Processing:     4  299  54.2    315     322
Waiting:        4  299  54.2    315     322
Total:          9  299  53.3    315     322

Percentage of the requests served within a certain time (ms)
  50%    315
  66%    316
  75%    317
  80%    318
  90%    319
  95%    320
  98%    321
  99%    322
 100%    322 (longest request)

经测试,php-fpm+redis 性能提示不大,swoole+redis提升明显


php-fpm swoole
mysql RPS:61.02 RPS: 255.73
redis+mysql RPS:73.81 RPS: 318.881

PHP函数学习之——func_get_args

官方说明 说明 func_get_args ( void ) : array 获取函数参数列表的数组。 该函数可以配合 func_get_arg() 和 func_num_args() 一起使用,从而使得用户自定...

阅读全文

Mysql 分页优化

在系统中需要进行分页的时候,我们通常会使用limit 加上偏移量的方法实现,再加上合适的order by 子句,如果有对应的索引,通常效率会很不错,否则mysql需要...

阅读全文

lcobucci/jwt —— 一个轻松生成jwt token的插件

github: https://github.com/lcobucci/jwt/tree/3.2 1.安装 PHP 5.5+ (v3.2) and PHP 7.1 (v4.x) OpenSSL Extension composer require lcobucci/jwt 2. ...

阅读全文

欢迎留言