diff --git a/CHANGELOG.md b/CHANGELOG.md index 39708b8..0e0a009 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ There are next changes: +## 1.3.1 + +There are next changes: + +- added example of sending profiling results to api + ## 1.3.0 There are next changes: diff --git a/README.md b/README.md index e6af480..c857450 100644 --- a/README.md +++ b/README.md @@ -45,14 +45,15 @@ Installation php composer.phar require badoo/liveprof ``` -* Prepare a database server. You can use any driver described [here](https://www.doctrine-project.org/projects/doctrine-dbal/en/2.8/reference/configuration.html#configuration) or implement the custom one -* Run a script to configure database. This script creates "details" table +* If you use DB mode you need to prepare a database server. You can use any driver described [here](https://www.doctrine-project.org/projects/doctrine-dbal/en/2.8/reference/configuration.html#configuration) or implement the custom one +* If you use DB mode you need run a script to configure database. This script creates "details" table ```bash LIVE_PROFILER_CONNECTION_URL=mysql://db_user:db_password@db_mysql:3306/Profiler?charset=utf8 php vendor/badoo/liveprof/bin/install.php ``` * It's also possible to save profiling result into files. To do it prepare a directory with write permissions. +* To use API mode you need to visit [liveprof.org](http://liveprof.org/) , sign in and get API key. * Init a profiler before working code in the project entry point (usually public/index.php). You should add a profiler call before your code to start profiling with default parameters: @@ -156,6 +157,11 @@ or latest hhvm with included **xhprof** extension: docker build -f DockerfileHHVM -t badoo/liveprof . docker run badoo/liveprof ``` +or if you want to use API with included **xhprof** extension: +```bash +docker build -f DockerfileUseApi -t badoo/liveprof . +docker run badoo/liveprof +``` If your server has php version 7.0 or later it's better to use [Tideways](https://github.com/tideways/php-profiler-extension) as profiler. diff --git a/bin/example_send_to_api.php b/bin/example_send_to_api.php new file mode 100644 index 0000000..ba642c7 --- /dev/null +++ b/bin/example_send_to_api.php @@ -0,0 +1,75 @@ + + */ + +if (file_exists(__DIR__ . '/../vendor/autoload.php')) { + require_once __DIR__ . '/../vendor/autoload.php'; +} elseif (file_exists(__DIR__ . '/../../../../vendor/autoload.php')) { + require_once __DIR__ . '/../../../../vendor/autoload.php'; +} + +use Badoo\LiveProfiler\LiveProfiler; + +// Initialize profiler with API mode +$path = '/tmp'; +$Profiler = new LiveProfiler($path, LiveProfiler::MODE_FILES); +$Profiler->setMode(LiveProfiler::MODE_API); +$Profiler->setApiKey('70366397-97d6-41be-a83c-e9e649c824e1'); +$Profiler->setApp('Test'); +$Profiler->setLabel('DockerTest'); +$Profiler->setDivider(1); + +// Run this method before profiled code +$Profiler->start(); + +// Start of profiled code +testCode(1); +// End of profiled code + +// Run this method after profiled code +$profiling_result = $Profiler->end(); + +echo $profiling_result ? "Profiling successfully finished\n" : "Error in profiling\n"; + + +/** + * Test functions + * @param int $level + */ +function testCode($level = 2) +{ + getSlower($level); + getFaster($level); +} + +/** + * @param int $level + * @return float + */ +function getSlower($level) +{ + $result = 0; + for ($i = 0; $i < $level; $i++) { + for ($j = 0; $j < 1000000; $j++) { + $result = $j * (1000000 - $j); + } + } + return $result; +} + +/** + * @param int $level + * @return float + */ +function getFaster($level) +{ + $result = 0; + for ($i = 0; $i < (10 - $level); $i++) { + for ($j = 0; $j < 1000000; $j++) { + $result = $i * (1000000 - $i); + } + } + return $result; +}