Home  >  Article  >  PHP Framework  >  Compare the efficiency of ThinkPHP5 and framework-less code under high concurrency

Compare the efficiency of ThinkPHP5 and framework-less code under high concurrency

藏色散人
藏色散人forward
2021-02-08 16:15:153312browse

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.

Compare the efficiency of ThinkPHP5 and framework-less code under high concurrencyKey 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][&#39;randnum&#39;]) {
                $level = $prizeArr[$i][&#39;level&#39;];
                break;
            }
        }

No framework code:

$sql = "select * from `lottory` where id=".$level." ";
     $res = $rnpdo->RnFetchRow($sql, array());
     $dataNum = $res[&#39;errmsg&#39;][&#39;prizenum&#39;];    //剩余数量
     $version = $res[&#39;errmsg&#39;][&#39;version&#39;];    //版本号
     
     $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[&#39;errmsg&#39;])) {
         //插入抽奖记录
         $openid = $version.&#39;-&#39;.createOpenid();
         $time = time();
         $insertSql = "insert into `lottory_list` ( `openid`, `prize`, `posttime`) values (&#39;$openid&#39;,&#39;$level&#39;,&#39;$time&#39;) ";
         $rnpdo->RnPtmQuery($insertSql, array());
         
         echo &#39;success&#39;;
     }
     else
     {
         echo &#39;fail&#39;;
     }

Use thinkPHP5 code part:

$res = Db::table("lottory")->where(&#39;id&#39;,$level)->find();
         
         $dataNum = $res[&#39;prizenum&#39;];    //剩余数量
         $version = $res[&#39;version&#39;];    //版本号
         dump($res);
         
         $result = Db::table(&#39;lottory&#39;)->where(&#39;id&#39;, $level)->where(&#39;version&#39;, $version)->update([&#39;prizenum&#39; => [&#39;exp&#39;,&#39;prizenum-1&#39;],&#39;version&#39; => [&#39;exp&#39;,&#39;version+1&#39;]]);
         dump($result);
         if($result) {
             //插入抽奖记录
             $openid = $version.&#39;-&#39;.createOpenid();
             $time = time();
             $data = [&#39;openid&#39; => $openid, &#39;prize&#39; => $level,&#39;posttime&#39;=>$time];
             Db::table(&#39;lottory_list&#39;)->insert($data);
             
             echo &#39;success&#39;;
         }
         else
         {
             echo &#39;fail&#39;;
         }

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!

Statement:
This article is reproduced at:oschina.net. If there is any infringement, please contact admin@php.cn delete