Issues (57)

core/libs/benchmark/benchmark.php (1 issue)

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 - 2023 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
0 ignored issues
show
The type name was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
34
     */
35
    private static $_benchmark;
36
    private static $_avgload = 0;
37
38
    /**
39
     * Inicia el reloj (profiling)
40
     *
41
     * @return void $_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 string|null $_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
}