官方测试地址:官方测试

最近在看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

解决 laravel-admin between datetime 假如数据库是时间戳int类型无法筛选。

laravel-admin默认的between->datetime(),查询默认是datetime类型,但是假如数据库是时间戳类型就会报错,又不想改底层文件的话可以试试加自定义筛选功能...

阅读全文

php解析英文语句,自动分解。

参考:https://www.php.net/manual/en/function.str-split.php 最近碰到一个问题,客户的英文地址太长,超出接口api字段长度,所以需要解析下语句分解发送。 ...

阅读全文

记录一个laravel-excel导出表格值为0导出excel显示空的解决方法。

最近在使用laravel-excel导出表格的时候,发现假如字段值为0的情况下,导出的excel中直接显示为空,找到一个方法解决,如下. 在laravel-excel的config配置中...

阅读全文

欢迎留言