2012-04-18 6 views
2

Интересной проблемой для части курсовой работы Uni нам было предложено запустить имитацию сервера, получающего запросы, а затем обработать их. Я много раз строил и перестраивал, и я думаю, что сделал это настолько эффективным, насколько я могу, есть ли что-нибудь, что вы можете увидеть, чтобы повысить эффективность этой программы?Эффективность PHP при массовом расчете

Моделирование выполняется для $simulationLength, и при каждом $currentTime есть вероятность получения нового запроса 1/$arrival_rate. Очередь размером $maxQueueSize хранит эти запросы и отклоняет их, если очередь заполнена. Для каждого $currentTime ЦП может удалить элемент из $queue и работать с ним за $executionTime секунд, в этот момент ЦПУ (сервер) удаляется из очереди занятости и может работать с другим запросом.

Мой код:

<?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); 

?> 

Спасибо за ваше время,

+1

Вы можете использовать любой другой язык? PHP не известен своей скоростью, когда речь заходит о числовых операциях. – Blender

+0

http://www.phpbench.com - надеюсь, что страница поможет – Hajo

+0

Если вы не можете или не будете использовать другой язык, возможно, посмотрите на [HipHop-PHP] (https://github.com/facebook/hiphop -PHP/вики /). –

ответ

1

микро-оптимизаций но:

  • Когда в тяжелом цикле, как это, - $ прединкремента быстрее, чем $ postIncrement ++
  • array_push ($ queue, $ currentTime); является вызовом функции служебные данные: используйте $ queue [] = $ currentTime; вместо
  • $ CURRENTTIME + $ executionTime вычисляется дважды, вычислить его один раз и сохранить результат, а затем использовать это
Смежные вопросы