search
HomePHP FrameworkThinkPHPCompare 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.

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. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.