corsasport.co.uk
 

Corsa Sport » Message Board » Off Day » Geek Day » Tech Question PHP


New Topic

New Poll
  Subscribe | Add to Favourites

You are not logged in and may not post or reply to messages. Please log in or create a new account or mail us about fixing an existing one - register@corsasport.co.uk

There are also many more features available when you are logged in such as private messages, buddy list, location services, post search and more.


Author Tech Question PHP
Reedy
Member

Registered: 11th Apr 04
Location: Hammersmith
User status: Offline
   21st Oct 09 at 13:14   View User's Profile U2U Member Reply With Quote

I had to do a test for a job and they gave me this question.
Q2.
Develop an efficient algorithm for a weighted round robin scheme which evenly spreads
load between several servers, taking into account the “weight” of each server (more weight
means that server can handle more load).
$servers = array(
"127.0.0.1",
"127.0.0.2",
"127.0.0.3",
"127.0.0.4"
);
$weights = array(50, 25, 15, 10);
$picked = array();
$total = 100000;
for($i = 0; $i < $total; $i++) {
$server = weightedRoundRobin($servers, $weights);
$picked[$server]++;
}
for($i = 0; $i < count($servers); $i++) {
$server = $servers[$i];
$times = $picked[$server];
$percent = round(($times / $total) * 100, 2);
echo "{$server} picked {$times} times ({$percent}% of total)<br />";
}
function weightedRoundRobin($servers, $weights) {
// ... your algorithm. Return a server IP address.
}


At first I wasnt sure how to approach it but in the end I came up with this solution:
function weightedRoundRobin($servers, $weights)
{
for ( $i=0; $i<count( $weights ); $i++ )
{
for ( $j=0; $j< $weights[$i]; $j++ )
{
$randomArray[] = $i;
}
}

$location = $servers[$randomArray[array_rand($randomArray)]];
return $location;
}


Im just wondering if anybody else has a better solution.
Neo
Member

Registered: 20th Feb 07
Location: Essex
User status: Offline
21st Oct 09 at 13:34   View User's Profile U2U Member Reply With Quote

That works yeah, one bit i don't quite understand though..

Just having a look and see if there is another algorithm for the same result.
Reedy
Member

Registered: 11th Apr 04
Location: Hammersmith
User status: Offline
21st Oct 09 at 15:19   View User's Profile U2U Member Reply With Quote

quote:
Originally posted by Neo
That works yeah, one bit i don't quite understand though..

Just having a look and see if there is another algorithm for the same result.


which bit dont you understand?

 
New Topic

New Poll

  Related Threads Author Forum Replies Views Last Post
TURBO CORSA big eck Help Zone, Modification and ICE Advice 31 3569
13th Jun 03 at 10:25
by Corsakid84
 
Scrubbing cargraphics4u.com Help Zone, Modification and ICE Advice 12 297
23rd Nov 04 at 11:55
by hazey-corsa
 
LMF Gaz General Chat 2 207
29th Sep 07 at 13:09
by Gaz
 
your best 10 games ssj_kakarot Geek Day 51 2788
17th Oct 07 at 17:18
by Lee Wilson
 
PROBLEM: Oil Light prob ad1989 General Chat 32 2177
1st Jul 08 at 17:14
by Haimsey
 

Corsa Sport » Message Board » Off Day » Geek Day » Tech Question PHP 29 database queries in 0.0113101 seconds