Home > Archive > PHP Language > May 2004 > PHP Performance/Algorithm Improvment Question
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
PHP Performance/Algorithm Improvment Question
|
|
| BlueBall 2004-05-12, 7:21 pm |
| I wrote the following code, what I want to do is to generate random data to
the client (for Networking stress testing purpose), but I find the
performance is so slow. When I measured it with my stop watch, I can only
get up to 2-3 Mbps on a 100 Mbps LAN. Compare to transfering a 10MB JPG
file, which has 20-30 Mbps...
Is there any suggestion on how to improve the code? or is it PHP
performance issue? Thanks in advance..
$size=10485760; // 10 MB
$buffer = "";
for ($i=1; $i<=($size/1024); $i++) {
for ($j=1; $j<=1024; $j++) {
$buffer .= chr(rand(1, 254));
}
echo $buffer;
$buffer = "";
}
| |
| Ron Barnett 2004-05-12, 7:21 pm |
| "BlueBall" <ballblue3242344@hotmail.com> wrote in message
news:bBwoc.10858$3Mj1.1196@news01.bloor.is.net.cable.rogers.com...
> I wrote the following code, what I want to do is to generate random data
to
> the client (for Networking stress testing purpose), but I find the
> performance is so slow. When I measured it with my stop watch, I can
only
> get up to 2-3 Mbps on a 100 Mbps LAN. Compare to transfering a 10MB JPG
> file, which has 20-30 Mbps...
> Is there any suggestion on how to improve the code? or is it PHP
> performance issue? Thanks in advance..
>
> $size=10485760; // 10 MB
> $buffer = "";
>
> for ($i=1; $i<=($size/1024); $i++) {
> for ($j=1; $j<=1024; $j++) {
> $buffer .= chr(rand(1, 254));
> }
> echo $buffer;
> $buffer = "";
> }
Hi,
It looks to me as if you're stressing the server not the network.
What happens if you put the echo and buffer ='' outside the outer loop?
you should then build a solid block of data to release.
at the moment, the network is presumably taking the data packet in its
stride and then having to sit idle while the webserver builds another 1kb
packet.
try sending 10k or 100kb blocks.
Another option is to write files containing the random data and send them as
passthrough
you could take a timestamp after the block is built and another when the
passthrough completes.
Cheers
Ron
| |
| Jan Gerritsen 2004-05-13, 10:30 am |
| Hi,
> $size=10485760; // 10 MB
> $buffer = "";
>
> for ($i=1; $i<=($size/1024); $i++) {
> for ($j=1; $j<=1024; $j++) {
> $buffer .= chr(rand(1, 254));
> }
> echo $buffer;
> $buffer = "";
> }
you should not calc so much data in the loop. when you do so, you don't
test the speed of the network transfer,.. maybe you want do something
like this: (not tested)
$size=10485760; // 10 MB
$block_size=1024; // 1kb
# Create buffer, there is no need to generate new
# "random" date for each block to send,..
$buffer = "";
for ($j = 1; $j <= $block_size; $j++) {
$buffer .= chr(rand(1, 254));
}
# Calc repeat counter outside the loop
$counter = $size / $block_size;
# function for getting the time in useconds,..
function getmicrotime(){
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}
# At this point you should get the start time
$time_start = getmicrotime();
# Send data
for ($i=1; $i <= $counter; $i++) {
echo $buffer;
}
# Get end time
$time_end = getmicrotime();
# Calc time needed
$time = $time_end - $time_start;
# clac throughput
$throughput = ((float)$size / $time);
# There may be a problem of reading this,
# because you sent binary (random) data,..
# so you may want to write this into a file,..
echo "Time needed for transfer: $time seconds - throughput: $throughput
MB/s";
greets,
Jan
|
|
|
|
|