Localisation updates from https://translatewiki.net.
[lhc/web/wiklou.git] / includes / StatCounter.php
1 <?php
2 /**
3 * @defgroup StatCounter StatCounter
4 *
5 * StatCounter is used to increment arbitrary keys for profiling reasons.
6 * The key/values are persisted in several possible ways (see $wgStatsMethod).
7 */
8
9 /**
10 * Aggregator for wfIncrStats() that batches updates per request.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 * http://www.gnu.org/copyleft/gpl.html
26 *
27 * @file
28 * @ingroup StatCounter
29 * @author Aaron Schulz
30 */
31
32 /**
33 * Aggregator for wfIncrStats() that batches updates per request.
34 * This avoids spamming the collector many times for the same key.
35 *
36 * @ingroup StatCounter
37 */
38 class StatCounter {
39 /** @var array */
40 protected $deltas = array(); // (key => count)
41
42 protected function __construct() {
43 }
44
45 /**
46 * @return StatCounter
47 */
48 public static function singleton() {
49 static $instance = null;
50 if ( !$instance ) {
51 $instance = new self();
52 }
53 return $instance;
54 }
55
56 /**
57 * Increment a key by delta $count
58 *
59 * @param string $key
60 * @param int $count
61 * @return void
62 */
63 public function incr( $key, $count = 1 ) {
64 $this->deltas[$key] = isset( $this->deltas[$key] ) ? $this->deltas[$key] : 0;
65 $this->deltas[$key] += $count;
66 if ( PHP_SAPI === 'cli' ) {
67 $this->flush();
68 }
69 }
70
71 /**
72 * Flush all pending deltas to persistent storage
73 *
74 * @return void
75 */
76 public function flush() {
77 global $wgStatsMethod;
78
79 $deltas = array_filter( $this->deltas ); // remove 0 valued entries
80 if ( $wgStatsMethod === 'udp' ) {
81 $this->sendDeltasUDP( $deltas );
82 } elseif ( $wgStatsMethod === 'cache' ) {
83 $this->sendDeltasMemc( $deltas );
84 } else {
85 // disabled
86 }
87 $this->deltas = array();
88 }
89
90 /**
91 * @param array $deltas
92 * @return void
93 */
94 protected function sendDeltasUDP( array $deltas ) {
95 global $wgUDPProfilerHost, $wgUDPProfilerPort, $wgAggregateStatsID,
96 $wgStatsFormatString;
97
98 $id = strlen( $wgAggregateStatsID ) ? $wgAggregateStatsID : wfWikiID();
99
100 $lines = array();
101 foreach ( $deltas as $key => $count ) {
102 $lines[] = sprintf( $wgStatsFormatString, $id, $count, $key );
103 }
104
105 if ( count( $lines ) ) {
106 static $socket = null;
107 if ( !$socket ) {
108 $socket = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
109 }
110 $packet = '';
111 $packets = array();
112 foreach ( $lines as $line ) {
113 if ( ( strlen( $packet ) + strlen( $line ) ) > 1450 ) {
114 $packets[] = $packet;
115 $packet = '';
116 }
117 $packet .= $line;
118 }
119 if ( $packet != '' ) {
120 $packets[] = $packet;
121 }
122 foreach ( $packets as $packet ) {
123 wfSuppressWarnings();
124 socket_sendto(
125 $socket,
126 $packet,
127 strlen( $packet ),
128 0,
129 $wgUDPProfilerHost,
130 $wgUDPProfilerPort
131 );
132 wfRestoreWarnings();
133 }
134 }
135 }
136
137 /**
138 * @param array $deltas
139 * @return void
140 */
141 protected function sendDeltasMemc( array $deltas ) {
142 global $wgMemc;
143
144 foreach ( $deltas as $key => $count ) {
145 $ckey = wfMemcKey( 'stats', $key );
146 if ( $wgMemc->incr( $ckey, $count ) === null ) {
147 $wgMemc->add( $ckey, $count );
148 }
149 }
150 }
151 }