Revert "jquery.textSelection: Remove hardcoded checks for removed WikiEditor iframe...
[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 * @return StatCounter
46 */
47 public static function singleton() {
48 static $instance = null;
49 if ( !$instance ) {
50 $instance = new self();
51 }
52 return $instance;
53 }
54
55 /**
56 * Increment a key by delta $count
57 *
58 * @param string $key
59 * @param int $count
60 * @return void
61 */
62 public function incr( $key, $count = 1 ) {
63 $this->deltas[$key] = isset( $this->deltas[$key] ) ? $this->deltas[$key] : 0;
64 $this->deltas[$key] += $count;
65 if ( PHP_SAPI === 'cli' ) {
66 $this->flush();
67 }
68 }
69
70 /**
71 * Flush all pending deltas to persistent storage
72 *
73 * @return void
74 */
75 public function flush() {
76 global $wgStatsMethod;
77
78 $deltas = array_filter( $this->deltas ); // remove 0 valued entries
79 if ( $wgStatsMethod === 'udp' ) {
80 $this->sendDeltasUDP( $deltas );
81 } elseif ( $wgStatsMethod === 'cache' ) {
82 $this->sendDeltasMemc( $deltas );
83 } else {
84 // disabled
85 }
86 $this->deltas = array();
87 }
88
89 /**
90 * @param array $deltas
91 * @return void
92 */
93 protected function sendDeltasUDP( array $deltas ) {
94 global $wgUDPProfilerHost, $wgUDPProfilerPort, $wgAggregateStatsID,
95 $wgStatsFormatString;
96
97 $id = strlen( $wgAggregateStatsID ) ? $wgAggregateStatsID : wfWikiID();
98
99 $lines = array();
100 foreach ( $deltas as $key => $count ) {
101 $lines[] = sprintf( $wgStatsFormatString, $id, $count, $key );
102 }
103
104 if ( count( $lines ) ) {
105 static $socket = null;
106 if ( !$socket ) {
107 $socket = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
108 }
109 $packet = '';
110 $packets = array();
111 foreach ( $lines as $line ) {
112 if ( ( strlen( $packet ) + strlen( $line ) ) > 1450 ) {
113 $packets[] = $packet;
114 $packet = '';
115 }
116 $packet .= $line;
117 }
118 if ( $packet != '' ) {
119 $packets[] = $packet;
120 }
121 foreach ( $packets as $packet ) {
122 wfSuppressWarnings();
123 socket_sendto(
124 $socket,
125 $packet,
126 strlen( $packet ),
127 0,
128 $wgUDPProfilerHost,
129 $wgUDPProfilerPort
130 );
131 wfRestoreWarnings();
132 }
133 }
134 }
135
136 /**
137 * @param array $deltas
138 * @return void
139 */
140 protected function sendDeltasMemc( array $deltas ) {
141 global $wgMemc;
142
143 foreach ( $deltas as $key => $count ) {
144 $ckey = wfMemcKey( 'stats', $key );
145 if ( $wgMemc->incr( $ckey, $count ) === null ) {
146 $wgMemc->add( $ckey, $count );
147 }
148 }
149 }
150 }