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