|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Bootstrap for php-profiler. Copy and customize this file, |
| 5 | + * include this file inside some bootstrapper or other "early central point in execution" |
| 6 | + * |
| 7 | + * Documentation: |
| 8 | + * - https://github.com/perftools/php-profiler#create-profiler |
| 9 | + * - https://github.com/perftools/php-profiler#config |
| 10 | + */ |
| 11 | + |
| 12 | +use Xhgui\Profiler\Profiler; |
| 13 | +use Xhgui\Profiler\ProfilingFlags; |
| 14 | + |
| 15 | +require __DIR__ . '/../vendor/autoload.php'; |
| 16 | + |
| 17 | +try { |
| 18 | + $config = array( |
| 19 | + // If defined, use specific profiler |
| 20 | + // otherwise use any profiler that's found |
| 21 | + 'profiler' => Profiler::PROFILER_TIDEWAYS_XHPROF, |
| 22 | + |
| 23 | + // This allows to configure, what profiling data to capture |
| 24 | + 'profiler.flags' => array( |
| 25 | + ProfilingFlags::CPU, |
| 26 | + ProfilingFlags::MEMORY, |
| 27 | + ProfilingFlags::NO_BUILTINS, |
| 28 | + ProfilingFlags::NO_SPANS, |
| 29 | + ), |
| 30 | + |
| 31 | + // Saver to use. |
| 32 | + // Please note that 'pdo' and 'mongo' savers are deprecated |
| 33 | + // Prefer 'upload' or 'file' saver. |
| 34 | + 'save.handler' => Profiler::SAVER_UPLOAD, |
| 35 | + |
| 36 | + // Environment variables to exclude from profiling data |
| 37 | + 'profiler.exclude-env' => array(), |
| 38 | + 'profiler.options' => array(), |
| 39 | + |
| 40 | + /** |
| 41 | + * Determine whether the profiler should run. |
| 42 | + * This default implementation just disables the profiler. |
| 43 | + * Override this with your custom logic in your config |
| 44 | + * @return bool |
| 45 | + */ |
| 46 | + 'profiler.enable' => function () { |
| 47 | + return true; |
| 48 | + }, |
| 49 | + |
| 50 | + /** |
| 51 | + * Creates a simplified URL given a standard URL. |
| 52 | + * Does the following transformations: |
| 53 | + * |
| 54 | + * - Remove numeric values after "=" in query string. |
| 55 | + * |
| 56 | + * @param string $url |
| 57 | + * @return string |
| 58 | + */ |
| 59 | + 'profiler.simple_url' => function ($url) { |
| 60 | + return preg_replace('/=\d+/', '', $url); |
| 61 | + }, |
| 62 | + |
| 63 | + /** |
| 64 | + * Enable this to clean up the url before submitting it to XHGui. |
| 65 | + * This way it is possible to remove sensitive data or discard any other data. |
| 66 | + * |
| 67 | + * The URL argument is the `REQUEST_URI` or `argv` value. |
| 68 | + * |
| 69 | + * @param string $url |
| 70 | + * @return string |
| 71 | + */ |
| 72 | + 'profiler.replace_url' => function ($url) { |
| 73 | + return str_replace('token', '', $url); |
| 74 | + }, |
| 75 | + ); |
| 76 | + |
| 77 | + /** |
| 78 | + * The constructor will throw an exception if the environment |
| 79 | + * isn't fit for profiling (extensions missing, other problems) |
| 80 | + */ |
| 81 | + $profiler = new Profiler($config); |
| 82 | + |
| 83 | + // The profiler itself checks whether it should be enabled |
| 84 | + // for request (executes lambda function from config) |
| 85 | + $profiler->start(); |
| 86 | +} catch (Exception $e) { |
| 87 | + // throw away or log error about profiling instantiation failure |
| 88 | + error_log($e->getMessage()); |
| 89 | +} |
0 commit comments