2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
4 // Copyright (c) 2009 Facebook
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
10 // http://www.apache.org/licenses/LICENSE-2.0
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
20 // This file defines the interface iXHProfRuns and also provides a default
21 // implementation of the interface (class XHProfRuns).
25 * iXHProfRuns interface for getting/saving a XHProf run.
27 * Clients can either use the default implementation,
28 * namely XHProfRuns_Default, of this interface or define
29 * their own implementation.
33 interface iXHProfRuns {
36 * Returns XHProf data given a run id ($run) of a given
39 * Also, a brief description of the run is returned via the
40 * $run_desc out parameter.
42 public function get_run($run_id, $type, &$run_desc);
45 * Save XHProf data for a profiler run of specified type
48 * The caller may optionally pass in run_id (which they
49 * promise to be unique). If a run_id is not passed in,
50 * the implementation of this method must generated a
51 * unique run id for this saved XHProf run.
53 * Returns the run id for the saved XHProf run.
56 public function save_run($xhprof_data, $type, $run_id = null);
61 * XHProfRuns_Default is the default implementation of the
62 * iXHProfRuns interface for saving/fetching XHProf runs.
64 * It stores/retrieves runs to/from a filesystem directory
65 * specified by the "xhprof.output_dir" ini parameter.
69 class XHProfRuns_Default implements iXHProfRuns {
72 private $suffix = 'xhprof';
74 private function gen_run_id($type) {
78 private function file_name($run_id, $type) {
80 $file = "$run_id.$type." . $this->suffix;
82 if (!empty($this->dir)) {
83 $file = $this->dir . "/" . $file;
88 public function __construct($dir = null) {
90 // if user hasn't passed a directory location,
91 // we use the xhprof.output_dir ini setting
92 // if specified, else we default to the directory
93 // in which the error_log file resides.
96 $dir = ini_get("xhprof.output_dir");
99 // some default that at least works on unix...
102 xhprof_error("Warning: Must specify directory location for XHProf runs. ".
103 "Trying {$dir} as default. You can either pass the " .
104 "directory location as an argument to the constructor ".
105 "for XHProfRuns_Default() or set xhprof.output_dir ".
112 public function get_run($run_id, $type, &$run_desc) {
113 $file_name = $this->file_name($run_id, $type);
115 if (!file_exists($file_name)) {
116 xhprof_error("Could not find file $file_name");
117 $run_desc = "Invalid Run Id = $run_id";
121 $contents = file_get_contents($file_name);
122 $run_desc = "XHProf Run (Namespace=$type)";
123 return unserialize($contents);
126 public function save_run($xhprof_data, $type, $run_id = null) {
128 // Use PHP serialize function to store the XHProf's
129 // raw profiler data.
130 $xhprof_data = serialize($xhprof_data);
132 if ($run_id === null) {
133 $run_id = $this->gen_run_id($type);
136 $file_name = $this->file_name($run_id, $type);
137 $file = fopen($file_name, 'w');
140 fwrite($file, $xhprof_data);
143 xhprof_error("Could not open $file_name\n");
146 // echo "Saved run in {$file_name}.\nRun id = {$run_id}.\n";
150 function list_runs() {
151 if (is_dir($this->dir)) {
152 echo "<hr/>Existing runs:\n<ul>\n";
153 $files = glob("{$this->dir}/*.{$this->suffix}");
154 usort($files, create_function('$a,$b', 'return filemtime($b) - filemtime($a);'));
155 foreach ($files as $file) {
156 list($run,$source) = explode('.', basename($file));
157 echo '<li><a href="' . htmlentities($_SERVER['SCRIPT_NAME'])
158 . '?run=' . htmlentities($run) . '&source='
159 . htmlentities($source) . '">'
160 . htmlentities(basename($file)) . "</a><small> "
161 . date("Y-m-d H:i:s", filemtime($file)) . "</small></li>\n";