MixPHP 2.2 / Beego 1.12 数据库查询性能对比

昨天在 v2ex 分享了一下 PHP7.3+Swoole4.4 / Go1.13 / MixPHP2.2 / Beego1.12 性能对比 被很多网友质疑,本不想引起争端,但一时冲动和网友打赌了 10 块钱。

质疑的点

本次主要质疑的是:

  • 测试没有数据库查询:大家觉得加上 db 查询会很不一样,但是我认为基准测试 hello world 代表天花板,还是有一些意义的,db 查询性能方便我猜测 mixbeego 应该是性能接近,我现在还没开始测试,等下看结果。
  • 测试没有序列化:本次测试,我也加上 json 序列化。
  • ab 测试不适合高并发测试:这一点我详细列举很多举例,试图说明同样的环境下,不同的压力,强弱的数值会变,但结果是不变的,这基本属于物理原则,既然说服不了对方,那我就本次采用 wrk 测试吧。

当然测试没办法做到条件绝对一致的,但结果还是可以参考的

环境

硬件

  • CPU: Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz
  • CPU(s): 12
  • Mem: 15G
  • Linux version 3.10.0-957.10.1.el7.x86_64

数据库:

  • 本机

测试命令

wrk -d 120 -t 4 http://127.0.0.1:*/

连接池

  • 最大闲置:5
  • 最大连接:50

线程数

  • 为了最大化公平,本次两个框架都采用 1 个线程测试

MixPHP 2.2

代码:为了公平,我把配置里的默认中间件都移除了,之前测试没有移除。

<?php

namespace App\Web\Controllers;

use App\Common\Helpers\ResponseHelper;
use Mix\Http\Message\ServerRequest;
use Mix\Http\Message\Response;

/**
 * Class IndexController
 * @package App\Web\Controllers
 * @author liu,jian <coder.keda@gmail.com>
 */
class IndexController
{

    /**
     * Index
     * @param ServerRequest $request
     * @param Response $response
     * @return Response
     */
    public function index(ServerRequest $request, Response $response)
    {

       /** @var Database $db */
        $db     = context()->get('database');
        $result = $db->prepare('select * from test limit 1')->queryAll();

        $content = json_encode($result);

        return ResponseHelper::html($response, $content);
    }

}
  • 启动方式
/usr/local/php-7.3.12/bin/php mix/bin/mix.php web -d
  • 进程
[nobody@~]$ ps -ef | grep mix.php
nobody   25972     1  0 18:36 ?        00:00:00 /usr/local/php-7.3.12/bin/php mix/bin/mix.php web -d
  • 响应内容
[nobody@~]$ curl http://127.0.0.1:9501/
[{"id":1,"name":"3"}]
  • 测试结果:测试了很多次,在 9936.36~10080.25 左右
[nobody@~]$ wrk -d 120 -t 4 http://127.0.0.1:9501/
Running 2m test @ http://127.0.0.1:9501/
  4 threads and 10 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   806.18us  501.04us  51.95ms   97.58%
    Req/Sec     2.53k   245.91     5.92k    79.28%
  1210639 requests in 2.00m, 218.21MB read
Requests/sec:  10080.25
Transfer/sec:      1.82MB
  • CPU 状态:稳定在 99.3~99.7% 左右。
  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
25972 nobody    20   0 1166992  12368   4064 R  99.7  0.1   2:41.11 php

Beego 1.12

代码:使用 runtime.GOMAXPROCS(1) 限制了线程数。

package main

import (
    "encoding/json"
    "github.com/astaxie/beego"
    "github.com/astaxie/beego/orm"
    _ "github.com/go-sql-driver/mysql"
    _ "hello/routers"
    "runtime"
)

type Test struct {
    Id   int    `orm:"column(id)"json:"id"`
    Name string `orm:"column(name)"json:"name"`
}

func init() {
    orm.RegisterModel(new(Test))
    orm.RegisterDriver("mysql", orm.DRMySQL)
    maxIdle := 5
    maxConn := 50
    orm.RegisterDataBase("default", "mysql", "*****@tcp(***:3306)/test?charset=utf8&loc=Asia%2FShanghai&parseTime=true", maxIdle, maxConn)
}

type IndexController struct {
    beego.Controller
}

func (c *IndexController) Index() {
    o := orm.NewOrm();
    var row []*Test
    o.Raw("select * from test limit 1").QueryRows(&row);

    js, _ := json.Marshal(row)

    c.Ctx.Output.Body(js)
}

func main() {
    runtime.GOMAXPROCS(1) // 限制使用线程数
    beego.Router("/index", &IndexController{}, "*:Index")
    beego.Run()
}
  • 启动方式

为了不让日志影响到性能,屏蔽输出。

nohup ./gobeego_linux > /dev/null 2>&1 &
  • 进程
[nobody@~]$ ps -ef| grep bee
nobody   27316     1  0 18:37 ?        00:00:00 ./gobeego_linux
  • 响应内容
[nobody@~]$ curl http://127.0.0.1:8989/index
[{"id":1,"name":"3"}]
  • 测试结果:测试了很多次,在 16306.15~16327.19 左右
[nobody@~]$ wrk -d 120 -t 4 http://127.0.0.1:8989/index
Running 2m test @ http://127.0.0.1:8989/index
  4 threads and 10 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   521.18us  427.56us  29.46ms   92.23%
    Req/Sec     4.10k   260.69     4.74k    79.96%
  1959389 requests in 2.00m, 310.19MB read
Requests/sec:  16327.19
Transfer/sec:      2.58MB
  • CPU 状态:稳定在 99.7~100.3% 左右。
  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
27316 nobody    20   0  114736  10660   5008 S 100.3  0.1   0:39.87 gobeego_linux

修改执行模式

有网友评论:beego dev 模式性能会差一些,于是重新测试

  • 修改执行模式为 prod

测试发现 proddev 性能高大概 2666 Requests/sec

[nobody@tmp]$ cat conf/app.conf
appname = hello
httpport = 8989
runmode = prod
  • 测试结果
[nobody@~]$ wrk -d 120 -t 4 http://127.0.0.1:8989/index
Running 2m test @ http://127.0.0.1:8989/index
  4 threads and 10 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   453.55us  427.00us  29.69ms   93.22%
    Req/Sec     4.77k   328.51     5.70k    82.71%
  2279372 requests in 2.00m, 299.98MB read
Requests/sec:  18993.39
Transfer/sec:      2.50MB

为避免不同时间的测试数据浮动,同时也再测试一下 MixPHP

[nobody@tmp]$ wrk -d 120 -t 4 http://127.0.0.1:9501/
Running 2m test @ http://127.0.0.1:9501/
  4 threads and 10 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   821.18us  347.98us  20.65ms   89.92%
    Req/Sec     2.47k   227.03     5.80k    81.13%
  1181243 requests in 2.00m, 212.91MB read
Requests/sec:   9835.54
Transfer/sec:      1.77MB

移除序列化,只测试数据库

有网友评论:PHP 的 json_encode 可能是性能差的原因,于是重新测试

  • MixPHP

代码修改

<?php

namespace App\Web\Controllers;

use App\Common\Helpers\ResponseHelper;
use Mix\Http\Message\ServerRequest;
use Mix\Http\Message\Response;

/**
 * Class IndexController
 * @package App\Web\Controllers
 * @author liu,jian <coder.keda@gmail.com>
 */
class IndexController
{

    /**
     * Index
     * @param ServerRequest $request
     * @param Response $response
     * @return Response
     */
    public function index(ServerRequest $request, Response $response)
    {

       /** @var Database $db */
        $db     = context()->get('database');
        $result = $db->prepare('select * from test limit 1')->queryAll();

        $content = 'hello, world!'; // 不序列化

        return ResponseHelper::html($response, $content);
    }

}

测试结果

[nobody@tmp]$ wrk -d 120 -t 4 http://127.0.0.1:9501/
Running 2m test @ http://127.0.0.1:9501/
  4 threads and 10 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   819.22us  309.68us  23.65ms   87.91%
    Req/Sec     2.47k   221.66     3.07k    76.21%
  1179327 requests in 2.00m, 203.57MB read
Requests/sec:   9827.51
Transfer/sec:      1.70MB
  • Beego

代码修改

package main

import (
    "encoding/json"
    "github.com/astaxie/beego"
    "github.com/astaxie/beego/orm"
    _ "github.com/go-sql-driver/mysql"
    _ "hello/routers"
    "runtime"
)

type Test struct {
    Id   int    `orm:"column(id)"json:"id"`
    Name string `orm:"column(name)"json:"name"`
}

func init() {
    orm.RegisterModel(new(Test))
    orm.RegisterDriver("mysql", orm.DRMySQL)
    maxIdle := 5
    maxConn := 50
    orm.RegisterDataBase("default", "mysql", "*****@tcp(***:3306)/test?charset=utf8&loc=Asia%2FShanghai&parseTime=true", maxIdle, maxConn)
}

type IndexController struct {
    beego.Controller
}

func (c *IndexController) Index() {
    o := orm.NewOrm();
    var row []*Test
    o.Raw("select * from test limit 1").QueryRows(&row);

    js := []byte("hello, world!")  // 不序列化

    c.Ctx.Output.Body(js)
}

func main() {
    runtime.GOMAXPROCS(1) // 限制使用线程数
    beego.Router("/index", &IndexController{}, "*:Index")
    beego.Run()
}

测试结果

[nobody@tmp]$ wrk -d 120 -t 4 http://127.0.0.1:8989/index
Running 2m test @ http://127.0.0.1:8989/index
  4 threads and 10 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   436.00us  363.84us  24.06ms   92.74%
    Req/Sec     4.94k   319.79     5.99k    79.62%
  2358720 requests in 2.00m, 292.43MB read
Requests/sec:  19652.80
Transfer/sec:      2.44MB

总结一下

测试结果 mix 比 beego 数据库查询+序列化的综合性能要低 38.3%,beego 更加优秀,不过 mix 动态脚本语言能做到这样也是蛮可以了(我只能这样安慰自己,但也是事实),显然我打赌输了,愿赌服输。

框架 线程数 CPU 数值
PHP 7.3.12 + Swoole 4.4.14 + MixPHP 2.2 1 99.3~99.7% 9936.36~10080.25
Go 1.13.4 + Beego 1.12.1 1 99.7~100.3% 16306.15~16327.19
  • 开启 beego prod 模式

更换模式后 beego 性能得到了显著提升,测试结果 mix 比 beego 数据库查询+序列化的综合性能要低 48.2%

框架 线程数 数值
PHP 7.3.12 + Swoole 4.4.14 + MixPHP 2.2 1 9835.54
Go 1.13.4 + Beego 1.12.1 1 18993.39
  • 移除序列化,只测试数据库

移除序列化后测试结果变化非常小,说明序列化在这个测试中影响很小,也就是序列化相对于 db 查询来说,对整体性能影响比我们想象的要小很多。

框架 线程数 数值
PHP 7.3.12 + Swoole 4.4.14 + MixPHP 2.2 1 9827.51
Go 1.13.4 + Beego 1.12.1 1 19652.80
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,928评论 6 509
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,748评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,282评论 0 357
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,065评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,101评论 6 395
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,855评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,521评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,414评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,931评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,053评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,191评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,873评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,529评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,074评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,188评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,491评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,173评论 2 357