[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/xhprof/xhprof_lib/utils/ -> xhprof_runs.php (source)

   1  <?php
   2  //
   3  //  Copyright (c) 2009 Facebook
   4  //
   5  //  Licensed under the Apache License, Version 2.0 (the "License");
   6  //  you may not use this file except in compliance with the License.
   7  //  You may obtain a copy of the License at
   8  //
   9  //      http://www.apache.org/licenses/LICENSE-2.0
  10  //
  11  //  Unless required by applicable law or agreed to in writing, software
  12  //  distributed under the License is distributed on an "AS IS" BASIS,
  13  //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14  //  See the License for the specific language governing permissions and
  15  //  limitations under the License.
  16  //
  17  
  18  //
  19  // This file defines the interface iXHProfRuns and also provides a default
  20  // implementation of the interface (class XHProfRuns).
  21  //
  22  
  23  /**
  24   * iXHProfRuns interface for getting/saving a XHProf run.
  25   *
  26   * Clients can either use the default implementation,
  27   * namely XHProfRuns_Default, of this interface or define
  28   * their own implementation.
  29   *
  30   * @author Kannan
  31   */
  32  interface iXHProfRuns {
  33  
  34    /**
  35     * Returns XHProf data given a run id ($run) of a given
  36     * type ($type).
  37     *
  38     * Also, a brief description of the run is returned via the
  39     * $run_desc out parameter.
  40     */
  41    public function get_run($run_id, $type, &$run_desc);
  42  
  43    /**
  44     * Save XHProf data for a profiler run of specified type
  45     * ($type).
  46     *
  47     * The caller may optionally pass in run_id (which they
  48     * promise to be unique). If a run_id is not passed in,
  49     * the implementation of this method must generated a
  50     * unique run id for this saved XHProf run.
  51     *
  52     * Returns the run id for the saved XHProf run.
  53     *
  54     */
  55    public function save_run($xhprof_data, $type, $run_id = null);
  56  }
  57  
  58  
  59  /**
  60   * XHProfRuns_Default is the default implementation of the
  61   * iXHProfRuns interface for saving/fetching XHProf runs.
  62   *
  63   * It stores/retrieves runs to/from a filesystem directory
  64   * specified by the "xhprof.output_dir" ini parameter.
  65   *
  66   * @author Kannan
  67   */
  68  class XHProfRuns_Default implements iXHProfRuns {
  69  
  70    private $dir = '';
  71    private $suffix = 'xhprof';
  72  
  73    private function gen_run_id($type) {
  74      return uniqid();
  75    }
  76  
  77    private function file_name($run_id, $type) {
  78  
  79      $file = "$run_id.$type." . $this->suffix;
  80  
  81      if (!empty($this->dir)) {
  82        $file = $this->dir . "/" . $file;
  83      }
  84      return $file;
  85    }
  86  
  87    public function __construct($dir = null) {
  88  
  89      // if user hasn't passed a directory location,
  90      // we use the xhprof.output_dir ini setting
  91      // if specified, else we default to the directory
  92      // in which the error_log file resides.
  93  
  94      if (empty($dir)) {
  95        $dir = ini_get("xhprof.output_dir");
  96        if (empty($dir)) {
  97  
  98          // some default that at least works on unix...
  99          $dir = "/tmp";
 100  
 101          xhprof_error("Warning: Must specify directory location for XHProf runs. ".
 102                       "Trying {$dir} as default. You can either pass the " .
 103                       "directory location as an argument to the constructor ".
 104                       "for XHProfRuns_Default() or set xhprof.output_dir ".
 105                       "ini param.");
 106        }
 107      }
 108      $this->dir = $dir;
 109    }
 110  
 111    public function get_run($run_id, $type, &$run_desc) {
 112      $file_name = $this->file_name($run_id, $type);
 113  
 114      if (!file_exists($file_name)) {
 115        xhprof_error("Could not find file $file_name");
 116        $run_desc = "Invalid Run Id = $run_id";
 117        return null;
 118      }
 119  
 120      $contents = file_get_contents($file_name);
 121      $run_desc = "XHProf Run (Namespace=$type)";
 122      return unserialize($contents);
 123    }
 124  
 125    public function save_run($xhprof_data, $type, $run_id = null) {
 126  
 127      // Use PHP serialize function to store the XHProf's
 128      // raw profiler data.
 129      $xhprof_data = serialize($xhprof_data);
 130  
 131      if ($run_id === null) {
 132        $run_id = $this->gen_run_id($type);
 133      }
 134  
 135      $file_name = $this->file_name($run_id, $type);
 136      $file = fopen($file_name, 'w');
 137  
 138      if ($file) {
 139        fwrite($file, $xhprof_data);
 140        fclose($file);
 141      } else {
 142        xhprof_error("Could not open $file_name\n");
 143      }
 144  
 145      // echo "Saved run in {$file_name}.\nRun id = {$run_id}.\n";
 146      return $run_id;
 147    }
 148  
 149    function list_runs() {
 150      if (is_dir($this->dir)) {
 151          echo "<hr/>Existing runs:\n<ul>\n";
 152          $files = glob("{$this->dir}/*.{$this->suffix}");
 153          usort($files, create_function('$a,$b', 'return filemtime($b) - filemtime($a);'));
 154          foreach ($files as $file) {
 155              list($run,$source) = explode('.', basename($file));
 156              echo '<li><a href="' . htmlentities($_SERVER['SCRIPT_NAME'])
 157                  . '?run=' . htmlentities($run) . '&source='
 158                  . htmlentities($source) . '">'
 159                  . htmlentities(basename($file)) . "</a><small> "
 160                  . date("Y-m-d H:i:s", filemtime($file)) . "</small></li>\n";
 161          }
 162          echo "</ul>\n";
 163      }
 164    }
 165  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1