Timer page runtime monitoring implemented by php

Source: Internet
Author: User
Tags usleep
This article mainly introduces the Timer page running time monitoring class implemented by php, which can detect different running times by different keys, for more information about how to monitor the running time of Timer pages implemented by php, see the example in this article. it is a very practical PHP file. Share it with you for your reference. The specific analysis is as follows:

This php Timer page runtime monitoring class can monitor different runtime according to different keys.

The Timer. class. php class file is as follows:

<? Php/** Timer class: calculates the page running time. different running times can be calculated based on different keys * Date: * Author: fdipzone * Ver: 1.0 ** Func: * public start record start time * public end record end time * public getTime calculation run time * pulbic printTime output run time * private getKey get key * private getMicrotime get microtime */class Timer {/ /class start private $ _ start = array (); private $ _ end = array (); private $ _ default_key = 'timer'; private $ _ prefix = 'timer _'; /** Record start time * @ param String $ key mark */public function start ($ key = '') {$ flag = $ this-> getKey ($ key ); $ this-> _ start [$ flag] = $ this-> getMicrotime ();} /** record end time * @ param String $ key mark */public function end ($ key = '') {$ flag = $ this-> getKey ($ key ); $ this-> _ end [$ flag] = $ this-> getMicrotime ();} /** computing run time * @ param String $ key mark * @ return float */public function getTime ($ key = '') {$ flag = $ this-> getKey ($ Key); if (isset ($ this-> _ end [$ flag]) & isset ($ this-> _ start [$ flag]) {return (float) ($ this-> _ end [$ flag]-$ this-> _ start [$ flag]);} else {return 0 ;}} /** output page running time * @ param String $ key mark * @ return String */public function printTime ($ key = '') {printf ("% srun time % f ms \ r \ n", $ key = ''? $ Key: $ key. '', $ this-> getTime ($ key) * 1000 );} /** Get key * @ param String $ key mark * @ return String */private function getKey ($ key = '') {if ($ key = '') {return $ this-> _ default_key;} else {return $ this-> _ prefix. $ key ;}}/** get microtime */private function getMicrotime () {list ($ usec, $ sec) = explode ('', microtime ()); return (float) $ usec + (float) $ sec ;}// class end?>

The demo code is as follows:

<?php  require 'Timer.class.php';  $timer = new Timer(); $timer->start();  $timer->start('program1'); usleep(mt_rand(100000,500000)); $timer->end('program1'); $timer->printTime('program1');  $timer->start('program2'); usleep(mt_rand(100000,500000)); $timer->end('program2'); $timer->printTime('program2');  $timer->end(); $timer->printTime();  ?>

Demo Running output:

program1 run time 163.285971 ms program2 run time 100.347042 ms run time 264.035940 ms 

Click here to download the complete instance source code.

I hope this article will help you with PHP programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.