/var/www/html_de/wp-content/plugins/query-monitor/classes/Timer.php


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
<?php declare(strict_types 1);
/**
 * Timer that collects timing and memory usage.
 *
 * @package query-monitor
 */

class QM_Timer {

    
/**
     * @var array<string, mixed>
     * @phpstan-var array{
     *   time: float,
     *   memory: int,
     *   data: mixed[]|null,
     * }
     */
    
protected $start;

    
/**
     * @var array<string, mixed>|null
     * @phpstan-var array{
     *   time: float,
     *   memory: int,
     *   data: mixed[]|null,
     * }|null
     */
    
protected $end null;

    
/**
     * @var QM_Backtrace
     */
    
protected $trace;

    
/**
     * @var array<string, array<string, mixed>>
     * @phpstan-var array<string, array{
     *   time: float,
     *   memory: int,
     *   data: mixed[]|null,
     * }>
     */
    
protected $laps = array();

    
/**
     * @param mixed[] $data
     * @return self
     */
    
public function start( ?array $data null ) {
        
$this->trace = new QM_Backtrace();
        
$this->start = array(
            
'time' => microtimetrue ),
            
'memory' => memory_get_usage(),
            
'data' => $data,
        );
        return 
$this;
    }

    
/**
     * @param mixed[] $data
     * @return self
     */
    
public function stop( ?array $data null ) {

        
$this->end = array(
            
'time' => microtimetrue ),
            
'memory' => memory_get_usage(),
            
'data' => $data,
        );

        return 
$this;

    }

    
/**
     * @param mixed[] $data
     * @param string $name
     * @return self
     */
    
public function lap( ?array $data null, ?string $name null ) {

        
$lap = array(
            
'time' => microtimetrue ),
            
'memory' => memory_get_usage(),
            
'data' => $data,
        );

        if ( ! isset( 
$name ) ) {
            
$i sprintf(
                
/* translators: %s: Timing lap number */
                
__'Lap %s''query-monitor' ),
                
number_format_i18ncount$this->laps ) + )
            );
        } else {
            
$i $name;
        }

        
$this->laps$i ] = $lap;

        return 
$this;

    }

    
/**
     * @return mixed[]
     */
    
public function get_laps() {

        
$laps = array();
        
$prev $this->start;

        foreach ( 
$this->laps as $lap_id => $lap ) {

            
$lap['time_used'] = $lap['time'] - $prev['time'];
            
$lap['memory_used'] = $lap['memory'] - $prev['memory'];

            
$laps$lap_id ] = $lap;
            
$prev $lap;

        }

        return 
$laps;

    }

    
/**
     * @return float
     */
    
public function get_time() {
        return 
$this->end['time'] - $this->start['time'];
    }

    
/**
     * @return int
     */
    
public function get_memory() {
        return 
$this->end['memory'] - $this->start['memory'];
    }

    
/**
     * @return float
     */
    
public function get_start_time() {
        return 
$this->start['time'];
    }

    
/**
     * @return int
     */
    
public function get_start_memory() {
        return 
$this->start['memory'];
    }

    
/**
     * @return float
     */
    
public function get_end_time() {
        return 
$this->end['time'];
    }

    
/**
     * @return int
     */
    
public function get_end_memory() {
        return 
$this->end['memory'];
    }

    
/**
     * @return QM_Backtrace
     */
    
public function get_trace() {
        return 
$this->trace;
    }

    
/**
     * @param mixed[] $data
     * @return self
     */
    
public function end( ?array $data null ) {
        return 
$this->stop$data );
    }

}