Skip to content

Instantly share code, notes, and snippets.

@XOlegator
Last active August 31, 2020 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save XOlegator/92466b7027b8e83b348fbed3e0f3a5b1 to your computer and use it in GitHub Desktop.
Save XOlegator/92466b7027b8e83b348fbed3e0f3a5b1 to your computer and use it in GitHub Desktop.
Обрамление PHP кода для профилирования профилировщиком Xhprof или его форком Tideways XHProf
<?php
if (isset($_GET['prof']) && 'Y' === $_GET['prof']) {
if (function_exists('tideways_xhprof_enable')) {
// Профилировщик для PHP7 - https://github.com/tideways/php-xhprof-extension
tideways_xhprof_enable(TIDEWAYS_XHPROF_FLAGS_MEMORY | TIDEWAYS_XHPROF_FLAGS_CPU);
} elseif (function_exists('xhprof_enable')) {
// XHprof заброшен и работает только на PHP5
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
}
}
// End of first part profiling code
////////////////////////////
// PHP CODE FOR PROFILING //
////////////////////////////
// Start of last part profiling code
if (isset($_GET['prof']) && 'Y' === $_GET['prof']) {
if (function_exists('tideways_xhprof_disable')) {
// Форк профилеровщика XHprof - https://github.com/tideways/php-xhprof-extension
$xhprof_data = tideways_xhprof_disable();
} elseif (function_exists('xhprof_disable')) {
// XHprof - https://pecl.php.net/package/xhprof
$xhprof_data = xhprof_disable();
}
$xhprofCategory = 'category';
// TODO: Включить это, если потребуется сохранить отчёт не во временном каталоге,
// а в постоянном хранилище (для дальнейшего исследования)
/*
file_put_contents(
$_SERVER['DOCUMENT_ROOT'] . '/log/xhprof' . DIRECTORY_SEPARATOR . uniqid() . '.' . $xhprofCategory . '.xhprof',
serialize($xhprof_data)
);
*/
if (defined('XHPROF_LIB_DIR')) {
// В коде определена константа с каталогом Xhprof
include_once XHPROF_LIB_DIR . 'utils/xhprof_lib.php';
include_once XHPROF_LIB_DIR . 'utils/xhprof_runs.php';
} else {
// Вариант жёсткого указанаий на каталог Xhprof
include_once '/mnt/projects/sites/xhprof/www/xhprof_lib/utils/xhprof_lib.php';
include_once '/mnt/projects/sites/xhprof/www/xhprof_lib/utils/xhprof_runs.php';
}
$xhprof_runs = new XHProfRuns_Default();
$run_id = $xhprof_runs->save_run($xhprof_data, $xhprofCategory);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment