4

I need some help to figure out a situation with a webserver regarding execution time. I've noticed a problem when the server returns a higher number of characters than ~41000 - around 40KB.

So I did a script:

<?php
    $php_start_time = MICROTIME(TRUE);
    echo $_GET['i'].':';
    for($i=0;$i<=$_GET['i'];$i++) { echo 'a'; }
    echo '<br>runtime: '.(MICROTIME(TRUE) - $php_start_time);
?>

And I try it out, more than 10 times each, multiple browsers:

when $_GET['i']=40952 I get around 0.013...ms

when $_GET['i']=40953 I get around 0.679...ms

a difference of 0.666 for just one single char?

I saw the runtime differs a lot when trying to get the page results from different locations (online proxy).

So I suppose it has to do with distances. I'm in EU, the server is in US.

Until I find a way to fix this issue the script is available at: http://selfreveal.com/speed_test_1.php?i=40953

Also a phpinfo(): http://selfreveal.com/phpinfo.php

B D
  • 41
  • 2
  • Performance questions? [Profile your code](http://stackoverflow.com/questions/133686/profiling-php-code) and stop guessing. Wait.. you're performing these measurements across an ocean? Too many variables.. voting to close. – Mike B Jun 05 '12 at 20:03
  • 1
    I appreciate the access, but be careful posting the live test (http://selfreveal.com/speed_test_1.php?i=40953)... someone could easily lock up your server by passing large values for i (your set timeouts and memory caps are pretty large, so it doesn't take many hits to drag it down... you might want an escape clause for very large numbers (above 100000 or so) – Ben D Jun 05 '12 at 20:06
  • Thank you for the suggestion. I added a limit of 500000. – B D Jun 05 '12 at 20:08
  • 1
    There are to many places to point fingers at. Until you narrow down some more you won't be finding an answer very soon. – zaf Jun 05 '12 at 20:14
  • adding flush() after every echo returns a higher runtime, even with $_GET['i']=40000 – B D Jun 05 '12 at 20:23
  • 1
    It's not very smart to post live `phpinfo()` output ... you might as well send out an invitation that says, **PLEASE HACK ME** –  Jun 05 '12 at 20:23
  • @rdlowrey - I agree... and you should also keep it in a location that can't be easily guessed (i.e. not mydomin.com/phpinfo.php) – Ben D Jun 05 '12 at 20:27
  • 1
    it is just a testing server... go hack it :) – B D Jun 05 '12 at 20:28

3 Answers3

0

My first guess is that you're running into a memory limit and the garbage collector is kicking in. Try increasing the memory limit:

ini_set('memory_limit'. '128M');
soulmerge
  • 73,842
  • 19
  • 118
  • 155
0

Timing isn't always the same, your computer/server could be handling another process and that could slow down other processes you're currently running.

Try running it 100x or 1000x and then get an avarage. You'll see the difference is almost gone.

frietkot
  • 891
  • 12
  • 28
  • Tried it lots of time since then. With 40953 or more I never got a runtime below 0.6 and with 40952 never above 0.1 – B D Jun 05 '12 at 20:02
  • I think this might just be an issue of perception. I wrote a performance check, and the tally is .085 to .093. `$array1=array(); for($i=0;$i<100;$i++){ $contents = file_get_contents('http://selfreveal.com/speed_test_1.php?i=40952'); $pieces = explode('
    runtime: ',$contents); $array1[] = floatval($pieces[1]); } $array2=array(); for($i=0;$i<100;$i++){ $contents = file_get_contents('http://selfreveal.com/speed_test_1.php?i=40953'); $pieces = explode('
    runtime: ',$contents); $array2[] = floatval($pieces[1]); } print_r($array1); print_r($array2);`
    – Ben D Jun 05 '12 at 20:22
  • apologies for the jumble there... the question was closed just as I was posting a proper response... the code there is the script I ran to check it, and then I output `echo "\naverage for 40952: ".(array_sum($array1)/count($array1)); echo "\naverage for 40953: ".(array_sum($array2)/count($array2));` – Ben D Jun 05 '12 at 20:25
  • Thank you for the script. When testing like that, indeed, it returns the same results for both pages. But still... When accessing it from the browser is returns weird values. I make a 100 x Iframe test: http://cl.ly/1Q15133D0l2n1L2j3B3p – B D Jun 05 '12 at 20:51
0

can you try to add flush(); right after the echo 'a'; and re-test ?
you can also try using ob_start() and ob_flush() to make sure it's not some internal buffer (OS dependent) or anything

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129