/var/www/html_nl/wp-content/plugins/query-monitor/collectors/timing.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
<?php declare(strict_types 1);
/**
 * Timing and profiling collector.
 *
 * @package query-monitor
 */

if ( ! defined'ABSPATH' ) ) {
    exit;
}

/**
 * @extends QM_DataCollector<QM_Data_Timing>
 */
class QM_Collector_Timing extends QM_DataCollector {

    
/**
     * @var string
     */
    
public $id 'timing';

    
/**
     * @var array<string, QM_Timer>
     */
    
private $track_timer = array();

    
/**
     * @var array<string, QM_Timer>
     */
    
private $start = array();

    
/**
     * @var array<string, QM_Timer>
     */
    
private $stop = array();

    public function 
get_storage(): QM_Data {
        return new 
QM_Data_Timing();
    }

    
/**
     * @return void
     */
    
public function set_up() {
        
parent::set_up();

        
add_action'qm/start', array( $this'action_function_time_start' ), 10);
        
add_action'qm/stop', array( $this'action_function_time_stop' ), 10);
        
add_action'qm/lap', array( $this'action_function_time_lap' ), 10);
    }

    
/**
     * @return void
     */
    
public function tear_down() {
        
remove_action'qm/start', array( $this'action_function_time_start' ), 10 );
        
remove_action'qm/stop', array( $this'action_function_time_stop' ), 10 );
        
remove_action'qm/lap', array( $this'action_function_time_lap' ), 10 );

        
parent::tear_down();
    }

    
/**
     * @param string $function
     * @return void
     */
    
public function action_function_time_start$function ) {
        
$this->track_timer$function ] = new QM_Timer();
        
$this->start$function ] = $this->track_timer$function ]->start();
    }

    
/**
     * @param string $function
     * @return void
     */
    
public function action_function_time_stop$function ) {
        if ( ! isset( 
$this->track_timer$function ] ) ) {
            
$trace = new QM_Backtrace();
            
$this->data->warning[] = array(
                
'function' => $function,
                
'message' => __'Timer not started''query-monitor' ),
                
'filtered_trace' => $trace->get_filtered_trace(),
                
'component' => $trace->get_component(),
            );
            return;
        }
        
$this->stop$function ] = $this->track_timer$function ]->stop();
        
$this->calculate_time$function );
    }

    
/**
     * @param string $function
     * @param string $name
     * @return void
     */
    
public function action_function_time_lap$function$name null ) {
        if ( ! isset( 
$this->track_timer$function ] ) ) {
            
$trace = new QM_Backtrace();
            
$this->data->warning[] = array(
                
'function' => $function,
                
'message' => __'Timer not started''query-monitor' ),
                
'filtered_trace' => $trace->get_filtered_trace(),
                
'component' => $trace->get_component(),
            );
            return;
        }
        
$this->track_timer$function ]->lap( array(), $name );
    }

    
/**
     * @param string $function
     * @return void
     */
    
public function calculate_time$function ) {
        
$trace $this->track_timer$function ]->get_trace();
        
$function_time $this->track_timer$function ]->get_time();
        
$function_memory $this->track_timer$function ]->get_memory();
        
$function_laps $this->track_timer$function ]->get_laps();
        
$start_time $this->track_timer$function ]->get_start_time();
        
$end_time $this->track_timer$function ]->get_end_time();

        
$this->data->timing[] = array(
            
'function' => $function,
            
'function_time' => $function_time,
            
'function_memory' => $function_memory,
            
'laps' => $function_laps,
            
'filtered_trace' => $trace->get_filtered_trace(),
            
'component' => $trace->get_component(),
            
'start_time' => ( $start_time $_SERVER['REQUEST_TIME_FLOAT'] ),
            
'end_time' => ( $end_time $_SERVER['REQUEST_TIME_FLOAT'] ),
        );
    }

    
/**
     * @return void
     */
    
public function process() {
        foreach ( 
$this->start as $function => $value ) {
            if ( ! isset( 
$this->stop$function ] ) ) {
                
$trace $this->track_timer$function ]->get_trace();
                
$this->data->warning[] = array(
                    
'function' => $function,
                    
'message' => __'Timer not stopped''query-monitor' ),
                    
'filtered_trace' => $trace->get_filtered_trace(),
                    
'component' => $trace->get_component(),
                );
            }
        }

        if ( ! empty( 
$this->data->timing ) ) {
            
usort$this->data->timing, array( $this'sort_by_start_time' ) );
        }
    }

    
/**
     * @param mixed[] $a
     * @param mixed[] $b
     * @return int
     * @phpstan-return -1|0|1
     */
    
public function sort_by_start_time( array $a, array $b ) {
        return 
$a['start_time'] <=> $b['start_time'];
    }

}

# Load early in case a plugin is setting the function to be checked when it initialises instead of after the `plugins_loaded` hook
QM_Collectors::add( new QM_Collector_Timing() );