PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/xhprof/xhprof_lib/utils/xhprof_runs.php

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