xh.drush.inc

  1. 8.0.x commands/xh.drush.inc
  2. 7.x commands/xh.drush.inc
  3. master commands/xh.drush.inc

Functions

Namesort ascending Description
xh_link Returns the XHProf link.
xh_flags Determines flags for xhprof_enable based on drush options.
xh_enabled
xh_drush_init Implements hook_drush_init().
xh_drush_help_alter Implements hook_drush_help_alter().
xh_drush_exit Implements hook_drush_exit().

Constants

File

commands/xh.drush.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. */
  5. use Drush\Log\LogLevel;
  6. define('XH_PROFILE_MEMORY', FALSE);
  7. define('XH_PROFILE_CPU', FALSE);
  8. define('XH_PROFILE_BUILTINS', TRUE);
  9. /**
  10. * Implements hook_drush_help_alter().
  11. */
  12. function xh_drush_help_alter(&$command) {
  13. if ($command['command'] == 'global-options') {
  14. // Do not include these in options in standard help.
  15. if ($command['#brief'] === FALSE) {
  16. $command['options']['xh'] = array(
  17. 'description' => 'Enable profiling via XHProf',
  18. );
  19. $command['sub-options']['xh']['xh-link'] = 'URL to your XHProf report site.';
  20. $command['sub-options']['xh']['xh-path'] = 'Absolute path to the xhprof project.';
  21. $command['sub-options']['xh']['xh-profile-builtins'] = 'Profile built-in PHP functions (defaults to TRUE).';
  22. $command['sub-options']['xh']['xh-profile-cpu'] = 'Profile CPU (defaults to FALSE).';
  23. $command['sub-options']['xh']['xh-profile-memory'] = 'Profile Memory (defaults to FALSE).';
  24. }
  25. }
  26. }
  27. function xh_enabled() {
  28. if (drush_get_option('xh')) {
  29. if (!extension_loaded('xhprof')) {
  30. return drush_set_error('You must enable the xhprof PHP extension in your CLI PHP in order to profile.');
  31. }
  32. if (!drush_get_option('xh-path', '')) {
  33. return drush_set_error('You must provide the xh-path option in your drushrc or on the CLI in order to profile.');
  34. }
  35. return TRUE;
  36. }
  37. }
  38. /**
  39. * Determines flags for xhprof_enable based on drush options.
  40. */
  41. function xh_flags() {
  42. $flags = 0;
  43. if (!drush_get_option('xh-profile-builtins', XH_PROFILE_BUILTINS)) {
  44. $flags |= XHPROF_FLAGS_NO_BUILTINS;
  45. }
  46. if (drush_get_option('xh-profile-cpu', XH_PROFILE_CPU)) {
  47. $flags |= XHPROF_FLAGS_CPU;
  48. }
  49. if (drush_get_option('xh-profile-memory', XH_PROFILE_MEMORY)) {
  50. $flags |= XHPROF_FLAGS_MEMORY;
  51. }
  52. return $flags;
  53. }
  54. /**
  55. * Implements hook_drush_init().
  56. */
  57. function xh_drush_init() {
  58. if (xh_enabled()) {
  59. if ($path = drush_get_option('xh-path', '')) {
  60. include_once $path . '/xhprof_lib/utils/xhprof_lib.php';
  61. include_once $path . '/xhprof_lib/utils/xhprof_runs.php';
  62. xhprof_enable(xh_flags());
  63. }
  64. }
  65. }
  66. /**
  67. * Implements hook_drush_exit().
  68. */
  69. function xh_drush_exit() {
  70. if (xh_enabled()) {
  71. $args = func_get_args();
  72. $namespace = 'Drush'; // variable_get('site_name', '');
  73. $xhprof_data = xhprof_disable();
  74. $xhprof_runs = new XHProfRuns_Default();
  75. $run_id = $xhprof_runs->save_run($xhprof_data, $namespace);
  76. if ($url = xh_link($run_id)) {
  77. drush_log(dt('XHProf run saved. View report at !url', array('!url' => $url)), LogLevel::OK);
  78. }
  79. }
  80. }
  81. /**
  82. * Returns the XHProf link.
  83. */
  84. function xh_link($run_id) {
  85. if ($xhprof_url = trim(drush_get_option('xh-link'))) {
  86. $namespace = 'Drush'; //variable_get('site_name', '');
  87. return $xhprof_url . '/index.php?run=' . urlencode($run_id) . '&source=' . urlencode($namespace);
  88. }
  89. else {
  90. drush_log('Configure xh_link in order to see a link to the XHProf report for this request instead of this message.');
  91. }
  92. }