Hailwood
Hailwood

Reputation: 92691

benchmarking code in php

I have some php code,

Something in the code (a large portion of code) is causing the page to take upwards of 30 seconds to load,

Is there an easy way to find out what part of the code is doing this?

Upvotes: 0

Views: 258

Answers (3)

user743234
user743234

Reputation:

You can simply check the execution time of your code by blocks in PHP follow this:

start_time = get_time_now
<execution block 1>
execution_time = get_time_now - start_time

start_time = get_time_now
<execution block 2>
execution_time = get_time_now - start_time

So each time your certain code block is executed, it will be checked and printed out to the screen in second. What you need to do is to add the calculation to every single block of your code to test which one is slowing down the page. I've written a sample code for you to reference:

<?php
    $time_start = microtime(true);
    // Execution 1
    usleep(300); // say it sleeps for a while, like executing something
    $time = microtime(true) - $time_start;
    echo "Execution 1 is done in $time seconds \n";

    $time_start = microtime(true);
    // Execution 2
    usleep(300); // say it sleeps for a while, like executing something
    $time = microtime(true) - $time_start;
    echo "Execution 2 is done in $time seconds \n";

    $time_start = microtime(true);
    // Execution 3
    usleep(3000000); // this gonna slow down your page
    $time = microtime(true) - $time_start;
    echo "Execution 3 is done in $time seconds";
    echo "And it's slowing down your page \n";
?>

But note that it's hard to tell the difference between simple instructions since they're just different in lower than millisecond; however if there is a complex instruction that slows down your page, it will be very easy to notice because it will stand out from the rest :)

Upvotes: 1

Eugene Manuilov
Eugene Manuilov

Reputation: 4361

Try to use profiling/tracing with xdebug: Profiling PHP Applications With xdebug and Tracing PHP Applications with xdebug

Upvotes: 7

inquam
inquam

Reputation: 12942

If you are using Zend Studio you could use their profiler. Or profile using xdebug. Otherwise you could put timers around functioncalls to narrow down your search.

Something like

$startTime = microtime();
... code to execute ...
$timeSpent = microtime() - $startTime;

You could also use the PECL APD extension

<?php
apd_set_pprof_trace();

... rest of your code ...

This will give you an output similar to

Trace for /yourscript.php
Total Elapsed Time = 0.00
Total System Time  = 0.00
Total User Time    = 0.00


    Real         User        System             secs/    cumm
    %Time (excl/cumm)  (excl/cumm)  (excl/cumm) Calls    call    s/call  Memory Usage Name
    --------------------------------------------------------------------------------------
    100.0 0.00 0.00  0.00 0.00  0.00 0.00     1  0.0000   0.0009            0 main
    56.9 0.00 0.00  0.00 0.00  0.00 0.00     1  0.0005   0.0005            0 apd_set_pprof_trace
    28.0 0.00 0.00  0.00 0.00  0.00 0.00    10  0.0000   0.0000            0 foo
    14.3 0.00 0.00  0.00 0.00  0.00 0.00    10  0.0000   0.0000            0 bar

Good luck!

Upvotes: 1

Related Questions