PageRenderTime 28ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/include/SugarXHprof/xhprof_lib/utils/xhprof_runs.php

https://gitlab.com/tjaafar/SuiteCRM
PHP | 166 lines | 63 code | 26 blank | 77 comment | 11 complexity | f3637c01789372611ef321ba192f7a12 MD5 | raw file
  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. // This file defines the interface iXHProfRuns and also provides a default
  20. // implementation of the interface (class XHProfRuns).
  21. //
  22. /**
  23. * iXHProfRuns interface for getting/saving a XHProf run.
  24. *
  25. * Clients can either use the default implementation,
  26. * namely XHProfRuns_Default, of this interface or define
  27. * their own implementation.
  28. *
  29. * @author Kannan
  30. */
  31. interface iXHProfRuns {
  32. /**
  33. * Returns XHProf data given a run id ($run) of a given
  34. * type ($type).
  35. *
  36. * Also, a brief description of the run is returned via the
  37. * $run_desc out parameter.
  38. */
  39. public function get_run($run_id, $type, &$run_desc);
  40. /**
  41. * Save XHProf data for a profiler run of specified type
  42. * ($type).
  43. *
  44. * The caller may optionally pass in run_id (which they
  45. * promise to be unique). If a run_id is not passed in,
  46. * the implementation of this method must generated a
  47. * unique run id for this saved XHProf run.
  48. *
  49. * Returns the run id for the saved XHProf run.
  50. *
  51. */
  52. public function save_run($xhprof_data, $type, $run_id = null);
  53. }
  54. /**
  55. * XHProfRuns_Default is the default implementation of the
  56. * iXHProfRuns interface for saving/fetching XHProf runs.
  57. *
  58. * It stores/retrieves runs to/from a filesystem directory
  59. * specified by the "xhprof.output_dir" ini parameter.
  60. *
  61. * @author Kannan
  62. */
  63. class XHProfRuns_Default implements iXHProfRuns {
  64. private $dir = '';
  65. private $suffix = 'xhprof';
  66. private function gen_run_id($type) {
  67. return uniqid();
  68. }
  69. private function file_name($run_id, $type) {
  70. $file = "$run_id.$type." . $this->suffix;
  71. if (!empty($this->dir)) {
  72. $file = $this->dir . "/" . $file;
  73. }
  74. return $file;
  75. }
  76. public function __construct($dir = null) {
  77. // if user hasn't passed a directory location,
  78. // we use the xhprof.output_dir ini setting
  79. // if specified, else we default to the directory
  80. // in which the error_log file resides.
  81. if (empty($dir)) {
  82. $dir = ini_get("xhprof.output_dir");
  83. if (empty($dir)) {
  84. // some default that at least works on unix...
  85. $dir = "/tmp";
  86. xhprof_error("Warning: Must specify directory location for XHProf runs. ".
  87. "Trying {$dir} as default. You can either pass the " .
  88. "directory location as an argument to the constructor ".
  89. "for XHProfRuns_Default() or set xhprof.output_dir ".
  90. "ini param.");
  91. }
  92. }
  93. $this->dir = $dir;
  94. }
  95. public function get_run($run_id, $type, &$run_desc) {
  96. $file_name = $this->file_name($run_id, $type);
  97. if (!file_exists($file_name)) {
  98. xhprof_error("Could not find file $file_name");
  99. $run_desc = "Invalid Run Id = $run_id";
  100. return null;
  101. }
  102. $contents = file_get_contents($file_name);
  103. $run_desc = "XHProf Run (Namespace=$type)";
  104. return unserialize($contents);
  105. }
  106. public function save_run($xhprof_data, $type, $run_id = null) {
  107. // Use PHP serialize function to store the XHProf's
  108. // raw profiler data.
  109. $xhprof_data = serialize($xhprof_data);
  110. if ($run_id === null) {
  111. $run_id = $this->gen_run_id($type);
  112. }
  113. $file_name = $this->file_name($run_id, $type);
  114. $file = fopen($file_name, 'w');
  115. if ($file) {
  116. fwrite($file, $xhprof_data);
  117. fclose($file);
  118. } else {
  119. xhprof_error("Could not open $file_name\n");
  120. }
  121. // echo "Saved run in {$file_name}.\nRun id = {$run_id}.\n";
  122. return $run_id;
  123. }
  124. function list_runs() {
  125. if (is_dir($this->dir)) {
  126. echo "<hr/>Existing runs:\n<ul>\n";
  127. $files = glob("{$this->dir}/*.{$this->suffix}");
  128. usort($files, create_function('$a,$b', 'return filemtime($b) - filemtime($a);'));
  129. foreach ($files as $file) {
  130. list($run,$source) = explode('.', basename($file));
  131. echo '<li><a href="' . htmlentities($_SERVER['SCRIPT_NAME'])
  132. . '?run=' . htmlentities($run) . '&source='
  133. . htmlentities($source) . '">'
  134. . htmlentities(basename($file)) . "</a><small> "
  135. . date("Y-m-d H:i:s", filemtime($file)) . "</small></li>\n";
  136. }
  137. echo "</ul>\n";
  138. }
  139. }
  140. }