Created
September 14, 2016 16:22
-
-
Save aufa/d4d3e25e595c0017a5237d6ecb9e0d19 to your computer and use it in GitHub Desktop.
Creating Simple BenchMark Collection on Slim 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Example | |
*/ | |
namespace MyNameSpace; | |
use Slim\App as Slim; | |
use Pentagonal\Component\Slim\BenchMark; | |
/** | |
* Class App | |
* @package MyNameSpace | |
*/ | |
class App | |
{ | |
/** | |
* @var \Slim\App | |
*/ | |
protected $slim; | |
/** | |
* App Constructor | |
*/ | |
public function __construct() | |
{ | |
$this->slim = new Slim([ | |
// fill with config | |
]); | |
$container = $this->slim->getContainer(); | |
$container['benchmark'] = new BenchMark(); | |
$container['benchmark']->start('app'); | |
} | |
/** | |
* @return \Slim\Container | |
*/ | |
public function getContainer() | |
{ | |
return $this->slim->getContainer(); | |
} | |
/** | |
* Get Application | |
* | |
* @param string $appName Application Container Name | |
* @apram mixed $default default return value if has no application | |
* @return mixed | |
*/ | |
public function get($appName, $default = null) | |
{ | |
return $this->has($appName) ? $this->getContainer()->get($appName) : $default; | |
} | |
/** | |
* @param string $appName Application Name | |
*/ | |
public function has($appName) | |
{ | |
if (!is_string($appName) && !is_numeric($appName)) { | |
return $default; | |
} | |
return $this->getContainer()->has($appName); | |
} | |
/** | |
* Magic Method getting property | |
* | |
* @param string $name | |
* @return mixed | |
*/ | |
public function __get($name) | |
{ | |
return $this->get($name); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Pentagonal\Component\Slim; | |
use Slim\Collection; | |
/** | |
* Class BenchMark | |
* @package Pentagonal\Component\Slim | |
*/ | |
class BenchMark | |
{ | |
/** | |
* @var Collection[] | |
*/ | |
protected $collection; | |
/** | |
* @var Collection[] | |
*/ | |
protected $old_collect; | |
/** | |
* BenchMark constructor. | |
*/ | |
public function __construct() | |
{ | |
$this->collection = new Collection(); | |
$this->old_collect = new Collection(); | |
} | |
/** | |
* @return Collection | |
*/ | |
protected function buildProperty() | |
{ | |
return new Collection([ | |
'memory' => memory_get_usage(), | |
'memory_real' => memory_get_usage(true), | |
'memory_peak' => memory_get_peak_usage(true), | |
'microtime' => microtime(true), | |
'microtime_string' => microtime(), | |
]); | |
} | |
/** | |
* @param string $name | |
*/ | |
public function start($name) | |
{ | |
if (!is_string($name)) { | |
return; | |
} | |
if (!$this->has($name)) { | |
$this->collection->set($name, $this->buildProperty()); | |
} | |
} | |
/** | |
* @param string $name | |
* @return bool | |
*/ | |
public function has($name) | |
{ | |
return $this->collection->has($name); | |
} | |
/** | |
* @param string $name | |
* @param null $default | |
* @return mixed | |
*/ | |
public function get($name, $default = null) | |
{ | |
return $this->collection->get($name, $default); | |
} | |
/** | |
* @param string $name | |
* @return Collection | |
*/ | |
public function current($name = null) | |
{ | |
$ret_val = new Collection(); | |
if ($name === null) { | |
foreach ($this->collection->all() as $key => $v) { | |
$ret_val->set( | |
$key, | |
new Collection([ | |
'time_start' => $v['microtime'], | |
'time_elapsed' => (microtime(true) - $v['microtime']), | |
'memory_start' => $v['memory'], | |
'memory' => (memory_get_usage() - $v['memory']), | |
'memory_real_start' => $v['memory_real'], | |
'memory_real' => (memory_get_usage(true) - $v['memory_real']), | |
'memory_peak_start'=> $v['memory_peak'], | |
'memory_peak' => (memory_get_peak_usage(true) - $v['memory_peak']), | |
]) | |
); | |
if ($this->old_collect->has($key)) { | |
$this->old_collect->set($key, $ret_val->get($key)); | |
} | |
} | |
return $ret_val; | |
} | |
if (is_string($name) && $this->collection->has($name)) { | |
$v = $this->collection->get($name); | |
$ret_val->replace( | |
[ | |
'time_start' => $v['microtime'], | |
'time_elapsed' => (microtime(true) - $v['microtime']), | |
'memory_start' => $v['memory'], | |
'memory' => (memory_get_usage() - $v['memory']), | |
'memory_real_start' => $v['memory_real'], | |
'memory_real' => (memory_get_usage(true) - $v['memory_real']), | |
'memory_peak_start'=> $v['memory_peak'], | |
'memory_peak' => (memory_get_peak_usage(true) - $v['memory_peak']), | |
] | |
); | |
if (!$this->old_collect->has($name)) { | |
$this->old_collect->set($name, $ret_val); | |
} | |
} | |
return $ret_val; | |
} | |
/** | |
* @param string $name | |
* @return Collection | |
*/ | |
public function getBenchMarkOf($name = null) | |
{ | |
if ($name === null) { | |
foreach ($this->collection->all() as $key => $v) { | |
if (!$this->old_collect->has($key)) { | |
$this->old_collect->set( | |
$key, | |
new Collection([ | |
'time_start' => $v['microtime'], | |
'time_elapsed' => (microtime(true) - $v['microtime']), | |
'memory_start' => $v['memory'], | |
'memory' => (memory_get_usage() - $v['memory']), | |
'memory_real_start' => $v['memory_real'], | |
'memory_real' => (memory_get_usage(true) - $v['memory_real']), | |
'memory_peak_start' => $v['memory_peak'], | |
'memory_peak' => (memory_get_peak_usage(true) - $v['memory_peak']), | |
]) | |
); | |
} | |
} | |
return $this->old_collect; | |
} | |
if (is_string($name) && $this->collection->has($name)) { | |
$v = $this->collection->get($name); | |
if (!$this->old_collect->has($name)) { | |
$this->old_collect->set( | |
$name, | |
new Collection([ | |
'time_start' => $v['microtime'], | |
'time_elapsed' => (microtime(true) - $v['microtime']), | |
'memory_start' => $v['memory'], | |
'memory' => (memory_get_usage() - $v['memory']), | |
'memory_real_start' => $v['memory_real'], | |
'memory_real' => (memory_get_usage(true) - $v['memory_real']), | |
'memory_peak_start' => $v['memory_peak'], | |
'memory_peak' => (memory_get_peak_usage(true) - $v['memory_peak']), | |
]) | |
); | |
} | |
return $this->old_collect; | |
} | |
return new Collection(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use MyNameSpace\App; | |
$app = new App(); | |
// call as property and used magic method __get() | |
$benchmark = $app->benchmark; | |
/** | |
* @var \Slim\Collection | |
*/ | |
$benchmarkcollection = $benchmark->current('app'); | |
echo "<p>time elapsed : {$benchmarkcollection->get('time_elapsed')}</p>\n"; | |
echo "<p>memory used : {$benchmarkcollection->get('memory')}</p>\n" | |
/** | |
* Different use BenchMark::getBenchMarkOf() & BenchMark::current(); | |
* BenchMark::getBenchMarkOf() -> use old value setted if possible | |
* BenchMark::current() -> use current time or memory execution | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment