興味深い問題です。Uniのコースワークの一部で、リクエストを受信して処理するサーバーのシミュレーションを実行するという課題がありました。私はこれを何度も構築して再構築しましたが、私ができる限り効率的にしたと思いますが、このプログラムの効率を改善するためにあなたが見ることができるものはありますか?
シミュレーションが実行され$simulationLength
、それぞれ$currentTime
に1/$arrival_rate
新しいリクエストが到着する可能性があります。サイズのキューは$maxQueueSize
これらの要求を保持し、キューがいっぱいになるとそれらを拒否します。各$currentTime
CPUは、からアイテムを削除し$queue
て$executionTime
数秒間処理することができます。その時点で、CPU(サーバー)はビジーキューから削除され、別の要求で自由に処理できます。
私のコード:
<?php
...SNIP error checking...
$queue_size = isset($argv[1]) ? $argv[1] : 10000;
$arrival_rate = isset($argv[2]) ? $argv[2] : 3;
$number_of_servers = isset($argv[3]) ? $argv[3] : 2;
$execution_time = isset($argv[4]) ? $argv[4] : 25;
...SNIP error checking...
$simulationLength = 1000000;
$arrivalRate = $arrival_rate;
$executionTime = $execution_time;
$busyServers = array();
$freeServers = $number_of_servers;
$currentTime = 0;
$currentQueueSize = 0;
$maxQueueSize = $queue_size; //Max
$queue = array();
//Stats
$totalRequests = 0;
$rejectedRequests = 0;
$queueSize = array_fill(0, 100, $simulationLength);
$totalWaitingTime = 0;
//while the simulation is running
while($currentTime++ < $simulationLength) {
//a request has arrived
if(mt_rand(1, $arrival_rate)==1) {
$totalRequests++;
//Adding to the queue
if($currentQueueSize < $maxQueueSize) {
$currentQueueSize++;
array_push($queue, $currentTime); //add to the end of the queue
} else {
$rejectedRequests++;
}
} //end request arrived
//Only one server can fetch requests at a time
if(!empty($busyServers)&&reset($busyServers) < $currentTime) {
unset($busyServers[key($busyServers)]); //remove first element - efficient
$freeServers++; //increase free servers
}
//Only one server can fetch requests at a time
if($currentQueueSize>0&&$freeServers>1) {
$currentQueueSize--;
reset($queue); //reset pointer
$queueTime = $queue[key($queue)]; //read of the front
unset($busyServers[key($busyServers)]); //delete off print
$freeServers--; //use up a server
$busyServers[] = $currentTime + $executionTime; //mark when free
$totalWaitingTime += ($currentTime - $queueTime) + $executionTime;
}
$queueSize[$currentTime] = $currentQueueSize;
}
printf("Requests served %d\n", $totalRequests);
printf("Rejected requests %d\n", $rejectedRequests);
printf("Total waiting time %d\n", $totalWaitingTime);
printf("Percentage of rejected requests %0.1f\n", $rejectedRequests/$totalRequests);
printf("The average queue size %d\n", array_sum($queueSize)/sizeof($queueSize));
printf("Average response time %d\n", $totalWaitingTime/$totalRequests);
?>
御時間ありがとうございます、