Merge "add htmlform-no and htmlform-yes messages for generic yes-no questions"
[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 */
30
31 /**
32 * Aggregator for wfIncrStats() that batches updates per request.
33 * This avoids spamming the collector many times for the same key.
34 *
35 * @ingroup StatCounter
36 */
37 class StatCounter {
38 /** @var Array */
39 protected $deltas = array(); // (key => count)
40
41 protected function __construct() {}
42
43 /**
44 * @return StatCounter
45 */
46 public static function singleton() {
47 static $instance = null;
48 if ( !$instance ) {
49 $instance = new self();
50 }
51 return $instance;
52 }
53
54 /**
55 * Increment a key by delta $count
56 *
57 * @param string $key
58 * @param integer $count
59 * @return void
60 */
61 public function incr( $key, $count = 1 ) {
62 $this->deltas[$key] = isset( $this->deltas[$key] ) ? $this->deltas[$key] : 0;
63 $this->deltas[$key] += $count;
64 if ( PHP_SAPI === 'cli' ) {
65 $this->flush();
66 }
67 }
68
69 /**
70 * Flush all pending deltas to persistent storage
71 *
72 * @return void
73 */
74 public function flush() {
75 global $wgStatsMethod;
76
77 $deltas = array_filter( $this->deltas ); // remove 0 valued entries
78 if ( $wgStatsMethod === 'udp' ) {
79 $this->sendDeltasUDP( $deltas );
80 } elseif ( $wgStatsMethod === 'cache' ) {
81 $this->sendDeltasMemc( $deltas );
82 } else {
83 // disabled
84 }
85 $this->deltas = array();
86 }
87
88 protected function sendDeltasUDP( array $deltas ) {
89 global $wgUDPProfilerHost, $wgUDPProfilerPort, $wgAggregateStatsID;
90
91 $id = strlen( $wgAggregateStatsID ) ? $wgAggregateStatsID : wfWikiID();
92
93 $lines = array();
94 foreach ( $deltas as $key => $count ) {
95 $lines[] = "stats/{$id} - {$count} 1 1 1 1 {$key}\n";
96 }
97
98 if ( count( $lines ) ) {
99 static $socket = null;
100 if ( !$socket ) {
101 $socket = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
102 array_unshift( $lines, "stats/{$id} - 1 1 1 1 1 -total\n" );
103 }
104 $packet = '';
105 $packets = array();
106 foreach ( $lines as $line ) {
107 if ( ( strlen( $packet ) + strlen( $line ) ) > 1450 ) {
108 $packets[] = $packet;
109 $packet = '';
110 }
111 $packet .= $line;
112 }
113 if ( $packet != '' ) {
114 $packets[] = $packet;
115 }
116 foreach ( $packets as $packet ) {
117 wfSuppressWarnings();
118 socket_sendto(
119 $socket,
120 $packet,
121 strlen( $packet ),
122 0,
123 $wgUDPProfilerHost,
124 $wgUDPProfilerPort
125 );
126 wfRestoreWarnings();
127 }
128 }
129 }
130
131 protected function sendDeltasMemc( array $deltas ) {
132 global $wgMemc;
133
134 foreach ( $deltas as $key => $count ) {
135 $ckey = wfMemcKey( 'stats', $key );
136 if ( $wgMemc->incr( $ckey, $count ) === null ) {
137 $wgMemc->add( $ckey, $count );
138 }
139 }
140 }
141 }