Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   1  <?php
   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  /**
  18   * Profiling tool.
  19   *
  20   * @package    tool_profiling
  21   * @copyright  2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  // TODO: Move all the DB stuff to profiling_db_xxxx() function in xhprof_moodle.php
  26  
  27  // TODO: it is wrong when core lib references ANY plugin lang strings, maybe more login could be moved here (skodak)
  28  
  29  require_once(__DIR__ . '/../../../config.php');
  30  require_once($CFG->libdir.'/adminlib.php');
  31  require_once($CFG->libdir . '/xhprof/xhprof_moodle.php');
  32  
  33  define('PROFILING_RUNSPERPAGE', 50);
  34  
  35  // page parameters
  36  $script   = optional_param('script', null, PARAM_PATH);
  37  $runid    = optional_param('runid', null, PARAM_ALPHANUM);
  38  $runid2   = optional_param('runid2', null, PARAM_ALPHANUM);
  39  $listurl  = optional_param('listurl', null, PARAM_PATH);
  40  $runreference= optional_param('runreference', 0, PARAM_INT);
  41  $runcomment  = optional_param('runcomment', null, PARAM_TEXT);
  42  
  43  $dbfields = 'runid, url, totalexecutiontime, totalcputime, ' .
  44              'totalcalls, totalmemory, runreference, runcomment, timecreated';
  45  
  46  admin_externalpage_setup('toolprofiling');
  47  
  48  // Always add listurl if available
  49  if ($listurl) {
  50      $listurlnav = new moodle_url('/admin/tool/profiling/index.php', array('listurl' => $listurl));
  51      $PAGE->navbar->add($listurl, $listurlnav);
  52  }
  53  
  54  // Header
  55  echo $OUTPUT->header();
  56  
  57  // We have requested the last available run for one script
  58  if (isset($script)) {
  59      // Get the last available run for the given script
  60      $run = $DB->get_record_sql("SELECT $dbfields
  61                                   FROM {profiling}
  62                                  WHERE url = ?
  63                                    AND id = (SELECT MAX(id)
  64                                                FROM {profiling}
  65                                               WHERE url = ?)",
  66                                array($script, $script), IGNORE_MISSING);
  67  
  68      // No run found for script, warn and exit
  69      if (!$run) {
  70          notice(get_string('cannotfindanyrunforurl', 'tool_profiling', $script), 'index.php');
  71      }
  72  
  73      // Check if there is any previous run marked as reference one
  74      $prevreferences = $DB->get_records_select('profiling',
  75                                                'url = ? AND runreference = 1 AND timecreated < ?',
  76                                                array($run->url, $run->timecreated),
  77                                                'timecreated DESC', 'runid, runcomment, timecreated', 0, 10);
  78      echo $OUTPUT->box_start('generalbox boxwidthwide boxaligncenter');
  79      $header = get_string('lastrunof', 'tool_profiling', $script);
  80      echo $OUTPUT->heading($header);
  81      $table = profiling_print_run($run, $prevreferences);
  82      echo $table;
  83      echo $OUTPUT->box_end();
  84  
  85  
  86  // We have requested the diff between 2 runs
  87  } else if (isset($runid) && isset($runid2)) {
  88      $run1 = $DB->get_record('profiling', array('runid'=>$runid), $dbfields, MUST_EXIST);
  89      $run2 = $DB->get_record('profiling', array('runid'=>$runid2), $dbfields, MUST_EXIST);
  90      if ($run1->url == $run2->url && $run1->runid != $run2->runid) {
  91          if ($run2->timecreated < $run1->timecreated) {
  92              $runtemp = $run1;
  93              $run1 = $run2;
  94              $run2 = $runtemp;
  95          }
  96          echo $OUTPUT->box_start('generalbox boxwidthwide boxaligncenter');
  97          $header = get_string('differencesbetween2runsof', 'tool_profiling', $run1->url);
  98          echo $OUTPUT->heading($header);
  99          $table = profiling_print_rundiff($run1, $run2);
 100          echo $table;
 101          echo $OUTPUT->box_end();
 102      }
 103  
 104  
 105  // We have requested one run, invoke it
 106  } else if (isset($runid)) {
 107      // Check if we are trying to update the runreference/runcomment for the run
 108      if (isset($runcomment) && confirm_sesskey()) {
 109          $id = $DB->get_field('profiling', 'id', array('runid' => $runid), MUST_EXIST);
 110          $rec = new stdClass();
 111          $rec->id = $id;
 112          $rec->runreference = (bool)$runreference;
 113          $rec->runcomment   = $runcomment;
 114          $DB->update_record('profiling', $rec);
 115      }
 116      // Get the requested runid
 117      $run = $DB->get_record('profiling', array('runid'=>$runid), $dbfields, IGNORE_MISSING);
 118  
 119      // No run found for runid, warn and exit
 120      if (!$run) {
 121          notice(get_string('cannotfindanyrunforrunid', 'tool_profiling', $runid), 'index.php');
 122      }
 123  
 124      // Check if there is any previous run marked as reference one
 125      $prevreferences = $DB->get_records_select('profiling',
 126                                                'url = ? AND runreference = 1 AND timecreated < ?',
 127                                                array($run->url, $run->timecreated),
 128                                                'timecreated DESC', 'runid, runcomment, timecreated', 0, 10);
 129      echo $OUTPUT->box_start('generalbox boxwidthwide boxaligncenter');
 130      $header = get_string('summaryof', 'tool_profiling', $run->url);
 131      echo $OUTPUT->heading($header);
 132      $table = profiling_print_run($run, $prevreferences);
 133      echo $table;
 134      echo $OUTPUT->box_end();
 135  
 136  
 137  // Default: List one page of runs
 138  } else {
 139  
 140      // The flexitable that will root listings
 141      $table = new xhprof_table_sql('profiling-list-table');
 142      $baseurl = $CFG->wwwroot . '/'.$CFG->admin.'/tool/profiling/index.php';
 143  
 144      // Check if we are listing all or some URL ones
 145      $sqlconditions = '';
 146      $sqlparams = array();
 147      if (!isset($listurl)) {
 148          $header = get_string('pluginname', 'tool_profiling');
 149          $sqlconditions = '1 = 1';
 150          $table->set_listurlmode(false);
 151      } else {
 152          $header =  get_string('profilingrunsfor', 'tool_profiling', $listurl);
 153          $sqlconditions = 'url = :url';
 154          $sqlparams['url'] = $listurl;
 155          $table->set_listurlmode(true);
 156          $baseurl .= '?listurl=' . urlencode($listurl);
 157      }
 158  
 159      echo $OUTPUT->heading($header);
 160  
 161      // Print the controller block with different options.
 162      echo profiling_list_controls($listurl);
 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(
 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'));
 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  
 184  // Footer.
 185  echo $OUTPUT->footer();
 186