|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class Jetpack_Search_Performance_Logger { |
|
4
|
|
|
/** |
|
5
|
|
|
* @var Jetpack_Search_Performance_Logger |
|
6
|
|
|
**/ |
|
7
|
|
|
private static $instance = null; |
|
8
|
|
|
|
|
9
|
|
|
private $current_query = null; |
|
10
|
|
|
private $query_started = null; |
|
11
|
|
|
private $stats = null; |
|
12
|
|
|
|
|
13
|
|
|
static function init() { |
|
14
|
|
|
if ( is_null( self::$instance ) ) { |
|
15
|
|
|
self::$instance = new Jetpack_Search_Performance_Logger; |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
return self::$instance; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
private function __construct() { |
|
22
|
|
|
$this->stats = array(); |
|
23
|
|
|
add_action( 'pre_get_posts', array( $this, 'begin_log_query' ), 10, 1 ); |
|
24
|
|
|
add_action( 'did_jetpack_search_query', array( $this, 'log_jetpack_search_query' ) ); |
|
25
|
|
|
add_filter( 'found_posts', array( $this, 'log_mysql_query' ), 10, 2 ); |
|
26
|
|
|
add_action( 'wp_footer', array( $this, 'print_stats' ) ); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function begin_log_query( $query ) { |
|
30
|
|
|
if ( $this->should_log_query( $query ) ) { |
|
31
|
|
|
$this->query_started = microtime( true ); |
|
32
|
|
|
$this->current_query = $query; |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function log_mysql_query( $found_posts, $query ) { |
|
37
|
|
|
if ( $this->current_query === $query ) { |
|
38
|
|
|
$duration = microtime( true ) - $this->query_started; |
|
39
|
|
|
$this->record_query_time( $duration, false ); |
|
40
|
|
|
$this->reset_query_state(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return $found_posts; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function log_jetpack_search_query() { |
|
47
|
|
|
$duration = microtime( true ) - $this->query_started; |
|
48
|
|
|
$this->record_query_time( $duration, true ); |
|
49
|
|
|
$this->reset_query_state(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
private function reset_query_state() { |
|
53
|
|
|
$this->query_started = null; |
|
54
|
|
|
$this->current_query = null; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
private function should_log_query( $query ) { |
|
58
|
|
|
return $query->is_main_query() && $query->is_search(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
private function record_query_time( $duration, $was_jetpack_search ) { |
|
62
|
|
|
$this->stats[] = array( $was_jetpack_search, intval( $duration * 1000 ) ); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function print_stats() { |
|
66
|
|
|
$beacons = array(); |
|
67
|
|
|
if ( ! empty( $this->stats ) ) { |
|
68
|
|
|
foreach( $this->stats as $stat ) { |
|
69
|
|
|
$search_type = $stat[0] ? 'es' : 'mysql'; |
|
70
|
|
|
$beacons[] = "%22jetpack.search.{$search_type}.duration:{$stat[1]}|ms%22"; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$encoded_json = '{%22beacons%22:[' . implode(',', $beacons ) . ']}'; |
|
74
|
|
|
$encoded_site_url = urlencode( site_url() ); |
|
75
|
|
|
$url = "https://pixel.wp.com/boom.gif?v=0.9&u={$encoded_site_url}&json={$encoded_json}"; |
|
76
|
|
|
echo '<img src="' . $url . '" width="1" height="1" style="display:none;" />'; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
} |