Skip to content

Commit

Permalink
Merge branch 'xhprof' of git://github.com/stronk7/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Dec 20, 2010
2 parents dde5bfb + 6af80ca commit ab12793
Show file tree
Hide file tree
Showing 36 changed files with 9,587 additions and 7 deletions.
186 changes: 186 additions & 0 deletions admin/report/profiling/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* @package core
* @subpackage profiling
* @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

// TODO: Move all the DB stuff to profiling_db_xxxx() function in xhprof_moodle.php

require_once(dirname(__FILE__).'/../../../config.php');
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->libdir . '/xhprof/xhprof_moodle.php');

define('PROFILING_RUNSPERPAGE', 50);

// page parameters
$script = optional_param('script', null, PARAM_PATH);
$runid = optional_param('runid', null, PARAM_ALPHANUM);
$runid2 = optional_param('runid2', null, PARAM_ALPHANUM);
$listurl = optional_param('listurl', null, PARAM_PATH);
$runreference= optional_param('runreference', 0, PARAM_INT);
$runcomment = optional_param('runcomment', null, PARAM_TEXT);

$dbfields = 'runid, url, totalexecutiontime, totalcputime, ' .
'totalcalls, totalmemory, runreference, runcomment, timecreated';

admin_externalpage_setup('reportprofiling');

// Always add listurl if available
if ($listurl) {
$listurlnav = new moodle_url('/admin/report/profiling/index.php', array('listurl' => $listurl));
$PAGE->navbar->add($listurl, $listurlnav);
}

// Header
echo $OUTPUT->header();

// We have requested the last available run for one script
if (isset($script)) {
// Get the last available run for the given script
$run = $DB->get_record_sql("SELECT $dbfields
FROM {profiling}
WHERE url = ?
AND id = (SELECT MAX(id)
FROM {profiling}
WHERE url = ?)",
array($script, $script), IGNORE_MISSING);

// No run found for script, warn and exit
if (!$run) {
notice(get_string('cannotfindanyrunforurl', 'report_profiling', $script), 'index.php');
}

// Check if there is any previous run marked as reference one
$prevreferences = $DB->get_records_select('profiling',
'url = ? AND runreference = 1 AND timecreated < ?',
array($run->url, $run->timecreated),
'timecreated DESC', 'runid', 0, 1);
$prevrunid = $prevreferences ? reset($prevreferences)->runid : false;
echo $OUTPUT->box_start('generalbox boxwidthwide boxaligncenter');
$header = get_string('lastrunof', 'report_profiling', $script);
echo $OUTPUT->heading($header);
$table = profiling_print_run($run, $prevrunid);
echo $table;
echo $OUTPUT->box_end();


// We have requested the diff between 2 runs
} else if (isset($runid) && isset($runid2)) {
$run1 = $DB->get_record('profiling', array('runid'=>$runid), $dbfields, MUST_EXIST);
$run2 = $DB->get_record('profiling', array('runid'=>$runid2), $dbfields, MUST_EXIST);
if ($run1->url == $run2->url && $run1->runid != $run2->runid) {
if ($run2->timecreated < $run1->timecreated) {
$runtemp = $run1;
$run1 = $run2;
$run2 = $runtemp;
}
echo $OUTPUT->box_start('generalbox boxwidthwide boxaligncenter');
$header = get_string('differencesbetween2runsof', 'report_profiling', $run1->url);
echo $OUTPUT->heading($header);
$table = profiling_print_rundiff($run1, $run2);
echo $table;
echo $OUTPUT->box_end();
}


// We have requested one run, invoke it
} else if (isset($runid)) {
// Check if we are trying to update the runreference/runcomment for the run
if (isset($runcomment) && confirm_sesskey()) {
$id = $DB->get_field('profiling', 'id', array('runid' => $runid), MUST_EXIST);
$rec = new stdClass();
$rec->id = $id;
$rec->runreference = (bool)$runreference;
$rec->runcomment = $runcomment;
$DB->update_record('profiling', $rec);
}
// Get the requested runid
$run = $DB->get_record('profiling', array('runid'=>$runid), $dbfields, IGNORE_MISSING);

// No run found for runid, warn and exit
if (!$run) {
notice(get_string('cannotfindanyrunforrunid', 'report_profiling', $runid), 'index.php');
}

// Check if there is any previous run marked as reference one
$prevreferences = $DB->get_records_select('profiling',
'url = ? AND runreference = 1 AND timecreated < ?',
array($run->url, $run->timecreated),
'timecreated DESC', 'runid', 0, 1);
$prevrunid = $prevreferences ? reset($prevreferences)->runid : false;
echo $OUTPUT->box_start('generalbox boxwidthwide boxaligncenter');
$header = get_string('summaryof', 'report_profiling', $run->url);
echo $OUTPUT->heading($header);
$table = profiling_print_run($run, $prevrunid);
echo $table;
echo $OUTPUT->box_end();


// Default: List one page of runs
} else {

// The flexitable that will root listings
$table = new xhprof_table_sql('profiling-list-table');
$baseurl = $CFG->wwwroot . '/admin/report/profiling/index.php';

// Check if we are listing all or some URL ones
$sqlconditions = '';
$sqlparams = array();
if (!isset($listurl)) {
$header = get_string('pluginname', 'report_profiling');
$sqlconditions = '1 = 1';
$table->set_listurlmode(false);
} else {
$header = get_string('profilingrunsfor', 'report_profiling', $listurl);
$sqlconditions = 'url = :url';
$sqlparams['url'] = $listurl;
$table->set_listurlmode(true);
$baseurl .= '?listurl=' . urlencode($listurl);
}

echo $OUTPUT->heading($header);

// TODO: Fix flexitable to validate tsort/thide/tshow/tifirs/tilast/page
// TODO: Fix table_sql to allow it to work without WHERE clause
// add silly condition (1 = 1) because of table_sql bug
$table->set_sql($dbfields, '{profiling}', $sqlconditions, $sqlparams);
$table->set_count_sql("SELECT COUNT(*) FROM {profiling} WHERE $sqlconditions", $sqlparams);
$columns = array(
'url', 'timecreated', 'totalexecutiontime', 'totalcputime',
'totalcalls', 'totalmemory', 'runcomment');
$headers = array(
get_string('url'), get_string('date'), get_string('executiontime', 'report_profiling'),
get_string('cputime', 'report_profiling'), get_string('calls', 'report_profiling'),
get_string('memory', 'report_profiling'), get_string('comment', 'report_profiling'));
$table->define_columns($columns);
$table->define_headers($headers);
$table->sortable(true, 'timecreated', SORT_DESC);
$table->define_baseurl($baseurl);
$table->column_suppress('url');
$table->out(PROFILING_RUNSPERPAGE, true);

// Print the controller block with different options
echo profiling_list_controls($listurl);
}

// Footer.
echo $OUTPUT->footer();

45 changes: 45 additions & 0 deletions admin/report/profiling/lang/en/report_profiling.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Strings for component 'report_profiling', language 'en', branch 'MOODLE_20_STABLE'
*
* @package report_profiling
* @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

$string['calls'] = 'Function calls';
$string['cannotfindanyrunforurl'] = 'Sorry, cannot find any profiling run for the \'{$a}\' URL';
$string['cannotfindanyrunforrunid'] = 'Sorry, cannot find the \'{$a}\' profiling run';
$string['comment'] = 'Comment';
$string['differencesbetween2runsof'] = 'Differences between 2 runs of {$a}';
$string['executiontime'] = 'Execution time';
$string['cputime'] = 'CPU time';
$string['lastrunof'] = 'Summary of last run of {$a}';
$string['markreferencerun'] = 'Mark as reference run/comment';
$string['memory'] = 'Memory used';
$string['pluginname'] = 'Profiling runs';
$string['profilingfocusscript'] = 'Focus on profiling runs for the script: {$a}';
$string['profilingruns'] = 'Profiling runs';
$string['profilingrunsfor'] = 'Profiling runs for {$a}';
$string['referencerun'] = 'Reference run/comment';
$string['runid'] = 'Run ID';
$string['summaryof'] = 'Summary of {$a}';
$string['viewdetails'] = 'View profiling details';
$string['viewdiff'] = 'View profiling differences with last reference run';
$string['viewdiffdetails'] = 'View profiling diff details';
8 changes: 8 additions & 0 deletions admin/report/profiling/settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

defined('MOODLE_INTERNAL') || die;

// profiling report, added to development
if (extension_loaded('xhprof') && function_exists('xhprof_enable') && !empty($CFG->profilingenabled)) {
$ADMIN->add('development', new admin_externalpage('reportprofiling', get_string('pluginname', 'report_profiling'), "$CFG->wwwroot/$CFG->admin/report/profiling/index.php", 'moodle/site:config'));
}
25 changes: 25 additions & 0 deletions admin/report/profiling/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* report_profiling styles */

.path-admin-report-profiling .profilingruntable .label {
font-weight: bold;
}
.path-admin-report-profiling .profiling_worse {
color: red;
}
.path-admin-report-profiling .profiling_better {
color: green;
}
.path-admin-report-profiling .profiling_same {
color: dimgrey;
}
.path-admin-report-profiling .profiling_important,
.path-admin-report-profiling .flexible .referencerun {
font-weight: bold;
}
.path-admin-report-profiling .flexible .r1 .cell {
background-color: whitesmoke;
}
.path-admin-report-profiling .flexible {
margin-left:auto;
margin-right:auto
}
38 changes: 38 additions & 0 deletions admin/settings/development.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,44 @@
$temp->add(new admin_setting_configcheckbox('debugpageinfo', get_string('debugpageinfo', 'admin'), get_string('configdebugpageinfo', 'admin'), 0));
$ADMIN->add('development', $temp);

// "profiling" settingpage (conditionally if the 'xhprof' extension is available only)
if (extension_loaded('xhprof') && function_exists('xhprof_enable')) {
$temp = new admin_settingpage('profiling', get_string('profiling', 'admin'));
// Main profiling switch
$temp->add(new admin_setting_configcheckbox('profilingenabled', get_string('profilingenabled', 'admin'), get_string('profilingenabled_help', 'admin'), false));
// List of URLs that will be automatically profiled
$temp->add(new admin_setting_configtextarea('profilingincluded', get_string('profilingincluded', 'admin'), get_string('profilingincluded_help', 'admin'), ''));
// List of URLs that won't be profiled ever
$temp->add(new admin_setting_configtextarea('profilingexcluded', get_string('profilingexcluded', 'admin'), get_string('profilingexcluded_help', 'admin'), ''));
// Allow random profiling each XX requests
$temp->add(new admin_setting_configtext('profilingautofrec', get_string('profilingautofrec', 'admin'), get_string('profilingautofrec_help', 'admin'), 0, PARAM_INT));
// Allow PROFILEME/DONTPROFILEME GPC
$temp->add(new admin_setting_configcheckbox('profilingallowme', get_string('profilingallowme', 'admin'), get_string('profilingallowme_help', 'admin'), false));
// Allow PROFILEALL/PROFILEALLSTOP GPC
$temp->add(new admin_setting_configcheckbox('profilingallowall', get_string('profilingallowall', 'admin'), get_string('profilingallowall_help', 'admin'), false));
// TODO: Allow to skip PHP functions (XHPROF_FLAGS_NO_BUILTINS)
// TODO: Allow to skip call_user functions (ignored_functions array)
// Specify the life time (in minutes) of profiling runs
$temp->add(new admin_setting_configselect('profilinglifetime', get_string('profilinglifetime', 'admin'), get_string('profilinglifetime_help', 'admin'), 24*60, array(
0 => get_string('neverdeleteruns', 'admin'),
30*24*60 => get_string('numdays', '', 30),
15*24*60 => get_string('numdays', '', 15),
7*24*60 => get_string('numdays', '', 7),
4*24*60 => get_string('numdays', '', 4),
2*24*60 => get_string('numdays', '', 2),
24*60 => get_string('numhours', '', 24),
16*80 => get_string('numhours', '', 16),
8*60 => get_string('numhours', '', 8),
4*60 => get_string('numhours', '', 4),
2*60 => get_string('numhours', '', 2),
60 => get_string('numminutes', '', 60),
30 => get_string('numminutes', '', 30),
15 => get_string('numminutes', '', 15))));

// Add the 'profiling' page to admin block
$ADMIN->add('development', $temp);
}


// XMLDB editor
$ADMIN->add('development', new admin_externalpage('xmldbeditor', get_string('xmldbeditor'), "$CFG->wwwroot/$CFG->admin/xmldb/"));
Expand Down
1 change: 1 addition & 0 deletions admin/settings/server.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
'2' => get_string('gd2'))));
$temp->add(new admin_setting_configexecutable('pathtodu', get_string('pathtodu', 'admin'), get_string('configpathtodu', 'admin'), ''));
$temp->add(new admin_setting_configexecutable('aspellpath', get_string('aspellpath', 'admin'), get_string('edhelpaspellpath'), ''));
$temp->add(new admin_setting_configexecutable('pathtodot', get_string('pathtodot', 'admin'), get_string('pathtodot_help', 'admin'), ''));
$ADMIN->add('server', $temp);


Expand Down
20 changes: 20 additions & 0 deletions lang/en/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,7 @@
$string['navcourselimit'] = 'Course limit';
$string['navshowallcourses'] = 'Show all courses';
$string['navshowcategories'] = 'Show course categories';
$string['neverdeleteruns'] = 'Never delete runs';
$string['nobookmarksforuser'] = 'You do not have any bookmarks.';
$string['nodatabase'] = 'No database';
$string['nodefaultuserrolelists'] = 'Don\'t return all default role users';
Expand Down Expand Up @@ -766,6 +767,8 @@
$string['pathdvips'] = 'Path of <i>dvips</i> binary';
$string['pathlatex'] = 'Path of <i>latex</i> binary';
$string['pathtoclam'] = 'clam AV path';
$string['pathtodot'] = 'Path to dot';
$string['pathtodot_help'] = 'Path to dot. Probably something like /usr/bin/dot. To be able to generate graphics from DOT files, you must have installed the dot executable and point to it here. Note that, for now, this only used by the profiling features (Development->Profiling) built into Moodle.';
$string['pathtodu'] = 'Path to du';
$string['pathtopgdump'] = 'Path to pg_dump';
$string['pathtopgdumpdesc'] = 'This is only necessary to enter if you have more than one pg_dump on your system (for example if you have more than one version of postgresql installed)';
Expand Down Expand Up @@ -801,6 +804,8 @@
$string['profiledeletecategory'] = 'Deleting a category';
$string['profiledeletefield'] = 'Deleting a field';
$string['profiledescription'] = 'Description of the field';
$string['profiledscript'] = 'This script has been profiled';
$string['profiledscriptview'] = 'View profiling information for this script';
$string['profileeditcategory'] = 'Editing category: {$a}';
$string['profileeditfield'] = 'Editing profile field: {$a}';
$string['profilefield'] = 'Profile field';
Expand Down Expand Up @@ -836,6 +841,21 @@
$string['profilevisibleall'] = 'Visible to everyone';
$string['profilevisiblenone'] = 'Not visible';
$string['profilevisibleprivate'] = 'Visible to user';
$string['profiling'] = 'Profiling';
$string['profilingallowall'] = 'Continuous profiling';
$string['profilingallowall_help'] = 'If you enable this setting, then, at any moment, you can use the PROFILEALL parameter anywhere (PGC) to enable profiling for all the executed scripts along the Moodle session life. Analogously, you can use the PROFILEALLSTOP parameter to stop it.';
$string['profilingallowme'] = 'Selective profiling';
$string['profilingallowme_help'] = 'If you enable this setting, then, selectively, you can use the PROFILEME parameter anywhere (PGC) and profiling for that script will happen. Analogously, you can use the DONTPROFILEME parameter to prevent profiling to happen';
$string['profilingautofrec'] = 'Automatic profiling';
$string['profilingautofrec_help'] = 'By configuring this setting, some request (randomly, based on the frecuency specified - 1 of XXX) will be picked and automatically profiled, storing results for further analysis. Note that this way of profiling observes the include/exclude settings. Set it to 0 to disable automatic profiling.';
$string['profilingenabled'] = 'Enable profiling';
$string['profilingenabled_help'] = 'If you enable this setting, then profiling will be available in this site and you will be able to define its behavior by configuring the next options.';
$string['profilingexcluded'] = 'Exclude profiling';
$string['profilingexcluded_help'] = 'List of (comma separated, absolute skipping wwwroot, callable) URLs that will be excluded from being profiled from the ones defined by \'Profile these\' setting.';
$string['profilingincluded'] = 'Profile these';
$string['profilingincluded_help'] = 'List of (comma separated, absolute skipping wwwroot, callable) URLs that will be automatically profiled. Examples: /index.php, /course/view.php. Also accepts the * wildchar at any position. Examples: /mod/forum/*, /mod/*/view.php.';
$string['profilinglifetime'] = 'Keep profiling runs';
$string['profilinglifetime_help'] = 'Specify the time you want to keep information about old profiling runs. Older ones will be pruned periodically. Note that this excludes any profiling run marked as \'reference run\'.';
$string['protectusernames'] = 'Protect usernames';
$string['proxybypass'] = 'Proxy bypass hosts';
$string['proxyhost'] = 'Proxy host';
Expand Down
Loading

0 comments on commit ab12793

Please sign in to comment.