|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // 00003 // Copyright (c) 2009 Facebook 00004 // 00005 // Licensed under the Apache License, Version 2.0 (the "License"); 00006 // you may not use this file except in compliance with the License. 00007 // You may obtain a copy of the License at 00008 // 00009 // http://www.apache.org/licenses/LICENSE-2.0 00010 // 00011 // Unless required by applicable law or agreed to in writing, software 00012 // distributed under the License is distributed on an "AS IS" BASIS, 00013 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 // See the License for the specific language governing permissions and 00015 // limitations under the License. 00016 // 00017 00018 // 00019 // This file defines the interface iXHProfRuns and also provides a default 00020 // implementation of the interface (class XHProfRuns). 00021 // 00022 00032 interface iXHProfRuns { 00033 00041 public function get_run($run_id, $type, &$run_desc); 00042 00055 public function save_run($xhprof_data, $type, $run_id = null); 00056 } 00057 00058 00068 class XHProfRuns_Default implements iXHProfRuns { 00069 00070 private $dir = ''; 00071 private $suffix = 'xhprof'; 00072 00073 private function gen_run_id($type) { 00074 return uniqid(); 00075 } 00076 00077 private function file_name($run_id, $type) { 00078 00079 $file = "$run_id.$type." . $this->suffix; 00080 00081 if (!empty($this->dir)) { 00082 $file = $this->dir . "/" . $file; 00083 } 00084 return $file; 00085 } 00086 00087 public function __construct($dir = null) { 00088 00089 // if user hasn't passed a directory location, 00090 // we use the xhprof.output_dir ini setting 00091 // if specified, else we default to the directory 00092 // in which the error_log file resides. 00093 00094 if (empty($dir)) { 00095 $dir = ini_get("xhprof.output_dir"); 00096 if (empty($dir)) { 00097 00098 // some default that at least works on unix... 00099 $dir = "/tmp"; 00100 00101 xhprof_error("Warning: Must specify directory location for XHProf runs. ". 00102 "Trying {$dir} as default. You can either pass the " . 00103 "directory location as an argument to the constructor ". 00104 "for XHProfRuns_Default() or set xhprof.output_dir ". 00105 "ini param."); 00106 } 00107 } 00108 $this->dir = $dir; 00109 } 00110 00111 public function get_run($run_id, $type, &$run_desc) { 00112 $file_name = $this->file_name($run_id, $type); 00113 00114 if (!file_exists($file_name)) { 00115 xhprof_error("Could not find file $file_name"); 00116 $run_desc = "Invalid Run Id = $run_id"; 00117 return null; 00118 } 00119 00120 $contents = file_get_contents($file_name); 00121 $run_desc = "XHProf Run (Namespace=$type)"; 00122 return unserialize($contents); 00123 } 00124 00125 public function save_run($xhprof_data, $type, $run_id = null) { 00126 00127 // Use PHP serialize function to store the XHProf's 00128 // raw profiler data. 00129 $xhprof_data = serialize($xhprof_data); 00130 00131 if ($run_id === null) { 00132 $run_id = $this->gen_run_id($type); 00133 } 00134 00135 $file_name = $this->file_name($run_id, $type); 00136 $file = fopen($file_name, 'w'); 00137 00138 if ($file) { 00139 fwrite($file, $xhprof_data); 00140 fclose($file); 00141 } else { 00142 xhprof_error("Could not open $file_name\n"); 00143 } 00144 00145 // echo "Saved run in {$file_name}.\nRun id = {$run_id}.\n"; 00146 return $run_id; 00147 } 00148 00149 function list_runs() { 00150 if (is_dir($this->dir)) { 00151 echo "<hr/>Existing runs:\n<ul>\n"; 00152 foreach (glob("{$this->dir}/*.{$this->suffix}") as $file) { 00153 list($run,$source) = explode('.', basename($file)); 00154 echo '<li><a href="' . htmlentities($_SERVER['SCRIPT_NAME']) 00155 . '?run=' . htmlentities($run) . '&source=' 00156 . htmlentities($source) . '">' 00157 . htmlentities(basename($file)) . "</a><small> " 00158 . date("Y-m-d H:i:s", filemtime($file)) . "</small></li>\n"; 00159 } 00160 echo "</ul>\n"; 00161 } 00162 } 00163 }