php Timer 页面运行时间监测类

2023-05-16

php Timer 页面运行时间监测类,可按不同key监测不同的运行时间


Timer.class.php

<?php
/** Timer class, 计算页面运行时间,可按不同key计算不同的运行时间
*   Date:   2014-02-28
*   Author: fdipzone
*   Ver:    1.0
*
*   Func:
*   public  start        记录开始时间
*   public  end          记录结束时间
*   public  getTime      计算运行时间
*   pulbic  printTime    输出运行时间
*   private getKey       获取key
*   private getMicrotime 获取microtime
*/

class Timer{ // class start

    private $_start = array();
    private $_end = array();
    private $_default_key = 'Timer';
    private $_prefix = 'Timer_';


    /** 记录开始时间
    * @param String $key 标记
    */
    public function start($key=''){
        $flag = $this->getKey($key);
        $this->_start[$flag] = $this->getMicrotime();
    }


    /** 记录结束时间
    * @param String $key 标记
    */
    public function end($key=''){
        $flag = $this->getKey($key);
        $this->_end[$flag] = $this->getMicrotime();
    }


    /** 计算运行时间
    * @param  String $key 标记
    * @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;
        }
    }


    /** 输出页面运行时间
    * @param  String $key 标记
    * @return String
    */
    public function printTime($key=''){
        printf("%srun time %f ms\r\n", $key==''? $key : $key.' ', $this->getTime($key)*1000);
    }


    /** 获取key
    * @param  String $key 标记
    * @return String 
    */
    private function getKey($key=''){
        if($key==''){
            return $this->_default_key;
        }else{
            return $this->_prefix.$key;
        }
    }


    /** 获取microtime
    */
    private function getMicrotime(){
        list($usec, $sec) = explode(' ', microtime());
        return (float)$usec + (float)$sec;
    }


} // class end

?>
demo:

<?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运行输出:

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


源码下载地址:点击查看


本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

php Timer 页面运行时间监测类 的相关文章

随机推荐

  • MessagePack 序列化格式

    MessagePack 是一种有效的二进制序列化格式 与JSON一样可以在多语言中转换数据 xff0c 但对比JSON xff0c 速度更快 xff0c 转换后数据更小 安装 1 下载msgpack xff0c 下载地址 xff1a 点击下
  • ubunut 管理开机程序

    安装 sysv rc conf sudo apt get install sysv rc conf 安装完成后输入命令 xff0c 可以管理开机程序 sudo sysv rc conf 带 X 的都是开机启动 xff0c 移动到要修改的位置
  • VC中TRACE()的用法,终于学会了点皮毛,好累!

    个人总结 xff1a 最近看网络编程是碰到了TRACE语句 xff0c 不知道在哪里输出 xff0c 查了一晚上资料也没找出来 xff0c 今天终于在CSDN上找到了 xff0c 真是个高地方啊 xff0c 方法如下 xff1a 1 在MF
  • php Cookies 操作类

    Cookies 操作类 功能 xff1a 1 保存 xff0c 读取 xff0c 更新 xff0c 清除cookies数据 2 可设置前缀 3 强制超时控制 4 cookies数据可以是字符串 xff0c 数组 xff0c 对象等 Cook
  • php 密码生成类

    php 密码生成类 功能 xff1a 1 可设定密码长度 2 可设定要生成的密码个数 xff0c 批量生成 3 可以指定密码的规则 xff0c 字母 xff0c 数字 xff0c 特殊字符等 GeneratePassword class p
  • php main 与 iframe 相互通讯类(同域/跨域)

    main 与 iframe 相互通讯类 之前写过一篇 iframe与主框架跨域相互访问方法 介绍了main与iframe相互通讯的原理 不了解原理的可以先看看 今天把main与iframe相互通讯的方法封装成类 主要有两个文件 JS Fra
  • php 异步调用方法

    php 异步调用方法 客户端与服务器端是通过HTTP协议进行连接通讯 xff0c 客户端发起请求 xff0c 服务器端接收到请求后执行处理 xff0c 并返回处理结果 有时服务器需要执行很耗时的操作 xff0c 这个操作的结果并不需要返回给
  • php 根据url自动生成缩略图,并处理高并发问题

    服务器生成缩略图的时机一般分为两种 xff1a 1 上传文件时生成 优点 xff1a 上传时就已经生成需要的缩略图 xff0c 读取时不需要再判断 xff0c 减少cpu运算 缺点 xff1a 当缩略图尺寸变化时或新增尺寸时 xff0c 需
  • MemcacheQ 安装与使用

    MemcacheQ 是一个基于 MemcacheDB 的消息队列服务器 官网地址 xff1a http memcachedb org memcacheq 特点 xff1a 1 简单易用 2 处理速度快 3 可创建多条队列 4 并发性能高 5
  • 深入Mysql字符集设置

    基本概念 字符 Character 是指人类语言中最小的表义符号 例如 A 39 B 39 等 xff1b 给定一系列字符 xff0c 对每个字符赋予一个数值 xff0c 用数值来代表对应的字符 xff0c 这一数值就是字符的编码 Enco
  • Rsync 安装与使用

    Rsync 是一个远程数据同步工具 xff0c 可以通过 LAN WAN 快速同步多台主机间的文件 Rsync 使用 34 Rsync演算法 34 来使本地和远程两个主机之间的文件同步 这个算法只传送两个文件的不同部分 xff0c 而不是每
  • php 字符串压缩方法比较

    php 提供的字符串压缩方法有 1 gzcompress Compress a string This function compress the given string using the ZLIB data format 2 gzen
  • php 生成短网址

    php 生成短网址 原理 xff1a 1 将原网址做crc32校验 xff0c 得到校验码 2 使用sprintf 39 u 39 将校验码转为无符号数字 3 对无符号数字进行求余62操作 xff08 大小写字母 43 数字等于62位 xf
  • debian7安装和配置小经验

    好久没碰linux了 xff0c 这回 捡 了台电脑测试玩 xff0c 发现过去的基本都忘记了 xff0c 拳不离手 曲不离口 xff0c 古人诚吾不欺 linux版本选择我比较熟悉debian xff0c 因为debian装包好方便 xf
  • ApacheBench 测试性能并使用GnuPlot绘制图表

    Apache Bench 是web性能测试工具 xff0c 功能强大 但输出的结果只是数字形式 xff0c 不容易看到数据的变化 因此 xff0c GnuPlot 的强大绘制功能正好可以弥补Apache Bench这方面的不足 关于Apac
  • VM ubuntu ping unknow host 解决方法

    例如网关地址为 xff1a 192 168 1 1 route add default gw 192 168 1 1 sudo vim etc resolv conf 在 etc resolv conf 中加入 Generated by N
  • apache日志分析及系统cpu,内存,负载情况监控

    1 根据Apache Log xff0c 获取当天秒并发数最多的记录 tail 10000 demo fdipzone com access log 2014 01 16 01 cut d 34 34 f 2 awk 39 print 1
  • Apache 搭建HTTPS Virtual Host

    Apache 搭建HTTPS Virtual Host 1 创建SSL证书 首先需要安装openssl xff0c linux系统默认已安装 xff0c 如没有则用以下命令安装 xff1a sudo apt get install open
  • MongoDB 主从同步设置

    MongoDB 主从同步设置 关于MongoDB的安装及启动参数说明可以参考我之前转载的 Ubuntu安装MongoDB 与 Mongodb启动命令mongod参数说明 主从设置 Master xff1a 192 168 111 103 P
  • php Timer 页面运行时间监测类

    php Timer 页面运行时间监测类 xff0c 可按不同key监测不同的运行时间 Timer class php lt php Timer class 计算页面运行时间 可按不同key计算不同的运行时间 Date 2014 02 28