Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to control xhprof enable conditions #69

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions INSTALL.md

This file was deleted.

54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
To begin with, you need to install the XHProf extension. Refer to the [PHP documentation](http://www.php.net/manual/en/xhprof.setup.php) if you need assistance.

You will need to manually create the database and populate it with the provided scheme. The database scheme is located at `/setup/database.sql`.

Rename the `/xhprof/includes/config.inc.sample.php` to `/xhprof/includes/config.inc.php`. There are only two supported parameters.

* `xhprof_url` is the URL to the XHProf.io library.
* `pdo` is the PDO instance. This library uses [PDO](http://uk3.php.net/pdo) to handle all of the database operations.
* `enable` is a closure to control when enable data collection, return true means always enable

Some cases for reference:

Alway enable
``` php
'enable' => function() {
return true;
}
```

Enable if url contents `debug` parameter:
``` php
'enable' => function() {
if (!empty($_GET['debug'])) {
return true;
}
}
```

Enable for 1/100 probability
``` php
'enable' => function() {
return rand(0, 100) === 42;
}
```

Enable for url path is `/`:
``` php
'enable' => function() {
if (parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) === '/') {
return true;
}
}
```


For XHProf.io to start collecting data, you need `/inc/inject.php` files included to every file of interest. The recommended approach is to update your `php.ini` configuration to automatically prepend and append these files.

auto_prepend_file = /[absolute path to xhprof.io]/inc/inject.php

If you use nginx, you could configuration `auto_prepend_file` as a `fastcgi_param`

fastcgi_param PHP_VALUE "auto_prepend_file=/[absolute path to xhprof.io]/inc/inject.php";

If you are using PHP-FPM, then XHProf.io will utilise `fastcgi_finish_request` to hide any overhead related to data collection. There is nothing to worry about if you are not using PHP-FPM either, as the overhead is less than a few milliseconds.
16 changes: 16 additions & 0 deletions inc/inject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
if (extension_loaded('xhprof')) {
$xhprof_config = require __DIR__ . '/../xhprof/includes/config.inc.php';
if (!empty($xhprof_config['enable']) && $xhprof_config['enable']()) {
xhprof_enable(XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_CPU);
register_shutdown_function(function() use ($xhprof_config){
$xhprof_data = xhprof_disable();
if (function_exists('fastcgi_finish_request')) {
fastcgi_finish_request();
}
require_once __DIR__ . '/../xhprof/classes/data.php';
$xhprof_data_obj = new \ay\xhprof\Data($xhprof_config['pdo']);
$xhprof_data_obj->save($xhprof_data);
});
}
}
9 changes: 8 additions & 1 deletion xhprof/includes/config.inc.sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,11 @@
'url_base' => 'https://dev.anuary.com/8d50658e-f8e0-5832-9e82-6b9e8aa940ac/',
'url_static' => null, // When undefined, it defaults to $config['url_base'] . 'public/'. This should be absolute URL.
'pdo' => new PDO('mysql:dbname=your_database_name;host=localhost;charset=utf8', 'username', 'password'),
);
'enable' => function() {
//enable for 1/100 probability
//return rand(0, 100) === 42;

//enable always
return true;
}
);