3 * @defgroup StatCounter StatCounter
5 * StatCounter is used to increment arbitrary keys for profiling reasons.
6 * The key/values are persisted in several possible ways (see $wgStatsMethod).
10 * Aggregator for wfIncrStats() that batches updates per request.
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.
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.
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
28 * @ingroup StatCounter
32 * Aggregator for wfIncrStats() that batches updates per request.
33 * This avoids spamming the collector many times for the same key.
35 * @ingroup StatCounter
39 protected $deltas = array(); // (key => count)
41 protected function __construct() {}
43 public static function singleton() {
44 static $instance = null;
46 $instance = new self();
52 * Increment a key by delta $count
55 * @param integer $count
58 public function incr( $key, $count = 1 ) {
59 if ( PHP_SAPI
=== 'cli' ) {
60 $this->sendDelta( $key, $count );
62 if ( !isset( $this->deltas
[$key] ) ) {
63 $this->deltas
[$key] = 0;
65 $this->deltas
[$key] +
= $count;
70 * Flush all pending deltas to persistent storage
74 public function flush() {
76 foreach ( $this->deltas
as $key => $count ) {
77 $this->sendDelta( $key, $count );
79 } catch ( MWException
$e ) {
80 trigger_error( "Caught exception: {$e->getMessage()}");
82 $this->deltas
= array();
87 * @param string $count
90 protected function sendDelta( $key, $count ) {
91 global $wgStatsMethod;
93 $count = intval( $count );
98 if ( $wgStatsMethod == 'udp' ) {
99 global $wgUDPProfilerHost, $wgUDPProfilerPort, $wgAggregateStatsID;
102 $id = $wgAggregateStatsID !== false ?
$wgAggregateStatsID : wfWikiID();
105 $socket = socket_create( AF_INET
, SOCK_DGRAM
, SOL_UDP
);
106 $statline = "stats/{$id} - 1 1 1 1 1 -total\n";
116 $statline = "stats/{$id} - {$count} 1 1 1 1 {$key}\n";
117 wfSuppressWarnings();
127 } elseif ( $wgStatsMethod == 'cache' ) {
129 $key = wfMemcKey( 'stats', $key );
130 if ( is_null( $wgMemc->incr( $key, $count ) ) ) {
131 $wgMemc->add( $key, $count );