Blame view

vendor/cakephp/debug_kit/src/DebugTimer.php 5.76 KB
c4650843   Etienne Pallier   Ajout du dossier ...
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
/**
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @since         DebugKit 0.1
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */
namespace DebugKit;

use Cake\Error\Debugger;

/**
 * Contains methods for Profiling and creating timers.
 *
 */
class DebugTimer
{

    /**
     * Internal timers array
     *
     * @var array
     */
    protected static $_timers = [];

    /**
     * Start an benchmarking timer.
     *
     * @param string $name The name of the timer to start.
     * @param string $message A message for your timer
     * @return bool Always true
     */
    public static function start($name = null, $message = null)
    {
        $start = microtime(true);

        if (!$name) {
            $named = false;
            $calledFrom = debug_backtrace();
            $name = Debugger::trimPath($calledFrom[0]['file']) . ' line ' . $calledFrom[0]['line'];
        } else {
            $named = true;
        }

        if (!$message) {
            $message = $name;
        }

        $_name = $name;
        $i = 1;
        while (isset(self::$_timers[$name])) {
            $i++;
            $name = $_name . ' #' . $i;
        }

        if ($i > 1) {
            $message .= ' #' . $i;
        }

        self::$_timers[$name] = [
            'start' => $start,
            'message' => $message,
            'named' => $named
        ];

        return true;
    }

    /**
     * Stop a benchmarking timer.
     *
     * $name should be the same as the $name used in startTimer().
     *
     * @param string $name The name of the timer to end.
     * @return bool true if timer was ended, false if timer was not started.
     */
    public static function stop($name = null)
    {
        $end = microtime(true);
        if (!$name) {
            $names = array_reverse(array_keys(self::$_timers));
            foreach ($names as $name) {
                if (!empty(self::$_timers[$name]['end'])) {
                    continue;
                }
                if (empty(self::$_timers[$name]['named'])) {
                    break;
                }
            }
        } else {
            $i = 1;
            $_name = $name;
            while (isset(self::$_timers[$name])) {
                if (empty(self::$_timers[$name]['end'])) {
                    break;
                }
                $i++;
                $name = $_name . ' #' . $i;
            }
        }
        if (!isset(self::$_timers[$name])) {
            return false;
        }
        self::$_timers[$name]['end'] = $end;

        return true;
    }

    /**
     * Get all timers that have been started and stopped.
     * Calculates elapsed time for each timer. If clear is true, will delete existing timers
     *
     * @param bool $clear false
     * @return array
     */
    public static function getAll($clear = false)
    {
        $start = self::requestStartTime();
        $now = microtime(true);

        $times = [];
        if (!empty(self::$_timers)) {
            $firstTimer = reset(self::$_timers);
            $_end = $firstTimer['start'];
        } else {
            $_end = $now;
        }
        $times['Core Processing (Derived from $_SERVER["REQUEST_TIME"])'] = [
            'message' => __d('debug_kit', 'Core Processing (Derived from $_SERVER["REQUEST_TIME"])'),
            'start' => 0,
            'end' => $_end - $start,
            'time' => round($_end - $start, 6),
            'named' => null
        ];
        foreach (self::$_timers as $name => $timer) {
            if (!isset($timer['end'])) {
                $timer['end'] = $now;
            }
            $times[$name] = array_merge($timer, [
                'start' => $timer['start'] - $start,
                'end' => $timer['end'] - $start,
                'time' => self::elapsedTime($name)
            ]);
        }
        if ($clear) {
            self::$_timers = [];
        }

        return $times;
    }

    /**
     * Clear all existing timers
     *
     * @return bool true
     */
    public static function clear()
    {
        self::$_timers = [];

        return true;
    }

    /**
     * Get the difference in time between the timer start and timer end.
     *
     * @param string $name the name of the timer you want elapsed time for.
     * @param int $precision the number of decimal places to return, defaults to 5.
     * @return float number of seconds elapsed for timer name, 0 on missing key
     */
    public static function elapsedTime($name = 'default', $precision = 5)
    {
        if (!isset(self::$_timers[$name]['start']) || !isset(self::$_timers[$name]['end'])) {
            return 0;
        }

        return round(self::$_timers[$name]['end'] - self::$_timers[$name]['start'], $precision);
    }

    /**
     * Get the total execution time until this point
     *
     * @return float elapsed time in seconds since script start.
     */
    public static function requestTime()
    {
        $start = self::requestStartTime();
        $now = microtime(true);

        return ($now - $start);
    }

    /**
     * get the time the current request started.
     *
     * @return float time of request start
     */
    public static function requestStartTime()
    {
        if (defined('TIME_START')) {
            $startTime = TIME_START;
        } elseif (isset($GLOBALS['TIME_START'])) {
            $startTime = $GLOBALS['TIME_START'];
        } else {
            $startTime = env('REQUEST_TIME');
        }

        return $startTime;
    }
}