Merge "Use the request object provided in User::setCookies"
[lhc/web/wiklou.git] / includes / profiler / output / ProfilerOutputDb.php
1 <?php
2 /**
3 * Profiler storing information in the DB.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Profiler
22 */
23
24 /**
25 * Logs profiling data into the local DB
26 *
27 * @ingroup Profiler
28 * @since 1.25
29 */
30 class ProfilerOutputDb extends ProfilerOutput {
31 /** @var bool Whether to store host data with profiling calls */
32 private $perHost = false;
33
34 public function __construct( Profiler $collector, array $params ) {
35 parent::__construct( $collector, $params );
36 global $wgProfilePerHost;
37
38 // Initialize per-host profiling from config, back-compat if available
39 if ( isset( $this->params['perHost'] ) ) {
40 $this->perHost = $this->params['perHost'];
41 } elseif ( $wgProfilePerHost ) {
42 $this->perHost = $wgProfilePerHost;
43 }
44 }
45
46 public function canUse() {
47 # Do not log anything if database is readonly (bug 5375)
48 return !wfReadOnly();
49 }
50
51 public function log( array $stats ) {
52 $pfhost = $this->perHost ? wfHostname() : '';
53
54 try {
55 $dbw = wfGetDB( DB_MASTER );
56 $useTrx = ( $dbw->getType() === 'sqlite' ); // much faster
57 if ( $useTrx ) {
58 $dbw->startAtomic( __METHOD__ );
59 }
60 foreach ( $stats as $data ) {
61 $name = $data['name'];
62 $eventCount = $data['calls'];
63 $timeSum = (float)$data['real'];
64 $memorySum = (float)$data['memory'];
65 $name = substr( $name, 0, 255 );
66
67 // Kludge
68 $timeSum = $timeSum >= 0 ? $timeSum : 0;
69 $memorySum = $memorySum >= 0 ? $memorySum : 0;
70
71 $dbw->upsert( 'profiling',
72 array(
73 'pf_name' => $name,
74 'pf_count' => $eventCount,
75 'pf_time' => $timeSum,
76 'pf_memory' => $memorySum,
77 'pf_server' => $pfhost
78 ),
79 array( array( 'pf_name', 'pf_server' ) ),
80 array(
81 "pf_count=pf_count+{$eventCount}",
82 "pf_time=pf_time+{$timeSum}",
83 "pf_memory=pf_memory+{$memorySum}",
84 ),
85 __METHOD__
86 );
87 }
88 if ( $useTrx ) {
89 $dbw->endAtomic( __METHOD__ );
90 }
91 } catch ( DBError $e ) {
92 }
93 }
94 }