should give the service number to the service number to do a single second kill function, the need to do a second kill test, want to try PHP multithreading, the simulation of the snatching single function.

said:

module seckill ideas under normal circumstances,

1 launched

user operation seckill seckill request
2,
3, enter the queue seckill the random time delay of 1-2 seconds (the query is seckill results in shunt it)
4,
5, order is generated results returned

below

simulation code:

 < seckill; PHP? Set_time_limit (0); / * * * * class Threadrun thread mission extends Thread {public $url; public $data; public $params; public function __construct ($url, $params=[]) {url = $this-> $url; $this-> params = $params;} public (function run (($url) {if = $this-> url ='goods_id')) {$params [1, =>'activity_id' => 1,'user_id '=> isset ($this-> params['user_id'])? $this-> params['user_id']: $this-> getCurrentThreadId ($startTime = microtime),]; (true); $this-> data = ['id' $params['user_id'],'result'=> => model_http_curl_get ($url, $params),'time' => microtime (true) -$startTime,'now'(true => microtime)];}}}, / * * * * function model_thread_result_get multithreading execution ($urls_array) {foreach ($urls_array as $key => $value) {$threadPool[$key] = new Threadrun ($value[URL, ['user_id'=> $value['user_id']]; $threadPool[$key]-> (start) foreach ($threadPool as);} $thread_key {while (=> $thread_value) $threadPool[$thread_key]-& GT (isRunning)) {; Usleep (10) ($threadPool[$thread_key]-> if;} (join)) {$variable_data[$thread_key] = $threadPool[$thread_key]-> data return $variable_data;}};} / * * * * function model_http_curl_get to send HTTP requests ($url, $data=[], $userAgent = "") {$userAgent = $userAgent? $userAgent:'Mozilla/4.0 (compatible; MSIE 7; Windows NT 5.2 '); $curl = curl_init (); curl_setopt ($curl, CURLOPT_URL, $url); curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($curl, CURLOPT_TIMEOUT, 5); curl_setopt ($curl, CURLOPT_USERAGENT, $userAgent); curl_setopt ($curl, CURLOPT_POST, true); if (! Empty ($data) {curl_setopt ($curl), CURLOPT_POSTFIELDS, $data);} $result = curl_exec ($curl); curl_close ($curl); return $res Ult; / * * * *} print variable friendly @param $val / function dump ($val) {echo'< pre> '; var_dump ($val); echo'< /pre>';} / * * * * * @param $msg @param log string $logPath / function writeLog ($msg, $logPath='') {if (empty ($logPath)) {$logPath = date ('Y_m_d'.'.log');} if (! File_exists ($logPath)) {$fp = fopen ($logPath,'w'); Fclose ($fp);} error_log ($msg.PHP_EOL, 3, $logPath);} / * * * * * $result @param log information @param $timeDiff * @return bool|string * / function createLog ($result, $timeDiff) {if (empty ($result) ||! Is_array ($result)) {return} false; $succeed = 0; $fail = 0; foreach ($result as $v) {$times[] = $v['time']; $v['result'] = = = false? $fail++: $totalTime = $succeed++;} array_sum ($times); $maxTime = max; $minTime = ($times) min ($times); $sum = count ($times); $avgTime = $totalTime/$sum; $segment = str_repeat (T, 100); $flag = $segment. PHP_EOL; $flag = total execution time: '' $timeDiff. PHP_EOL; the maximum execution time $flag = ':'. $maxTime. PHP_EOL; $flag = 'minimum execution time:'. $minTime. PHP_EOL; $flag = '$avgTime': the average request time. The number of flag requests. PHP_EOL; $= ':'. $sum. PHP_EOL; $flag = 'request success number:'. $succeed. PHP_EOL; $flag = '$fail': the number of request failed. PHP_EOL. $segment. PHP_EOL; $flag = return; $flag;} / * * * * function insertList (initiated seckill request $urls, $logPath='' ) {$t = microtime (true); $result = model_thread_result_get; $e = ($urls) microtime (true); $timeDiff = $e-$t; echo: the total execution time. $timeDiff. PHP_EOL; foreach ($result as $v) {$msg = '' $v['id']. '[users]. Seckill goods, return results'. $v['result'].' when '$v['time']'. []. The second time ['.$v['now'].']'; writeLog ($msg, $logPath);} $logStr = createLog ($result, $timeDiff); writeLog ($logStr, $logPath); return $result;} / / for request initiated seckill ($i=0; $i 1000; < $i++) {$urls_array[] = array ("name =>"; "Baidu", "URL" => "http://***.***.com/seckill/shopping/listinsert");} $list = insertList ($urls_array, inset.log''./); / / $urls_array query results by seckill = []; foreach ($list as $v) {if ($v['result'] = false) {continue}; $urls_array[] = array ("name =>"; "Baidu", "URL" => "http://***.***.com/seckill/shopping/query",'user_id'=> $v['id'], insertList);} ($urls_array,'./query.log'); 

(machine performance test code the development of

"p> order code machine performance (tester):

simulation system test results: 1000 concurrent, single 300+ orders per second server, no pressure.
can't stand the test machine, and the CPU soared 100%. Apache crashes occasionally.

does not know whether it is the support of PHP multithreading and Windows environment, or the problem of PHP multithreading itself, and the 1000 thread can't run. Multi thread is quite Python and C run.

is the whole content of this article, I hope to help you, and hope that you can support a lot of scripting home.

This paper fixed link:http://www.script-home.com/php-multi-thread-simulation-to-realize-second-killing-and-robbing.html | Script Home | +Copy Link

Article reprint please specify:PHP multi thread simulation to realize second killing and robbing | Script Home

You may also be interested in these articles!