Welcome Guest, Not a member yet? Register   Sign In
Issues with benchmark class
#1

[eluser]neen[/eluser]
I want to shift the decimal on elapsed_time() two places to the right, so I can output in MS (ie: 0.065 s to 65 ms). I'm using the following code:

Code:
<? $this->benchmark->elapsed_time() * 100 ?>

The problem is, it outputs (string) "{elapsed_time}" and then multiplies that by 100, which ends up equal to 0.

Now, I can always do this:
Code:
<?= number_format($this->benchmark->elapsed_time('total_execution_time_start', 'total_execution_time_end') * 1000)  ?>

Now the issue when I do that is very minor, but for some strange reason, 'total_execution_time_end' ends up before the benchmark for the current controller with profiler enabled...any ideas why this happens? Just curious..
#2

[eluser]TheFuzzy0ne[/eluser]
[quote author="neen" date="1237877939"]Now the issue when I do that is very minor, but for some strange reason, 'total_execution_time_end' ends up before the benchmark for the current controller with profiler enabled...any ideas why this happens? Just curious..[/quote]

I don't understand what this means. Please could your elaborate?
#3

[eluser]neen[/eluser]
Yes, in the profiler, normally the Total Execution Time ends up last, but for some reason when I have the above code in my view, it ends up between "Loading Base Time Classes" and "Controller Time Execution"...very strange...like I said, it's a minor issue and I am just curious why this happens
#4

[eluser]TheFuzzy0ne[/eluser]
I think it's because you're effectively adding the end marker before it's meant to be added. The final end benchmark tag is automatically set just as the last bit of view data is being sent to the browser. If you call it at any point before that, you'll end up with a false reading.

Hopefully this makes sense.
#5

[eluser]neen[/eluser]
Hmm, do you see any other way around my issue, short of writing a custom library?
#6

[eluser]TheFuzzy0ne[/eluser]
Sure. Try adding this to your code:

Code:
$elapsed_time = $this->benchmark->elapsed_time('total_execution_time_start');

Now this won't give you the exact same time that you'd get if you used {elapsed_time}, but if you use it within the last view, it should be pretty close. In fact it should give you the elapsed time (as expected).

Another alternative might be to do this:

./system/application/libraries/MY_Benchmark.php
Code:
class MY_Benchmark extends CI_Benchmark {

    function _display($output = '')
    {    
        // Note:  We use globals because we can't use $CI =& get_instance()
        // since this function is sometimes called by the caching mechanism,
        // which happens before the CI super object is available.
        global $BM, $CFG;
        
        // --------------------------------------------------------------------
        
        // Set the output data
        if ($output == '')
        {
            $output =& $this->final_output;
        }
        
        // --------------------------------------------------------------------
        
        // Do we need to write a cache file?
        if ($this->cache_expiration > 0)
        {
            $this->_write_cache($output);
        }
        
        // --------------------------------------------------------------------

        // Parse out the elapsed time and memory usage,
        // then swap the pseudo-variables with the data

        $elapsed = $BM->elapsed_time('total_execution_time_start', 'total_execution_time_end');

# START EDIT

        if (isset($this->show_ms))
        {
            $elapsed = $elapsed * 100;
        }    
    
# END EDIT

        $output = str_replace('{elapsed_time}', $elapsed, $output);
        
        $memory     = ( ! function_exists('memory_get_usage')) ? '0' : round(memory_get_usage()/1024/1024, 2).'MB';
        $output = str_replace('{memory_usage}', $memory, $output);        

        // --------------------------------------------------------------------
        
        // Is compression requested?
        if ($CFG->item('compress_output') === TRUE)
        {
            if (extension_loaded('zlib'))
            {
                if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) AND strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE)
                {
                    ob_start('ob_gzhandler');
                }
            }
        }

        // --------------------------------------------------------------------
        
        // Are there any server headers to send?
        if (count($this->headers) > 0)
        {
            foreach ($this->headers as $header)
            {
                @header($header[0], $header[1]);
            }
        }        

        // --------------------------------------------------------------------
        
        // Does the get_instance() function exist?
        // If not we know we are dealing with a cache file so we'll
        // simply echo out the data and exit.
        if ( ! function_exists('get_instance'))
        {
            echo $output;
            log_message('debug', "Final output sent to browser");
            log_message('debug', "Total execution time: ".$elapsed);
            return TRUE;
        }
    
        // --------------------------------------------------------------------

        // Grab the super object.  We'll need it in a moment...
        $CI =& get_instance();
        
        // Do we need to generate profile data?
        // If so, load the Profile class and run it.
        if ($this->enable_profiler == TRUE)
        {
            $CI->load->library('profiler');                
                                        
            // If the output data contains closing </body> and </html> tags
            // we will remove them and add them back after we insert the profile data
            if (preg_match("|</body>.*?</html>|is", $output))
            {
                $output  = preg_replace("|</body>.*?</html>|is", '', $output);
                $output .= $CI->profiler->run();
                $output .= '</body></html>';
            }
            else
            {
                $output .= $CI->profiler->run();
            }
        }
        
        // --------------------------------------------------------------------

        // Does the controller contain a function named _output()?
        // If so send the output there.  Otherwise, echo it.
        if (method_exists($CI, '_output'))
        {
            $CI->_output($output);
        }
        else
        {
            echo $output;  // Send it to the browser!
        }
        
        log_message('debug', "Final output sent to browser");
        log_message('debug', "Total execution time: ".$elapsed);        
    }
}

The above code is untested

Simply do this at any point before {elapsed_time} is parsed:
Code:
$this->output->show_ms = TRUE;

That should cause the output to be in milliseconds.

Hope this helps.
#7

[eluser]jedd[/eluser]
I can bite my tongue no longer.

Moving the decimal point two places will not convert seconds to milliseconds - it'll convert it to centiseconds.

milli- refers to a thousandth, so you would need to move the decimal point three places.

This means multiplying by a thousand, not a hundred.

I blame the various cultural backgrounds - dealing with miles, gallons, feet, farthings, chains and furlongs can't have been easy. </rant>
#8

[eluser]TheFuzzy0ne[/eluser]
You're right. Thanks for pointing that out. That was be last thing at night (4AM), and first thing this morning after 5 hours sleep.

It's a bit like the difference between a UK billion and a US billion.
UK = 1,000,000,000,000
US = 1,000,000,000
#9

[eluser]neen[/eluser]
Yeah, I meant 3 haha, in my actual code I had * 1000 Tongue




Theme © iAndrew 2016 - Forum software by © MyBB