]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/SugarXHprof/xhprof_lib/utils/xhprof_runs.php
Release 6.5.10
[Github/sugarcrm.git] / include / SugarXHprof / xhprof_lib / utils / xhprof_runs.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 //
4 //  Copyright (c) 2009 Facebook
5 //
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
9 //
10 //      http://www.apache.org/licenses/LICENSE-2.0
11 //
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.
17 //
18
19 //
20 // This file defines the interface iXHProfRuns and also provides a default
21 // implementation of the interface (class XHProfRuns).
22 //
23
24 /**
25  * iXHProfRuns interface for getting/saving a XHProf run.
26  *
27  * Clients can either use the default implementation,
28  * namely XHProfRuns_Default, of this interface or define
29  * their own implementation.
30  *
31  * @author Kannan
32  */
33 interface iXHProfRuns {
34
35   /**
36    * Returns XHProf data given a run id ($run) of a given
37    * type ($type).
38    *
39    * Also, a brief description of the run is returned via the
40    * $run_desc out parameter.
41    */
42   public function get_run($run_id, $type, &$run_desc);
43
44   /**
45    * Save XHProf data for a profiler run of specified type
46    * ($type).
47    *
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.
52    *
53    * Returns the run id for the saved XHProf run.
54    *
55    */
56   public function save_run($xhprof_data, $type, $run_id = null);
57 }
58
59
60 /**
61  * XHProfRuns_Default is the default implementation of the
62  * iXHProfRuns interface for saving/fetching XHProf runs.
63  *
64  * It stores/retrieves runs to/from a filesystem directory
65  * specified by the "xhprof.output_dir" ini parameter.
66  *
67  * @author Kannan
68  */
69 class XHProfRuns_Default implements iXHProfRuns {
70
71   private $dir = '';
72   private $suffix = 'xhprof';
73
74   private function gen_run_id($type) {
75     return uniqid();
76   }
77
78   private function file_name($run_id, $type) {
79
80     $file = "$run_id.$type." . $this->suffix;
81
82     if (!empty($this->dir)) {
83       $file = $this->dir . "/" . $file;
84     }
85     return $file;
86   }
87
88   public function __construct($dir = null) {
89
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.
94
95     if (empty($dir)) {
96       $dir = ini_get("xhprof.output_dir");
97       if (empty($dir)) {
98
99         // some default that at least works on unix...
100         $dir = "/tmp";
101
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 ".
106                      "ini param.");
107       }
108     }
109     $this->dir = $dir;
110   }
111
112   public function get_run($run_id, $type, &$run_desc) {
113     $file_name = $this->file_name($run_id, $type);
114
115     if (!file_exists($file_name)) {
116       xhprof_error("Could not find file $file_name");
117       $run_desc = "Invalid Run Id = $run_id";
118       return null;
119     }
120
121     $contents = file_get_contents($file_name);
122     $run_desc = "XHProf Run (Namespace=$type)";
123     return unserialize($contents);
124   }
125
126   public function save_run($xhprof_data, $type, $run_id = null) {
127
128     // Use PHP serialize function to store the XHProf's
129     // raw profiler data.
130     $xhprof_data = serialize($xhprof_data);
131
132     if ($run_id === null) {
133       $run_id = $this->gen_run_id($type);
134     }
135
136     $file_name = $this->file_name($run_id, $type);
137     $file = fopen($file_name, 'w');
138
139     if ($file) {
140       fwrite($file, $xhprof_data);
141       fclose($file);
142     } else {
143       xhprof_error("Could not open $file_name\n");
144     }
145
146     // echo "Saved run in {$file_name}.\nRun id = {$run_id}.\n";
147     return $run_id;
148   }
149
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";
162         }
163         echo "</ul>\n";
164     }
165   }
166 }