MixPHP 2.2 / Beego 1.12 database query performance comparison

Yesterday, I shared the performance comparison of PHP7.3+Swoole4.4 / Go1.13 / MixPHP2.2 / Beego1.12 on v2ex , which was questioned by many netizens.

point of doubt

The main questions this time are:

  • There is no database query in the test : everyone thinks that adding db query will be very different, but I think the benchmark test hello world represents the ceiling, which still has some meaning. The performance of db query is convenient. I guess that mix and beego should be close in performance, I haven't yet Start the test and wait to see the results.
  • The test is not serialized : In this test, I also added json serialization.
  • The ab test is not suitable for high concurrency testing : I have listed many examples in detail, trying to illustrate that under the same environment, different pressures, the value of strength and weakness will change, but the result will remain the same, which is basically a physical principle, since it cannot be persuaded The other party, then I will use the wrk test this time.

Of course, there is no way to test the conditions to be absolutely consistent, but the results can still be used for reference.

environment

hardware

  • 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

database:

  • native

test command

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

connection pool

  • Max idle: 5
  • Maximum connections: 50

Threads

  • In order to maximize fairness, this time the two frameworks use 1a thread test

MixPHP 2.2

Code: To be fair, I removed the default middleware in the configuration, which was not removed in the previous test.

<?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 <[email protected]>
 */
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);
    }

}
  • Start method
/usr/local/php-7.3.12/bin/php mix/bin/mix.php web -d
  • process
[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
  • response content
[nobody@~]$ curl http://127.0.0.1:9501/
[{"id":1,"name":"3"}]
  • Test results: Tested many times, 9936.36~10080.25around
[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 Status: Stable 99.3~99.7%around .
  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

Code: Use runtime.GOMAXPROCS(1)to limit the number of threads.

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()
}
  • Start method

To prevent the log from affecting performance, mask the output.

nohup ./gobeego_linux > /dev/null 2>&1 &
  • process
[nobody@~]$ ps -ef| grep bee
nobody   27316     1  0 18:37 ?        00:00:00 ./gobeego_linux
  • response content
[nobody@~]$ curl http://127.0.0.1:8989/index
[{"id":1,"name":"3"}]
  • Test results: Tested many times, 16306.15~16327.19around
[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 Status: Stable 99.7~100.3%around .
  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

Modify execution mode

Some netizens commented: the performance of beego dev mode will be worse, so re-test

  • Modify the execution mode toprod

The test found prodthat the devperformance is probably higher than 2666Requests/sec

[nobody@tmp]$ cat conf/app.conf
appname = hello
httpport = 8989
runmode = prod
  • Test Results
[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

In order to avoid the fluctuation of test data at different times, test MixPHP again at the same time

[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

Remove serialization, only test database

Some netizens commented: PHP's json_encode may be the reason for poor performance, so re-test

  • MixPHP

code modification

<?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 <[email protected]>
 */
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);
    }

}

Test Results

[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

code modification

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()
}

Test Results

[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

in conclusion

The test result mix is ​​lower than the comprehensive performance of beego database query + serialization 38.3%, and beego is better, but the mix dynamic scripting language can do this quite well (I can only comfort myself, but it is also true), obviously I bet to lose Yes, willing to admit defeat.

frame Threads CPU Numerical value
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
  • Enable beego prod mode

After changing the mode, the performance of beego has been significantly improved, and the test result mix is ​​lower than the comprehensive performance of beego database query + serialization 48.2%.

frame Threads Numerical value
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
  • Remove serialization, only test database

After removing serialization, the test results change very little, indicating that serialization has little impact in this test, that is, serialization has a much smaller impact on overall performance than db query than we thought.

frame Threads Numerical value
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
{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324135926&siteId=291194637