Skip to content

Commit

Permalink
added memory usage
Browse files Browse the repository at this point in the history
  • Loading branch information
shagtv committed Feb 6, 2020
1 parent 6a1274f commit cd15380
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 14 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

There are next changes:

## 1.3.4

There are next changes:

- added a memory usage for SimpleProfiler
- bug fixes

## 1.3.3

There are next changes:
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,12 @@ There is a full list of methods you can use to change options:
->setDataPacker($DatePacker) // optional, a class implemented \Badoo\LiveProfiler\DataPackerInterface to convert array into string
->setStartCallback($profiler_start_callback) // optional, set it if you use custom profiler
->setEndCallback($profiler_profiler_callback) // optional, set it if you use custom profiler
->useXhprof() // optional, force use xhprof as profiler
->useTidyWays() // optional, force use TidyWays as profiler
->useUprofiler() // optional, force use uprofiler as profiler
->useSimpleProfiler() // optional, force use internal profiler
->useXhprofSample() // optional, force use xhprof in sampling mode
->start();
\Badoo\LiveProfiler\LiveProfiler::getInstance()->useXhprofSample(); // optional, force use xhprof in sampling mode
```

If you want to change the Label during running (for instance, after you got some information in the router or controller) you can call:
Expand Down
4 changes: 4 additions & 0 deletions src/Badoo/LiveProfiler/LiveProfiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ public function end()
return false;
}

if (empty($data)) {
return false;
}

$this->last_profile_data = $data;
$result = $this->save($this->app, $this->label, $this->datetime, $data);

Expand Down
5 changes: 5 additions & 0 deletions src/Badoo/LiveProfiler/SimpleProfiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,15 @@ public function startTimer($tag)
if (!isset($this->methods[$full_method])) {
$this->methods[$full_method] = [
'wt' => 0,
'mu' => 0,
'ct' => 0,
];
}

$time = (int)(microtime(true) * 1e6);
$memory = memory_get_usage();
$this->methods[$full_method]['wt'] = $time - $this->methods[$full_method]['wt'];
$this->methods[$full_method]['mu'] = $memory - $this->methods[$full_method]['mu'];

return true;
}
Expand All @@ -85,8 +88,10 @@ public function endTimer($tag)
$full_method = $this->getFullMethod($tag);

$time = (int)(microtime(true) * 1e6);
$memory = memory_get_usage();
$this->methods[$full_method]['ct']++;
$this->methods[$full_method]['wt'] = $time - $this->methods[$full_method]['wt'];
$this->methods[$full_method]['mu'] = $memory - $this->methods[$full_method]['mu'];

return true;
}
Expand Down
27 changes: 14 additions & 13 deletions tests/unit/Badoo/LiveProfiler/LiveProfilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function testStart()
$ProfilerMock
->setDivider(1)
->setStartCallback(function () {
});
});

$result = $ProfilerMock->start();
self::assertTrue($result);
Expand Down Expand Up @@ -98,7 +98,7 @@ public function testStartTotal()
$ProfilerMock
->setTotalDivider(1)
->setStartCallback(function () {
});
});

$result = $ProfilerMock->start();
self::assertTrue($result);
Expand All @@ -124,8 +124,8 @@ public function testEnd()
$ProfilerMock
->setDataPacker($DataPacker)
->setEndCallback(function () {
return ['end result'];
});
return ['end result'];
});

$result = $ProfilerMock->end();
self::assertTrue($result);
Expand Down Expand Up @@ -469,7 +469,7 @@ public function testUseXhprofSample()
/** @var \Badoo\LiveProfiler\LiveProfiler $ProfilerMock */
$result = $ProfilerMock->useXhprofSample();

self::assertTrue($result);
self::assertInstanceOf('\Badoo\LiveProfiler\LiveProfiler', $result);
$start_callback = $this->getProtectedProperty($ProfilerMock, 'start_callback');
$end_callback = $this->getProtectedProperty($ProfilerMock, 'end_callback');
self::assertNotEmpty($start_callback);
Expand Down Expand Up @@ -502,7 +502,7 @@ public function testUseXhprofSampleAfterStart()
->setLogger($LoggerMock)
->useXhprofSample();

self::assertFalse($result);
self::assertInstanceOf('\Badoo\LiveProfiler\LiveProfiler', $result);
}

/**
Expand All @@ -518,7 +518,8 @@ public function testUseXhprof()
/** @var \Badoo\LiveProfiler\LiveProfiler $ProfilerMock */
$result = $ProfilerMock->useXhprof();

self::assertTrue($result);
self::assertInstanceOf('\Badoo\LiveProfiler\LiveProfiler', $result);

$start_callback = $this->getProtectedProperty($ProfilerMock, 'start_callback');
$end_callback = $this->getProtectedProperty($ProfilerMock, 'end_callback');
self::assertNotEmpty($start_callback);
Expand Down Expand Up @@ -551,7 +552,7 @@ public function testUseXhprofAfterStart()
->setLogger($LoggerMock)
->useXhprof();

self::assertFalse($result);
self::assertInstanceOf('\Badoo\LiveProfiler\LiveProfiler', $result);
}

/**
Expand All @@ -567,7 +568,7 @@ public function testUseUprofiler()
/** @var \Badoo\LiveProfiler\LiveProfiler $ProfilerMock */
$result = $ProfilerMock->useUprofiler();

self::assertTrue($result);
self::assertInstanceOf('\Badoo\LiveProfiler\LiveProfiler', $result);
$start_callback = $this->getProtectedProperty($ProfilerMock, 'start_callback');
$end_callback = $this->getProtectedProperty($ProfilerMock, 'end_callback');
self::assertNotEmpty($start_callback);
Expand Down Expand Up @@ -600,7 +601,7 @@ public function testUseUprofilerAfterStart()
->setLogger($LoggerMock)
->useUprofiler();

self::assertFalse($result);
self::assertInstanceOf('\Badoo\LiveProfiler\LiveProfiler', $result);
}

/**
Expand All @@ -616,7 +617,7 @@ public function testUseTidyWays()
/** @var \Badoo\LiveProfiler\LiveProfiler $ProfilerMock */
$result = $ProfilerMock->useTidyWays();

self::assertTrue($result);
self::assertInstanceOf('\Badoo\LiveProfiler\LiveProfiler', $result);
$start_callback = $this->getProtectedProperty($ProfilerMock, 'start_callback');
$end_callback = $this->getProtectedProperty($ProfilerMock, 'end_callback');
self::assertNotEmpty($start_callback);
Expand Down Expand Up @@ -649,7 +650,7 @@ public function testUseTidyWaysAfterStart()
->setLogger($LoggerMock)
->useTidyWays();

self::assertFalse($result);
self::assertInstanceOf('\Badoo\LiveProfiler\LiveProfiler', $result);
}

public function testDetectProfiler()
Expand All @@ -662,6 +663,6 @@ public function testDetectProfiler()
/** @var \Badoo\LiveProfiler\LiveProfiler $ProfilerMock */
$result = $ProfilerMock->detectProfiler();

self::assertInternalType('bool', $result);
self::assertInstanceOf('\Badoo\LiveProfiler\LiveProfiler', $result);
}
}
54 changes: 54 additions & 0 deletions tests/unit/Badoo/LiveProfiler/SimpleProfilerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/**
* @maintainer Timur Shagiakhmetov <timur.shagiakhmetov@corp.badoo.com>
*/

namespace unit\Badoo\LiveProfiler;

class SimpleProfilerTest extends \unit\Badoo\BaseTestCase
{
public function testRunWithoutTags()
{
\Badoo\LiveProfiler\SimpleProfiler::getInstance()->enable();
$data = \Badoo\LiveProfiler\SimpleProfiler::getInstance()->disable();

self::assertEquals(['main()'], array_keys($data));
self::assertEquals(['wt', 'mu', 'ct'], array_keys($data['main()']));
}

public function testRunWithTag()
{
\Badoo\LiveProfiler\SimpleProfiler::getInstance()->enable();

\Badoo\LiveProfiler\SimpleProfiler::getInstance()->startTimer('tag');
\Badoo\LiveProfiler\SimpleProfiler::getInstance()->endTimer('tag');

$data = \Badoo\LiveProfiler\SimpleProfiler::getInstance()->disable();

self::assertEquals(['main()', 'main()==>tag'], array_keys($data));
}

public function testNotClosedTag()
{
\Badoo\LiveProfiler\SimpleProfiler::getInstance()->enable();

\Badoo\LiveProfiler\SimpleProfiler::getInstance()->startTimer('tag');

$data = \Badoo\LiveProfiler\SimpleProfiler::getInstance()->disable();

self::assertEquals([], $data);
}

public function testInvalidClosedTag()
{
\Badoo\LiveProfiler\SimpleProfiler::getInstance()->enable();

\Badoo\LiveProfiler\SimpleProfiler::getInstance()->startTimer('tag');
\Badoo\LiveProfiler\SimpleProfiler::getInstance()->endTimer('invalid');

$data = \Badoo\LiveProfiler\SimpleProfiler::getInstance()->disable();

self::assertEquals([], $data);
}
}

0 comments on commit cd15380

Please sign in to comment.