Profiler::getStatCalls()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
namespace Smoren\Profiler;
4
5
/**
6
 * Profiler helper
7
 */
8
class Profiler
9
{
10
    /**
11
     * @var array<string, float>
12
     */
13
    protected static $processMap = [];
14
    /**
15
     * @var array<string, float>
16
     */
17
    protected static $statTimeMap = [];
18
    /**
19
     * @var array<string, int>
20
     */
21
    protected static $statCallsCountMap = [];
22
23
    /**
24
     * Starts the profiling of named process
25
     * @param string $name process name
26
     * @throws ProfilerException
27
     */
28
    public static function start(string $name): void
29
    {
30
        if(isset(static::$processMap[$name])) {
31
            throw new ProfilerException(
32
                "process '{$name}' already started",
33
                ProfilerException::STATUS_PROCESS_ALREADY_STARTED,
34
                null,
35
                ['name' => $name]
36
            );
37
        }
38
        static::$processMap[$name] = microtime(true);
39
40
        if(!isset(static::$statCallsCountMap[$name])) {
41
            static::$statCallsCountMap[$name] = 0;
42
        }
43
44
        static::$statCallsCountMap[$name]++;
45
    }
46
47
    /**
48
     * Stops the profiling of named process
49
     * @param string $name process name
50
     * @throws ProfilerException
51
     */
52
    public static function stop(string $name): void
53
    {
54
        if(!isset(static::$processMap[$name])) {
55
            throw new ProfilerException(
56
                "process '{$name}' not started yet",
57
                ProfilerException::STATUS_PROCESS_NOT_STARTED_YET,
58
                null,
59
                ['name' => $name]
60
            );
61
        }
62
63
        if(!isset(static::$statTimeMap[$name])) {
64
            static::$statTimeMap[$name] = 0;
65
        }
66
        static::$statTimeMap[$name] += microtime(true) - static::$processMap[$name];
67
        unset(static::$processMap[$name]);
68
    }
69
70
    /**
71
     * Profiles the body of callback function
72
     * @param string $name process name
73
     * @param callable $callback function with body to profile
74
     * @throws ProfilerException
75
     */
76
    public static function profile(string $name, callable $callback): void
77
    {
78
        try {
79
            static::start($name);
80
            $callback();
81
        } finally {
82
            static::stop($name);
83
        }
84
    }
85
86
    /**
87
     * Returns time usage summary
88
     * @return array<string, float>
89
     */
90
    public static function getStatTime(): array
91
    {
92
        uasort(static::$statTimeMap, function($lhs, $rhs) {
93
            if($rhs > $lhs) {
94
                return 1;
95
            }
96
            if($lhs < $rhs) {
97
                return -1;
98
            }
99
            return 0;
100
        });
101
        return static::$statTimeMap;
102
    }
103
104
    /**
105
     * Returns calls counters summary
106
     * @return array<string, int>
107
     */
108
    public static function getStatCalls(): array
109
    {
110
        uasort(static::$statCallsCountMap, function($lhs, $rhs) {
111
            return $rhs-$lhs;
112
        });
113
        return static::$statCallsCountMap;
114
    }
115
116
    /**
117
     * Clears all stat
118
     */
119
    public static function clear(): void
120
    {
121
        static::$processMap = [];
122
        static::$statTimeMap = [];
123
        static::$statCallsCountMap = [];
124
    }
125
}
126