ÔÚÃëɱ£¬ÇÀ¹ºµÈ²¢·¢³¡¾°Ï£¬¿ÉÄÜ»á³öÏÖ³¬ÂôµÄÏÖÏó£¬ÔÚ PHP ÓïÑÔÖв¢Ã»ÓÐÔÉúÌṩ²¢·¢µÄ½â¾ö·½°¸£¬Òò´Ë¾ÍÐèÒª½èÖúÆäËû·½Ê½À´ÊµÏÖ²¢·¢¿ØÖÆ£¬Æäʵ·½°¸ÓкܶàÖÖ£¬½ñÌìÖ»ÊǾٸöÀõ×ÓÅ×שÒýÓñ£¬ÓÐÆäËû¸üºÃµÄ·½°¸Äã¿ÉÒÔ×Ô¼ºÈ¥ÍæÒ»Íæ¾ÍºÃÁË¡£
Áгö³£¼ûµÄ3¸ö½â¾ö·½°¸ÓУº
ʹÓöÓÁУ¬¶îÍâÆðÒ»¸ö½ø³Ì´¦Àí¶ÓÁУ¬²¢·¢ÇëÇ󶼷ŵ½¶ÓÁÐÖУ¬ÓɶîÍâ½ø³Ì´®Ðд¦Àí£¬²¢·¢ÎÊÌâ¾Í²»´æÔÚÁË£¬µ«ÊÇÒª¶îÍâ½ø³ÌÖ§³ÖÒÔ¼°´¦ÀíÑÓ³ÙÑÏÖØ£¬±¾ÎIJ»ÏȲ»ÌÖÂÛÕâÖÖ·½·¨¡£
ÀûÓÃÊý¾Ý¿âÊÂÎñÌØÕ÷£¬×öÔ×Ó¸üУ¬´Ë·½·¨ÐèÒªÒÀÀµÊý¾Ý¿âµÄÊÂÎñÌØÐÔ¡£
½èÖúÎļþÅÅËûËø£¬ÔÚ´¦Àíϵ¥ÇëÇóµÄʱºò£¬Óà flock Ëø¶¨Ò»¸öÎļþ£¬³É¹¦Äõ½ËøµÄ²ÅÄÜ´¦Àí¶©µ¥¡£
redis ÊÂÎñÊÇÔ×Ó²Ù×÷£¬¿ÉÒÔ±£Ö¤¶©µ¥´¦ÀíµÄ¹ý³ÌÖÐÊý¾ÝûÓб»ÆäËü²¢·¢µÄ½ø³ÌÐ޸ġ£
ʾÀý´úÂ룺
<?php $http = new swoole_http_server("0.0.0.0", 9509); // ¼àÌý 9509 $http->set(array( 'reactor_num' => 2, //reactor thread num 'worker_num' => 4 //worker process num )); $http->on('request', function (swoole_http_request $request, swoole_http_response $response) { $uniqid = uniqid('uid-', TRUE); // Ä£ÄâΨһÓû§ID $redis = new Redis(); $redis->connect('127.0.0.1', 6379); // Á¬½Ó redis $redis->watch('rest_count'); // ¼à²â rest_count ÊÇ·ñ±»ÆäËüµÄ½ø³Ì¸ü¸Ä $rest_count = intval($redis->get("rest_count")); // Ä£ÄâΨһ¶©µ¥ID if ($rest_count > 0){ $value = "{$rest_count}-{$uniqid}"; // ±íʾµ±Ç°¶©µ¥£¬±»µ±Ç°Óû§ÇÀµ½ÁË // do something ... Ö÷ÒªÊÇÄ£ÄâÓû§ÇÀµ½µ¥ºó¿ÉÄÜÒª½øÐеÄһЩÃܼ¯ÔËËã $rand = rand(100, 1000000); $sum = 0; for ($i = 0; $i < $rand; $i++) {$sum += $i;} // redis ÊÂÎñ $redis->multi(); $redis->lPush('uniqids', $value); $redis->decr('rest_count'); $replies = $redis->exec(); // Ö´ÐÐÒÔÉÏ redis ÊÂÎñ // Èç¹û rest_count µÄÖµ±»ÆäËüµÄ²¢·¢½ø³Ì¸ü¸ÄÁË£¬ÒÔÉÏÊÂÎñ½«»Ø¹ö if (!$replies) { echo "¶©µ¥ {$value} »Ø¹ö" . PHP_EOL; } } $redis->unwatch(); }); $http->start();
ʹÓà ab ²âÊÔ
$ ab -t 20 -c 10 http://192.168.1.104:9509/
×èÈûģʽÏ£¬Èç¹û½ø³ÌÔÚ»ñÈ¡ÎļþÅÅËûËøʱ£¬ÆäËü½ø³ÌÕýÔÚÕ¼ÓÃËøµÄ»°£¬´Ë½ø³Ì»á¹ÒÆðµÈ´ýÆäËü½ø³ÌÊÍ·ÅËøºó£¬²¢×Ô¼º»ñÈ¡µ½Ëøºó£¬ÔÙÍùÏÂÖ´ÐС£
ʾÀý´úÂ룺
<?php $http = new swoole_http_server("0.0.0.0", 9510); $http->set(array( 'reactor_num' => 2, //reactor thread num 'worker_num' => 4 //worker process num )); $http->on('request', function (swoole_http_request $request, swoole_http_response $response) { $uniqid = uniqid('uid-', TRUE); $redis = new Redis(); $redis->connect('127.0.0.1', 6379); $fp = fopen("lock.txt", "w+"); // ×èÈû(µÈ´ý)ģʽ£¬ ҪȡµÃ¶ÀÕ¼Ëø¶¨£¨Ð´ÈëµÄ³ÌÐò£© if (flock($fp,LOCK_EX)) { //Ëø¶¨µ±Ç°Ö¸Õë // ³É¹¦È¡µÃËøºó£¬·ÅÐÄ´¦Àí¶©µ¥ $rest_count = intval($redis->get("rest_count")); $value = "{$rest_count}-{$uniqid}"; if ($rest_count > 0) { // do something ... $rand = rand(100, 1000000); $sum = 0; for ($i = 0; $i < $rand; $i++) {$sum += $i;} $redis->lPush('uniqids', $value); $redis->decr('rest_count'); } // ¶©µ¥´¦ÀíÍê³Éºó£¬ÔÙÊÍ·ÅËø flock($fp, LOCK_UN); } fclose($fp); }); $http->start();
ʹÓà ab ²âÊÔ
$ ab -t 20 -c 10 http://192.168.1.104:9510/
·Ç×èÈûģʽÏ£¬Èç¹û½ø³ÌÔÚ»ñÈ¡ÎļþÅÅËûËøʱ£¬ÆäËü½ø³ÌÕýÔÚÕ¼ÓÃËøµÄ»°£¬´Ë½ø³Ì»áÂíÉÏÅжϻñÈ¡Ëøʧ°Ü£¬²¢ÇÒ¼ÌÐøÍùÏÂÖ´ÐС£\
ʾÀý´úÂ룺
<?php $http = new swoole_http_server("0.0.0.0", 9511); $http->set(array( 'reactor_num' => 2, //reactor thread num 'worker_num' => 4 //worker process num )); $http->on('request', function (swoole_http_request $request, swoole_http_response $response) { $uniqid = uniqid('uid-', TRUE); $redis = new Redis(); $redis->connect('127.0.0.1', 6379); $fp = fopen("lock.txt", "w+"); // ·Ç×èÈûģʽ£¬ Èç¹û²»Ï£Íû flock() ÔÚËø¶¨Ê±¶ÂÈû£¬Ôò¸ø lock ¼ÓÉÏ LOCK_NB if(flock($fp,LOCK_EX | LOCK_NB)) //Ëø¶¨µ±Ç°Ö¸Õë { // ³É¹¦È¡µÃËøºó£¬·ÅÐÄ´¦Àí¶©µ¥ $rest_count = intval($redis->get("rest_count")); $value = "{$rest_count}-{$uniqid}"; if($rest_count > 0){ // do something ... $rand = rand(100, 1000000); $sum=0; for ($i=0;$i<$rand;$i++){ $sum+=$i; } $redis->lPush('uniqids', $value); $redis->decr('rest_count'); } // ¶©µ¥´¦ÀíÍê³Éºó£¬ÔÙÊÍ·ÅËø flock($fp,LOCK_UN); } else { // Èç¹û»ñÈ¡Ëøʧ°Ü£¬ÂíÉϽøÈëÕâÀïÖ´ÐÐ echo "{$uniqid} - ϵͳ·±Ã¦£¬ÇëÉÔºóÔÙÊÔ".PHP_EOL; } fclose($fp); }); $http->start();
ʹÓà ab ²âÊÔ
$ ab -t 20 -c 10 http://192.168.1.104:9511/
×îºó¸ø³öÈýÖÖ´¦Àí·½Ê½µÄ²âÊÔ½á¹û±È½Ï
redis ÊÂÎñ·½Ê½£º
Concurrency Level: 10 Time taken for tests: 20.005 seconds Complete requests: 17537 Failed requests: 0 Total transferred: 2578380 bytes HTML transferred: 0 bytes Requests per second: 876.62 [#/sec] (mean) Time per request: 11.407 [ms] (mean) Time per request: 1.141 [ms] (mean, across all concurrent requests) Transfer rate: 125.86 [Kbytes/sec] received
ÎļþÅÅËûËø£¨×èÈûģʽ£©£º
Concurrency Level: 10 Time taken for tests: 20.003 seconds Complete requests: 8205 Failed requests: 0 Total transferred: 1206282 bytes HTML transferred: 0 bytes Requests per second: 410.19 [#/sec] (mean) Time per request: 24.379 [ms] (mean) Time per request: 2.438 [ms] (mean, across all concurrent requests) Transfer rate: 58.89 [Kbytes/sec] received
ÎļþÅÅËûËø£¨·Ç×èÈûģʽ£©£º
Concurrency Level: 10 Time taken for tests: 20.002 seconds Complete requests: 8616 Failed requests: 0 Total transferred: 1266846 bytes HTML transferred: 0 bytes Requests per second: 430.77 [#/sec] (mean) Time per request: 23.214 [ms] (mean) Time per request: 2.321 [ms] (mean, across all concurrent requests) Transfer rate: 61.85 [Kbytes/sec] received
¾²âÊÔ½á¹û¶Ô±È£¬redis ÊÂÎñ·½Ê½ÓÅÓÚÎļþÅÅËûËø·½Ê½£¬¶øÎļþÅÅËûËø·½Ê½ÖУ¬·Ç×èÈûģʽÓÅÓÚ×èÈûģʽ¡£
µ½´ËÕâƪ¹ØÓÚPHP²¢·¢³¡¾°µÄÈýÖÖ½â¾ö·½°¸´úÂëʵÀýµÄÎÄÕ¾ͽéÉܵ½ÕâÁË,¸ü¶àÏà¹ØPHP²¢·¢³¡¾°µÄÈýÖÖ½â¾ö·½°¸ÄÚÈÝÇëËÑË÷Õ¾³¤¼¼ÊõÒÔÇ°µÄÎÄÕ»ò¼ÌÐøä¯ÀÀÏÂÃæµÄÏà¹ØÎÄÕÂÏ£Íû´ó¼ÒÒÔºó¶à¶àÖ§³ÖÕ¾³¤¼¼Êõ£¡
ÔÚFlash Player 10.1¼°ÒÔÉÏ°æ±¾ÖУ¬adobeÐÂÔöÁËÈ«¾Ö´íÎó´¦Àí³ÌÐòUncaughtErrorEv...
±¾ÎÄתÔØ×Ô΢ÐŹ«Öںš¸SHµÄÈ«Õ»±Ê¼Ç¡¹£¬×÷ÕßSH¡£×ªÔر¾ÎÄÇëÁªÏµSHµÄÈ«Õ»±Ê¼Ç¹«...
ÎÊÌ⣺ÎÒÃÇÔÚ×öflexµÄ¿ª·¢ÖУ¬Èç¹ûÓõ½±ðÈ˴ºÃµÄ¿ò¼Ü£¬¶ø±ðÈ˵ÄserverÃû³ÆÍù...
´ó¼ÒºÃ£¬ÎÒÊÇ¿ñÁľý¡£ ½ñÌìÀ´ÁÄÒ»ÁÄ Mysql »º´æ³ØÔÀí¡£ Ìá¸Ù¸½ÉÏ£¬»°²»¶à˵£¬Ö±...
CKeditor£¬ÒÔÇ°½ÐFCKeditor£¬ÒѾʹÓùýºÃ¶àÄêÁË£¬¹¦ÄÜ×ÔȻûµÄ˵¡£×î½üÉý¼¶µ½3....
Ç°ÑÔ ÏîÄ¿¿ª·¢Öв»¹ÜÊÇǰ̨»¹ÊǺǫ́¶¼»áÓöµ½·³È˵Änull£¬Êý¾Ý¿â±íÖÐ×Ö¶ÎÔÊÐí¿ÕÖµ...
idea¹Ù·½ÍÆËÍÁË2020.2.4°æ±¾µÄ¸üУ¬ÄÇô´ó¼Ò×î¹ØÐĵÄÎÊÌâÀ´ÁË£¬Ö®Ç°¼¤»îidea202...
±¾ÎÄתÔØ×Ô΢ÐŹ«Öںš¸SQLÊý¾Ý¿â¡¹£¬×÷Õßؼƽ·²ÊÀ½ç ¡£×ªÔر¾ÎÄÇëÁªÏµ¿ª·¢¹«ÖÚ...
À´Ô´£ºDeepenStudy ©¶´Îļþ£ºjs.asp % Dimoblog setoblog=newclass_sys oblog.a...
±¾ÎÄʵÀý½²ÊöÁËAJAX+ServletʵÏÖµÄÊý¾Ý´¦ÀíÏÔʾ¹¦ÄÜ¡£·ÖÏí¸ø´ó¼Ò¹©´ó¼Ò²Î¿¼£¬¾ß...