* Profiling tool export utility. * * @package tool_profiling * @copyright 2013 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ require_once dirname(__FILE__) . '/../../../config.php'; require_once $CFG->libdir . '/adminlib.php'; require_once $CFG->libdir . '/xhprof/xhprof_moodle.php'; // Page parameters. $runid = required_param('runid', PARAM_ALPHANUM); $listurl = required_param('listurl', PARAM_PATH); admin_externalpage_setup('toolprofiling'); $PAGE->navbar->add(get_string('export', 'tool_profiling')); // Calculate export variables. $tempdir = 'profiling'; make_temp_directory($tempdir); $runids = array($runid); $filename = $runid . '.mpr'; $filepath = $CFG->tempdir . '/' . $tempdir . '/' . $filename; // Generate the mpr file and send it. if (profiling_export_runs($runids, $filepath)) { send_file($filepath, $filename, 0, 0, false, false, '', true); unlink($filepath); // Delete once sent. die; } // Something wrong happened, notice it and done. $urlparams = array('runid' => $runid, 'listurl' => $listurl); $url = new moodle_url('/admin/tool/profiling/index.php', $urlparams); notice(get_string('exportproblem', 'tool_profiling', $urlparams), $url);
/** * Given some run data, one type and, optionally, one runid * store the information in DB * * Note that $type is completely ignored */ public function save_run($xhprof_data, $type, $run_id = null) { global $DB, $CFG; if (is_null($this->url)) { xhprof_error("Warning: You must use the prepare_run() method before saving it"); } // Calculate runid if needed $this->runid = is_null($run_id) ? md5($this->url . '-' . uniqid()) : $run_id; // Calculate totals $this->totalexecutiontime = $xhprof_data['main()']['wt']; $this->totalcputime = $xhprof_data['main()']['cpu']; $this->totalcalls = array_reduce($xhprof_data, array($this, 'sum_calls')); $this->totalmemory = $xhprof_data['main()']['mu']; // Prepare data $rec = new stdClass(); $rec->runid = $this->runid; $rec->url = $this->url; $rec->data = base64_encode(gzcompress(serialize($xhprof_data), 9)); $rec->totalexecutiontime = $this->totalexecutiontime; $rec->totalcputime = $this->totalcputime; $rec->totalcalls = $this->totalcalls; $rec->totalmemory = $this->totalmemory; $rec->timecreated = $this->timecreated; $DB->insert_record('profiling', $rec); if (PHPUNIT_TEST) { // Calculate export variables. $tempdir = 'profiling'; make_temp_directory($tempdir); $runids = array($this->runid); $filename = $this->runid . '.mpr'; $filepath = $CFG->tempdir . '/' . $tempdir . '/' . $filename; // Generate the mpr file and send it. if (profiling_export_runs($runids, $filepath)) { fprintf(STDERR, "Profiling data saved to: " . $filepath . "\n"); } } return $this->runid; }