php beego,MixPHP 2.2 / Beego 1.12 数据库查询性能对比
質(zhì)疑的點(diǎn)
本次主要質(zhì)疑的是:測(cè)試沒有數(shù)據(jù)庫(kù)查詢:大家覺得加上 db 查詢會(huì)很不一樣,但是我認(rèn)為基準(zhǔn)測(cè)試 hello world 代表天花板,還是有一些意義的,db 查詢性能方便我猜測(cè) mix 與 beego 應(yīng)該是性能接近,我現(xiàn)在還沒開始測(cè)試,等下看結(jié)果。
測(cè)試沒有序列化:本次測(cè)試,我也加上 json 序列化。
ab 測(cè)試不適合高并發(fā)測(cè)試:這一點(diǎn)我詳細(xì)列舉很多舉例,試圖說(shuō)明同樣的環(huán)境下,不同的壓力,強(qiáng)弱的數(shù)值會(huì)變,但結(jié)果是不變的,這基本屬于物理原則,既然說(shuō)服不了對(duì)方,那我就本次采用 wrk 測(cè)試吧。當(dāng)然測(cè)試沒辦法做到條件絕對(duì)一致的,但結(jié)果還是可以參考的
環(huán)境
硬件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
數(shù)據(jù)庫(kù):本機(jī)
測(cè)試命令wrk -d 120 -t 4 http://127.0.0.1:*/
連接池最大閑置:5
最大連接:50
線程數(shù)為了最大化公平,本次兩個(gè)框架都采用 1 個(gè)線程測(cè)試
MixPHP 2.2
代碼:為了公平,我把配置里的默認(rèn)中間件都移除了,之前測(cè)試沒有移除。<?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
*/
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);
}
}啟動(dòng)方式/usr/local/php-7.3.12/bin/php mix/bin/mix.php web -d進(jìn)程[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響應(yīng)內(nèi)容[nobody@~]$ curl http://127.0.0.1:9501/
[{"id":1,"name":"3"}]測(cè)試結(jié)果:測(cè)試了很多次,在 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.82MBCPU 狀態(tài):穩(wěn)定在 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) 限制了線程數(shù)。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) // 限制使用線程數(shù)
beego.Router("/index", &IndexController{}, "*:Index")
beego.Run()
}啟動(dòng)方式
為了不讓日志影響到性能,屏蔽輸出。nohup ./gobeego_linux > /dev/null 2>&1 &進(jìn)程[nobody@~]$ ps -ef| grep bee
nobody 27316 1 0 18:37 ? 00:00:00 ./gobeego_linux響應(yīng)內(nèi)容[nobody@~]$ curl http://127.0.0.1:8989/index
[{"id":1,"name":"3"}]測(cè)試結(jié)果:測(cè)試了很多次,在 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.58MBCPU 狀態(tài):穩(wěn)定在 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
修改執(zhí)行模式
有網(wǎng)友評(píng)論:beego dev 模式性能會(huì)差一些,于是重新測(cè)試修改執(zhí)行模式為 prod
測(cè)試發(fā)現(xiàn) prod 比 dev 性能高大概 2666 Requests/sec[nobody@tmp]$ cat conf/app.conf
appname = hello
httpport = 8989
runmode = prod測(cè)試結(jié)果[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
為避免不同時(shí)間的測(cè)試數(shù)據(jù)浮動(dòng),同時(shí)也再測(cè)試一下 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
移除序列化,只測(cè)試數(shù)據(jù)庫(kù)
有網(wǎng)友評(píng)論:PHP 的 json_encode 可能是性能差的原因,于是重新測(cè)試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
*/
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);
}
}
測(cè)試結(jié)果[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.70MBBeego
代碼修改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) // 限制使用線程數(shù)
beego.Router("/index", &IndexController{}, "*:Index")
beego.Run()
}
測(cè)試結(jié)果[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
總結(jié)一下
測(cè)試結(jié)果 mix 比 beego 數(shù)據(jù)庫(kù)查詢+序列化的綜合性能要低 38.3%,beego 更加優(yōu)秀,不過(guò) mix 動(dòng)態(tài)腳本語(yǔ)言能做到這樣也是蠻可以了(我只能這樣安慰自己,但也是事實(shí)),顯然我打賭輸了,愿賭服輸。框架線程數(shù)CPU數(shù)值PHP 7.3.12 + Swoole 4.4.14 + MixPHP 2.2199.3~99.7%9936.36~10080.25
Go 1.13.4 + Beego 1.12.1199.7~100.3%16306.15~16327.19開啟 beego prod 模式
更換模式后 beego 性能得到了顯著提升,測(cè)試結(jié)果 mix 比 beego 數(shù)據(jù)庫(kù)查詢+序列化的綜合性能要低 48.2%。框架線程數(shù)數(shù)值PHP 7.3.12 + Swoole 4.4.14 + MixPHP 2.219835.54
Go 1.13.4 + Beego 1.12.1118993.39移除序列化,只測(cè)試數(shù)據(jù)庫(kù)
移除序列化后測(cè)試結(jié)果變化非常小,說(shuō)明序列化在這個(gè)測(cè)試中影響很小,也就是序列化相對(duì)于 db 查詢來(lái)說(shuō),對(duì)整體性能影響比我們想象的要小很多。框架線程數(shù)數(shù)值PHP 7.3.12 + Swoole 4.4.14 + MixPHP 2.219827.51
Go 1.13.4 + Beego 1.12.1119652.80
總結(jié)
以上是生活随笔為你收集整理的php beego,MixPHP 2.2 / Beego 1.12 数据库查询性能对比的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: aes sample java,pyth
- 下一篇: 买房贷款银行会查什么 这几点要特别注意