Skip to content

Commit 0f643c9

Browse files
authored
Merge pull request #44 from perftools/autoload-example
2 parents 112ec60 + 1bebbd1 commit 0f643c9

2 files changed

Lines changed: 146 additions & 69 deletions

File tree

README.md

Lines changed: 11 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ In order to profile your application, you need to:
2525
- [Install this package](#installation)
2626
- [Install profiler extension](#installing-profilers)
2727
- [Instantiate the profiler](#create-profiler)
28-
- [Configure profiler to send data to XHGui](#config)
28+
- [Configure saver to send data to XHGui](#savers)
2929

3030
## Installation
3131

@@ -90,83 +90,19 @@ $profiler->save($profiler_data);
9090

9191
## Config
9292

93-
Here's a reference config of what can be configured.
93+
Reference config of what can be configured:
9494

95-
```php
96-
<?php
97-
$config = array(
98-
// If defined, use specific profiler
99-
// otherwise use any profiler that's found
100-
'profiler' => \Xhgui\Profiler\Profiler::PROFILER_TIDEWAYS_XHPROF,
101-
102-
// This allows to configure, what profiling data to capture
103-
'profiler.flags' => array(
104-
\Xhgui\Profiler\ProfilingFlags::CPU,
105-
\Xhgui\Profiler\ProfilingFlags::MEMORY,
106-
\Xhgui\Profiler\ProfilingFlags::NO_BUILTINS,
107-
\Xhgui\Profiler\ProfilingFlags::NO_SPANS,
108-
),
109-
110-
// Saver to use.
111-
// Please note that 'pdo' and 'mongo' savers are deprecated
112-
// Prefer 'upload' or 'file' saver.
113-
'save.handler' => \Xhgui\Profiler\Profiler::SAVER_UPLOAD,
114-
115-
// Environment variables to exclude from profiling data
116-
'profiler.exclude-env' => array(
117-
'APP_DATABASE_PASSWORD',
118-
'PATH',
119-
),
120-
121-
'profiler.options' => array(
122-
),
95+
- [examples/autoload.php](examples/autoload.php)
12396

124-
/**
125-
* Determine whether the profiler should run.
126-
* This default implementation profiles every request.
127-
* Override this with your custom logic in your config.
128-
*
129-
* @see https://github.com/perftools/php-profiler#configure-profiling-rate
130-
* @return bool
131-
*/
132-
'profiler.enable' => function () {
133-
return true;
134-
},
135-
136-
/**
137-
* Creates a simplified URL given a standard URL.
138-
* Does the following transformations:
139-
*
140-
* - Remove numeric values after "=" in query string.
141-
*
142-
* @param string $url
143-
* @return string
144-
*/
145-
'profiler.simple_url' => function($url) {
146-
return preg_replace('/=\d+/', '', $url);
147-
},
148-
149-
/**
150-
* Enable this to clean up the url before submitting it to XHGui.
151-
* This way it is possible to remove sensitive data or discard any other data from the url or command line.
152-
*
153-
* The URL argument is the `REQUEST_URI` or `argv` value.
154-
*
155-
* @param string $url
156-
* @return string
157-
*/
158-
'profiler.replace_url' => function($url) {
159-
return str_replace('token', '', $url);
160-
},
161-
);
162-
```
97+
It includes all configiration optioms and inline documentation about the options.
16398

16499
## Savers
165100

166101
To deliver captured data to XHGui, you will need one of the savers to submit to the datastore XHGui uses.
167102

168103
- [Upload saver](#upload-saver)
169104
- [File saver](#file-saver)
105+
- [Stack saver](#stack-saver)
170106
- [MongoDB Saver](#mongodb-saver)
171107
- [PDO Saver](#pdo-saver)
172108

@@ -393,6 +329,12 @@ calls for finishing profiling and storing the data.
393329
For this library to capture profiling data, you would need any of the profiler extension.
394330
Depending on your environment (PHP version), you may need to install different extension.
395331

332+
Supported profilers:
333+
- [Tideways XHProf v5.x](#tideways-xhprof-5): PHP >= 7.0
334+
- [XHProf](#xhprof): PHP >= 5.3, PHP >= 7.0
335+
- [Tideways v4.x](#tideways-4x): PHP >= 7.0
336+
- [UProfiler](#uprofiler): PHP >= 5.3, < PHP 7.0
337+
396338
### XHProf
397339

398340
[XHProf] supports all PHP versions.

examples/autoload.php

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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

Comments
 (0)