Benchmark   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 92
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A start_clock() 0 6 2
A _stop_clock() 0 16 3
A memory_usage() 0 8 2
A time_execution() 0 7 2
A test() 0 13 2
1
<?php
2
/**
3
 * KumbiaPHP web & app Framework
4
 *
5
 * LICENSE
6
 *
7
 * This source file is subject to the new BSD license that is bundled
8
 * with this package in the file LICENSE.
9
 *
10
 * @category   Kumbia
11
 * @package    Core
12
 *
13
 * @copyright  Copyright (c) 2005 - 2019 KumbiaPHP Team (http://www.kumbiaphp.com)
14
 * @license    https://github.com/KumbiaPHP/KumbiaPHP/blob/master/LICENSE   New BSD License
15
 */
16
17
/**
18
 * Class Para el manejo de Benchmark y Profiling
19
 *
20
 * Permite obtener tiempo de ejecucion de un script ó una peticion
21
 * con el fin de encontrar posibles cuellos de botellas y
22
 * optimizar el rendimiento de la aplicacion...
23
 *
24
 * @category   Kumbia
25
 * @package    Core
26
 */
27
final class Benchmark
28
{
29
30
    /**
31
     * Almacena los datos de un Benchmark especifico, esto para evitar colision
32
     *
33
     * @var name
34
     */
35
    private static $_benchmark;
36
    private static $_avgload = 0;
37
38
    /**
39
     * Inicia el reloj (profiling)
40
     *
41
     * @return array $_benchmark
42
     */
43
    public static function start_clock($name)
44
    {
45
        if (!isset(self::$_benchmark[$name])) {
46
            self::$_benchmark[$name] = array('start_time' => microtime(), 'final_time' => 0, 'memory_start' => memory_get_usage(), 'memory_stop' => 0, 'time_execution' => 0);
47
        }
48
    }
49
50
    /**
51
     * Detiene el reloj para efecto del calculo del
52
     * tiempo de ejecucion de un script
53
     *
54
     * @return array $_benchmark
55
     */
56
    private static function _stop_clock($name)
57
    {
58
        if (isset(self::$_benchmark[$name])) {
59
            $load = 0;
60
            if (PHP_OS == 'Linux') {
61
                $load = sys_getloadavg();
62
            }
63
            self::$_avgload = $load[0];
64
            self::$_benchmark[$name]['memory_stop'] = memory_get_usage();
65
            self::$_benchmark[$name]['final_time'] = microtime();
66
            list ($sm, $ss) = explode(' ', self::$_benchmark[$name]['start_time']);
67
            list ($em, $es) = explode(' ', self::$_benchmark[$name]['final_time']);
68
            self::$_benchmark[$name]['time_execution'] = number_format(($em + $es) - ($sm + $ss), 4);
69
            return self::$_benchmark[$name]['time_execution'];
70
        }
71
    }
72
73
    /**
74
     * Permite obtener la memoria usada por un script
75
     *
76
     * @return string memory_usage
77
     */
78
    public static function memory_usage($name)
79
    {
80
        if (self::$_benchmark[$name]) {
81
            self::$_benchmark[$name]['memory_usage'] = number_format((self::$_benchmark[$name]['memory_stop'] - self::$_benchmark[$name]['memory_start']) / 1048576, 2);
82
            return self::$_benchmark[$name]['memory_usage'];
83
        }
84
        throw new KumbiaException("No existe el Benchmark para el nombre: '$name', especificado");
85
    }
86
87
    /**
88
     * Retorna el tiempo de ejecucion del scripts (profiling)
89
     *
90
     * @return string time_execution
91
     */
92
    public static function time_execution($name)
93
    {
94
        if (isset(self::$_benchmark[$name])) {
95
            return self::_stop_clock($name);
96
        }
97
        throw new KumbiaException("No existe el Benchmark para el nombre: $name, especificado");
98
    }
99
100
    /**
101
     *
102
     * @deprecated
103
     */
104
    public static function test($func, $loops)
105
    {
106
        self::start_clock($func);
107
        ob_start();
108
        for ($i = 1; $i <= $loops; $i++) {
109
            $func;
110
        }
111
        ob_end_flush();
112
        $time = self::time_execution($func);
113
        echo '** Funcion: ', $func;
114
        echo $loops, ' veces';
115
        echo ' Tiempo: ', $time;
116
    }
117
118
}