Home > PHP Framework > ThinkPHP > body text

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

藏色散人
Release: 2021-02-08 16:15:15
forward
3103 people have browsed it

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]['randnum']) {
                $level = $prizeArr[$i]['level'];
                break;
            }
        }
Copy after login

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';
     }
Copy after login

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';
         }
Copy after login

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
Copy after login

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!

Related labels:
source:oschina.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Latest issues
Popular Tutorials
More>
Latest downloads
More>
web effects
Website source code
Website materials
Front end template
About us Disclaimer Sitemap
PHP Chinese website:Public welfare online PHP training,Help PHP learners grow quickly!