Skip to content

Commit

Permalink
added example of sending profiling results to api
Browse files Browse the repository at this point in the history
  • Loading branch information
shagtv committed Mar 22, 2019
1 parent 4cb0fbf commit e4d067f
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.

Expand Down
75 changes: 75 additions & 0 deletions bin/example_send_to_api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/**
* Runs profiling test code
* @maintainer Timur Shagiakhmetov <timur.shagiakhmetov@corp.badoo.com>
*/

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;
}

0 comments on commit e4d067f

Please sign in to comment.