public
Description: Git mirror of the CMS Made Simple 2.0 rewrite
Homepage: http://cmsmadesimple.org
Clone URL: git://github.com/tedkulp/cmsmadesimple-2-0.git
cmsmadesimple-2-0 / lib / classes / class.cms_profiler.php
100644 180 lines (165 sloc) 4.359 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php // -*- mode:php; tab-width:4; indent-tabs-mode:t; c-basic-offset:4; -*-
#CMS - CMS Made Simple
#(c)2004-2008 by Ted Kulp (ted@cmsmadesimple.org)
#This project's homepage is: http://cmsmadesimple.org
#
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#$Id$
#
#File apapted from (do not remove the copyright below!):
#
#@version profiler.php 5270 2006-10-02 03:30:29Z webImagery
#@package Joomla
#@copyright Copyright (C) 2005 - 2006 Open Source Matters. All rights reserved.
#@license GNU/GPL, see LICENSE.php
#Joomla! is free software. This version may have been modified pursuant
#to the GNU General Public License, and as distributed it includes or
#is derivative of works licensed under the GNU General Public License or
#other free or open source software licenses.
#See COPYRIGHT.php for copyright notices and details.
 
/**
* Class for handling profiling of various aspects of the system.
*
* @author Ted Kulp
* @since 2.0
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license GPL
**/
class CmsProfiler extends CmsObject
{
  /**
   * Constructor
   *
   * @access protected
   * @param string Prefix for mark messages
   **/
  function __construct( $prefix = '', $start_time = null )
  {
    $this->_start = ($start_time == null ? $this->get_microtime() : $start_time);
    $this->_prefix = $prefix;
    $this->_buffer = array();
  }
  
  /**
   * Returns a reference to the global Profiler object, only creating it
   * if it doesn't already exist.
   *
   * This method must be invoked as:
   * <pre> $browser = CmsProfiler::getInstance([$prefix]);</pre>
   *
   * @access public
   * @return CmsProfiler The Profiler object.
   **/
  public static function get_instance($prefix = '', $start_time = null)
  {
    static $instances;
 
    if (!isset($instances))
    {
      $instances = array();
    }
 
    if (empty($instances[$prefix]))
    {
      $instances[$prefix] = new CmsProfiler($prefix, $start_time);
    }
 
    return $instances[$prefix];
  }
 
  /**
   * Output a time mark
   *
   * @access public
   * @var string A label for the time mark
   **/
  function mark( $label )
  {
    $mark = @sprintf ( "\n<div class=\"profiler\">$this->_prefix %.4f $label</div>", $this->get_microtime() - $this->_start );
    $this->_buffer[] = $mark;
    return $mark;
  }
  
  function get_time()
  {
    return sprintf("%.4f", $this->get_microtime() - $this->_start);
  }
  
  /**
   * Reports on the buffered marks
   *
   * @access public
   * @param string Glue string
   **/
  function report( $memory = true, $database = true, $glue='' )
  {
    //$db = cms_db();
    
    echo '<div id="profiler_output" style="font-size: .75em;">';
 
    echo implode( $glue, $this->_buffer );
    if ($memory)
    {
      echo '<br />';
      echo $this->get_memory();
    }
    if ($database)
    {
      echo '<br />' . CmsDatabase::query_count() . ' queries executed';
    }
    
    echo '</div>';
  }
  
  /**
   *
   * @access public
   * @return float The current time
   **/
  public static function get_microtime()
  {
    list( $usec, $sec ) = explode( ' ', microtime() );
    return ((float)$usec + (float)$sec);
  }
  
  /**
   *
   * @access public
   * @return int The memory usage
   **/
  public static function get_memory()
  {
    static $isWin;
 
    if (function_exists( 'memory_get_usage' ))
    {
      return memory_get_usage();
    }
    else
    {
      if (is_null( $isWin ))
      {
        $isWin = (substr(PHP_OS, 0, 3) == 'WIN');
      }
      if ($isWin)
      {
        // Windows workaround
        $output = array();
        $pid = getmypid();
        exec( 'tasklist /FI "PID eq ' . $pid . '" /FO LIST', $output );
        if (!isset($output[5]))
        {
          $output[5] = null;
        }
        return substr( $output[5], strpos( $output[5], ':' ) + 1 );
      }
      else
      {
        return 0;
      }
    }
  }
}
 
# vim:ts=4 sw=4 noet
?>