|
| 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 | + // Saving profile data by upload is only recommended with HTTPS |
| 37 | + // endpoints that have IP whitelists applied. |
| 38 | + // https://github.com/perftools/php-profiler#upload-saver |
| 39 | + 'save.handler.upload' => array( |
| 40 | + 'uri' => 'https://example.com/run/import', |
| 41 | + // The timeout option is in seconds and defaults to 3 if unspecified. |
| 42 | + 'timeout' => 3, |
| 43 | + // the token must match 'upload.token' config in XHGui |
| 44 | + 'token' => 'token', |
| 45 | + ), |
| 46 | + |
| 47 | + // https://github.com/perftools/php-profiler#file-saver |
| 48 | + 'save.handler.file' => array( |
| 49 | + // Appends jsonlines formatted data to this path |
| 50 | + 'filename' => '/tmp/xhgui.data.jsonl', |
| 51 | + ), |
| 52 | + |
| 53 | + // https://github.com/perftools/php-profiler#stack-saver |
| 54 | + 'save.handler.stack' => array( |
| 55 | + 'savers' => array( |
| 56 | + Profiler::SAVER_UPLOAD, |
| 57 | + Profiler::SAVER_FILE, |
| 58 | + ), |
| 59 | + // if saveAll=false, break the chain on successful save |
| 60 | + 'saveAll' => false, |
| 61 | + ), |
| 62 | + |
| 63 | + // https://github.com/perftools/php-profiler#mongodb-saver |
| 64 | + 'save.handler.mongodb' => array( |
| 65 | + 'dsn' => 'mongodb://127.0.0.1:27017', |
| 66 | + 'database' => 'xhprof', |
| 67 | + // Allows you to pass additional options like replicaSet to MongoClient. |
| 68 | + // 'username', 'password' and 'db' (where the user is added) |
| 69 | + 'options' => array(), |
| 70 | + // Allows you to pass driver options like ca_file to MongoClient |
| 71 | + 'driverOptions' => array(), |
| 72 | + ), |
| 73 | + |
| 74 | + // https://github.com/perftools/php-profiler#pdo-saver |
| 75 | + 'save.handler.pdo' => array( |
| 76 | + 'dsn' => 'sqlite:/tmp/xhgui.sqlite3', |
| 77 | + 'user' => null, |
| 78 | + 'pass' => null, |
| 79 | + 'table' => 'results' |
| 80 | + ), |
| 81 | + |
| 82 | + // Environment variables to exclude from profiling data |
| 83 | + 'profiler.exclude-env' => array(), |
| 84 | + 'profiler.options' => array(), |
| 85 | + |
| 86 | + /** |
| 87 | + * Determine whether the profiler should run. |
| 88 | + * This default implementation just disables the profiler. |
| 89 | + * Override this with your custom logic in your config |
| 90 | + * @return bool |
| 91 | + */ |
| 92 | + 'profiler.enable' => function () { |
| 93 | + return true; |
| 94 | + }, |
| 95 | + |
| 96 | + /** |
| 97 | + * Creates a simplified URL given a standard URL. |
| 98 | + * Does the following transformations: |
| 99 | + * |
| 100 | + * - Remove numeric values after "=" in query string. |
| 101 | + * |
| 102 | + * @param string $url |
| 103 | + * @return string |
| 104 | + */ |
| 105 | + 'profiler.simple_url' => function ($url) { |
| 106 | + return preg_replace('/=\d+/', '', $url); |
| 107 | + }, |
| 108 | + |
| 109 | + /** |
| 110 | + * Enable this to clean up the url before submitting it to XHGui. |
| 111 | + * This way it is possible to remove sensitive data or discard any other data. |
| 112 | + * |
| 113 | + * The URL argument is the `REQUEST_URI` or `argv` value. |
| 114 | + * |
| 115 | + * @param string $url |
| 116 | + * @return string |
| 117 | + */ |
| 118 | + 'profiler.replace_url' => function ($url) { |
| 119 | + return str_replace('token', '', $url); |
| 120 | + }, |
| 121 | + ); |
| 122 | + |
| 123 | + /** |
| 124 | + * The constructor will throw an exception if the environment |
| 125 | + * isn't fit for profiling (extensions missing, other problems) |
| 126 | + */ |
| 127 | + $profiler = new Profiler($config); |
| 128 | + |
| 129 | + // The profiler itself checks whether it should be enabled |
| 130 | + // for request (executes lambda function from config) |
| 131 | + $profiler->start(); |
| 132 | +} catch (Exception $e) { |
| 133 | + // throw away or log error about profiling instantiation failure |
| 134 | + error_log($e->getMessage()); |
| 135 | +} |
0 commit comments