Merge "ProfilerOutput: Remove logStandardData() and make log() abstract"
[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 * $wgProfiler['class'] = 'ProfilerSimpleDB';
28 *
29 * @ingroup Profiler
30 * @since 1.25
31 */
32 class ProfilerOutputDb extends ProfilerOutput {
33 public function canUse() {
34 # Do not log anything if database is readonly (bug 5375)
35 return !wfReadOnly();
36 }
37
38 public function log( array $stats ) {
39 global $wgProfilePerHost;
40
41 if ( $wgProfilePerHost ) {
42 $pfhost = wfHostname();
43 } else {
44 $pfhost = '';
45 }
46
47 try {
48 $dbw = wfGetDB( DB_MASTER );
49 $useTrx = ( $dbw->getType() === 'sqlite' ); // much faster
50 if ( $useTrx ) {
51 $dbw->startAtomic( __METHOD__ );
52 }
53 foreach ( $stats as $data ) {
54 $name = $data['name'];
55 $eventCount = $data['calls'];
56 $timeSum = (float)$data['real'];
57 $memorySum = (float)$data['memory'];
58 $name = substr( $name, 0, 255 );
59
60 // Kludge
61 $timeSum = $timeSum >= 0 ? $timeSum : 0;
62 $memorySum = $memorySum >= 0 ? $memorySum : 0;
63
64 $dbw->update( 'profiling',
65 array(
66 "pf_count=pf_count+{$eventCount}",
67 "pf_time=pf_time+{$timeSum}",
68 "pf_memory=pf_memory+{$memorySum}",
69 ),
70 array(
71 'pf_name' => $name,
72 'pf_server' => $pfhost,
73 ),
74 __METHOD__ );
75
76 $rc = $dbw->affectedRows();
77 if ( $rc == 0 ) {
78 $dbw->insert( 'profiling',
79 array(
80 'pf_name' => $name,
81 'pf_count' => $eventCount,
82 'pf_time' => $timeSum,
83 'pf_memory' => $memorySum,
84 'pf_server' => $pfhost
85 ),
86 __METHOD__,
87 array( 'IGNORE' )
88 );
89 }
90 // When we upgrade to mysql 4.1, the insert+update
91 // can be merged into just a insert with this construct added:
92 // "ON DUPLICATE KEY UPDATE ".
93 // "pf_count=pf_count + VALUES(pf_count), ".
94 // "pf_time=pf_time + VALUES(pf_time)";
95 }
96 if ( $useTrx ) {
97 $dbw->endAtomic( __METHOD__ );
98 }
99 } catch ( DBError $e ) {
100 }
101 }
102 }