Commit | Line | Data |
---|---|---|
6af80cae | 1 | <?php |
6af80cae EL |
2 | // This file is part of Moodle - http://moodle.org/ |
3 | // | |
4 | // Moodle is free software: you can redistribute it and/or modify | |
5 | // it under the terms of the GNU General Public License as published by | |
6 | // the Free Software Foundation, either version 3 of the License, or | |
7 | // (at your option) any later version. | |
8 | // | |
9 | // Moodle is distributed in the hope that it will be useful, | |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | // GNU General Public License for more details. | |
13 | // | |
14 | // You should have received a copy of the GNU General Public License | |
15 | // along with Moodle. If not, see <http://www.gnu.org/licenses/>. | |
16 | ||
17 | /** | |
187536f6 PS |
18 | * Profiling tool. |
19 | * | |
20 | * @package tool | |
6af80cae EL |
21 | * @subpackage profiling |
22 | * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com} | |
23 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
24 | */ | |
25 | ||
26 | // TODO: Move all the DB stuff to profiling_db_xxxx() function in xhprof_moodle.php | |
27 | ||
187536f6 PS |
28 | // TODO: it is wrong when core lib references ANY plugin lang strings, maybe more login could be moved here (skodak) |
29 | ||
30 | require_once(dirname(__FILE__) . '/../../../config.php'); | |
6af80cae EL |
31 | require_once($CFG->libdir.'/adminlib.php'); |
32 | require_once($CFG->libdir . '/xhprof/xhprof_moodle.php'); | |
33 | ||
34 | define('PROFILING_RUNSPERPAGE', 50); | |
35 | ||
36 | // page parameters | |
37 | $script = optional_param('script', null, PARAM_PATH); | |
38 | $runid = optional_param('runid', null, PARAM_ALPHANUM); | |
39 | $runid2 = optional_param('runid2', null, PARAM_ALPHANUM); | |
40 | $listurl = optional_param('listurl', null, PARAM_PATH); | |
41 | $runreference= optional_param('runreference', 0, PARAM_INT); | |
42 | $runcomment = optional_param('runcomment', null, PARAM_TEXT); | |
43 | ||
44 | $dbfields = 'runid, url, totalexecutiontime, totalcputime, ' . | |
45 | 'totalcalls, totalmemory, runreference, runcomment, timecreated'; | |
46 | ||
187536f6 | 47 | admin_externalpage_setup('toolprofiling'); |
6af80cae EL |
48 | |
49 | // Always add listurl if available | |
50 | if ($listurl) { | |
187536f6 | 51 | $listurlnav = new moodle_url('/admin/tool/profiling/index.php', array('listurl' => $listurl)); |
6af80cae EL |
52 | $PAGE->navbar->add($listurl, $listurlnav); |
53 | } | |
54 | ||
55 | // Header | |
56 | echo $OUTPUT->header(); | |
57 | ||
58 | // We have requested the last available run for one script | |
59 | if (isset($script)) { | |
60 | // Get the last available run for the given script | |
61 | $run = $DB->get_record_sql("SELECT $dbfields | |
62 | FROM {profiling} | |
63 | WHERE url = ? | |
64 | AND id = (SELECT MAX(id) | |
65 | FROM {profiling} | |
66 | WHERE url = ?)", | |
67 | array($script, $script), IGNORE_MISSING); | |
68 | ||
69 | // No run found for script, warn and exit | |
70 | if (!$run) { | |
187536f6 | 71 | notice(get_string('cannotfindanyrunforurl', 'tool_profiling', $script), 'index.php'); |
6af80cae EL |
72 | } |
73 | ||
74 | // Check if there is any previous run marked as reference one | |
75 | $prevreferences = $DB->get_records_select('profiling', | |
76 | 'url = ? AND runreference = 1 AND timecreated < ?', | |
77 | array($run->url, $run->timecreated), | |
78 | 'timecreated DESC', 'runid', 0, 1); | |
79 | $prevrunid = $prevreferences ? reset($prevreferences)->runid : false; | |
80 | echo $OUTPUT->box_start('generalbox boxwidthwide boxaligncenter'); | |
187536f6 | 81 | $header = get_string('lastrunof', 'tool_profiling', $script); |
6af80cae EL |
82 | echo $OUTPUT->heading($header); |
83 | $table = profiling_print_run($run, $prevrunid); | |
84 | echo $table; | |
85 | echo $OUTPUT->box_end(); | |
86 | ||
87 | ||
88 | // We have requested the diff between 2 runs | |
89 | } else if (isset($runid) && isset($runid2)) { | |
90 | $run1 = $DB->get_record('profiling', array('runid'=>$runid), $dbfields, MUST_EXIST); | |
91 | $run2 = $DB->get_record('profiling', array('runid'=>$runid2), $dbfields, MUST_EXIST); | |
92 | if ($run1->url == $run2->url && $run1->runid != $run2->runid) { | |
93 | if ($run2->timecreated < $run1->timecreated) { | |
94 | $runtemp = $run1; | |
95 | $run1 = $run2; | |
96 | $run2 = $runtemp; | |
97 | } | |
98 | echo $OUTPUT->box_start('generalbox boxwidthwide boxaligncenter'); | |
187536f6 | 99 | $header = get_string('differencesbetween2runsof', 'tool_profiling', $run1->url); |
6af80cae EL |
100 | echo $OUTPUT->heading($header); |
101 | $table = profiling_print_rundiff($run1, $run2); | |
102 | echo $table; | |
103 | echo $OUTPUT->box_end(); | |
104 | } | |
105 | ||
106 | ||
107 | // We have requested one run, invoke it | |
108 | } else if (isset($runid)) { | |
109 | // Check if we are trying to update the runreference/runcomment for the run | |
110 | if (isset($runcomment) && confirm_sesskey()) { | |
111 | $id = $DB->get_field('profiling', 'id', array('runid' => $runid), MUST_EXIST); | |
112 | $rec = new stdClass(); | |
113 | $rec->id = $id; | |
114 | $rec->runreference = (bool)$runreference; | |
115 | $rec->runcomment = $runcomment; | |
116 | $DB->update_record('profiling', $rec); | |
117 | } | |
118 | // Get the requested runid | |
119 | $run = $DB->get_record('profiling', array('runid'=>$runid), $dbfields, IGNORE_MISSING); | |
120 | ||
121 | // No run found for runid, warn and exit | |
122 | if (!$run) { | |
187536f6 | 123 | notice(get_string('cannotfindanyrunforrunid', 'tool_profiling', $runid), 'index.php'); |
6af80cae EL |
124 | } |
125 | ||
126 | // Check if there is any previous run marked as reference one | |
127 | $prevreferences = $DB->get_records_select('profiling', | |
128 | 'url = ? AND runreference = 1 AND timecreated < ?', | |
129 | array($run->url, $run->timecreated), | |
130 | 'timecreated DESC', 'runid', 0, 1); | |
131 | $prevrunid = $prevreferences ? reset($prevreferences)->runid : false; | |
132 | echo $OUTPUT->box_start('generalbox boxwidthwide boxaligncenter'); | |
187536f6 | 133 | $header = get_string('summaryof', 'tool_profiling', $run->url); |
6af80cae EL |
134 | echo $OUTPUT->heading($header); |
135 | $table = profiling_print_run($run, $prevrunid); | |
136 | echo $table; | |
137 | echo $OUTPUT->box_end(); | |
138 | ||
139 | ||
140 | // Default: List one page of runs | |
141 | } else { | |
142 | ||
143 | // The flexitable that will root listings | |
144 | $table = new xhprof_table_sql('profiling-list-table'); | |
187536f6 | 145 | $baseurl = $CFG->wwwroot . '/admin/tool/profiling/index.php'; |
6af80cae EL |
146 | |
147 | // Check if we are listing all or some URL ones | |
148 | $sqlconditions = ''; | |
149 | $sqlparams = array(); | |
150 | if (!isset($listurl)) { | |
187536f6 | 151 | $header = get_string('pluginname', 'tool_profiling'); |
6af80cae EL |
152 | $sqlconditions = '1 = 1'; |
153 | $table->set_listurlmode(false); | |
154 | } else { | |
187536f6 | 155 | $header = get_string('profilingrunsfor', 'tool_profiling', $listurl); |
6af80cae EL |
156 | $sqlconditions = 'url = :url'; |
157 | $sqlparams['url'] = $listurl; | |
158 | $table->set_listurlmode(true); | |
159 | $baseurl .= '?listurl=' . urlencode($listurl); | |
160 | } | |
161 | ||
162 | echo $OUTPUT->heading($header); | |
163 | ||
164 | // TODO: Fix flexitable to validate tsort/thide/tshow/tifirs/tilast/page | |
165 | // TODO: Fix table_sql to allow it to work without WHERE clause | |
166 | // add silly condition (1 = 1) because of table_sql bug | |
167 | $table->set_sql($dbfields, '{profiling}', $sqlconditions, $sqlparams); | |
168 | $table->set_count_sql("SELECT COUNT(*) FROM {profiling} WHERE $sqlconditions", $sqlparams); | |
169 | $columns = array( | |
170 | 'url', 'timecreated', 'totalexecutiontime', 'totalcputime', | |
171 | 'totalcalls', 'totalmemory', 'runcomment'); | |
172 | $headers = array( | |
187536f6 PS |
173 | get_string('url'), get_string('date'), get_string('executiontime', 'tool_profiling'), |
174 | get_string('cputime', 'tool_profiling'), get_string('calls', 'tool_profiling'), | |
175 | get_string('memory', 'tool_profiling'), get_string('comment', 'tool_profiling')); | |
6af80cae EL |
176 | $table->define_columns($columns); |
177 | $table->define_headers($headers); | |
178 | $table->sortable(true, 'timecreated', SORT_DESC); | |
179 | $table->define_baseurl($baseurl); | |
180 | $table->column_suppress('url'); | |
181 | $table->out(PROFILING_RUNSPERPAGE, true); | |
182 | ||
183 | // Print the controller block with different options | |
184 | echo profiling_list_controls($listurl); | |
185 | } | |
186 | ||
187 | // Footer. | |
188 | echo $OUTPUT->footer(); | |
189 |