Home >PHP Framework >ThinkPHP >Compare the efficiency of ThinkPHP5 and framework-less code under high concurrency
The following tutorial column will introduce to you the comparison of the efficiency of ThinkPHP5 and frameless code under high concurrency. I hope it will be helpful to friends in need!
Test business logic: Test a lottery function and use the optimistic locking mechanism of the MySQL database to prevent over-issuance.Key code:
$prizeArr = array( array('level' => 1, 'name' => '手机', 'randnum' => 10), array('level' => 2, 'name' => '100元话费', 'randnum' => 5010), array('level' => 3, 'name' => '自拍杆', 'randnum' => 15010), array('level' => 4, 'name' => '5元红包', 'randnum' => 115010), ); $rand_num = mt_rand(1, 115010); $level = 4; for ($i = 0; $i < 4; $i++) { if ($rand_num <= $prizeArr[$i]['randnum']) { $level = $prizeArr[$i]['level']; break; } }
No framework code:
$sql = "select * from `lottory` where id=".$level." "; $res = $rnpdo->RnFetchRow($sql, array()); $dataNum = $res['errmsg']['prizenum']; //剩余数量 $version = $res['errmsg']['version']; //版本号 $updatesql = "update `lottory` set prizenum=prizenum-1,version=version+1 where id=".$level." and version=".$version." "; $updateres = $rnpdo->RnExec($updatesql, array()); //print_r($res); if(!empty($updateres['errmsg'])) { //插入抽奖记录 $openid = $version.'-'.createOpenid(); $time = time(); $insertSql = "insert into `lottory_list` ( `openid`, `prize`, `posttime`) values ('$openid','$level','$time') "; $rnpdo->RnPtmQuery($insertSql, array()); echo 'success'; } else { echo 'fail'; }
Use thinkPHP5 code part:
$res = Db::table("lottory")->where('id',$level)->find(); $dataNum = $res['prizenum']; //剩余数量 $version = $res['version']; //版本号 dump($res); $result = Db::table('lottory')->where('id', $level)->where('version', $version)->update(['prizenum' => ['exp','prizenum-1'],'version' => ['exp','version+1']]); dump($result); if($result) { //插入抽奖记录 $openid = $version.'-'.createOpenid(); $time = time(); $data = ['openid' => $openid, 'prize' => $level,'posttime'=>$time]; Db::table('lottory_list')->insert($data); echo 'success'; } else { echo 'fail'; }
Use ab to test the performance under high concurrency:
ab -c 1000 -n 10000 http://localhost/lottory.php ab -c 1000 -n 10000 http://localhost/index.php?s=index/index/hello
Tested on the same server, the web server uses nginx, in which TP5 cancels log writing (it was not removed the first time, QPS was lower).
The key data:
Frameless QPS: Requests per second: 972.21 [#/sec] (mean)
thinkPHP5: Requests per second: 206.92 [#/sec ] (mean)
The same business logic without framework is 4.7 times that of tp5.
I don’t know if there is anything else that is not optimized in TP5. Neither page code uses caching.
Do you have a deep understanding of tp5 and can you provide some guidance? Are there any other areas that need to be optimized?
The above is the detailed content of Compare the efficiency of ThinkPHP5 and framework-less code under high concurrency. For more information, please follow other related articles on the PHP Chinese website!