Skip to content

Commit

Permalink
Add time measurements
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Schöps committed Apr 14, 2020
1 parent 7fa2c73 commit fee54d8
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ loggy($myMessage);
```
`myMessage` can be anything that can be translated into JSON (usually you would use a string or an array).

### Time measurement
You can start and stop a time measurement with
```
loggy_start('name-of-time-measurement');
do_something();
loggy_stop('name-of-time-measurement');
```
The name is optional and defaults to "Measurement".

## More information
### Why?
When starting to work with Laravel Vapor we realized how great this product of the Laravel team is!
Expand Down
27 changes: 26 additions & 1 deletion src/Loggy.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class Loggy
protected $url;
protected $key;

protected $measurements = [];

public function __construct($url, $key)
{
if(strlen($key) != config('loggy.key-length')) {
Expand All @@ -33,12 +35,35 @@ public function send($message)
]);
}

/**
* @param $name
*/
public function startMeasurement($name = 'Measurement')
{
$this->measurements[$name] = microtime(true);
}

/**
* @param $name
*/
public function stopMeasurement($name = 'Measurement')
{
$time = microtime(true);

if(array_key_exists($name, $this->measurements)) {
$this->send('[' . $name . '] ' . number_format($time - $this->measurements[$name], 4));
}
else {
$this->send('[' . $name . '] Measurement was not started');
}
}

/**
*
* @param $url
* @param $params
*/
public function postWithoutWait($url, $params)
protected function postWithoutWait($url, $params)
{
$parts=parse_url($url);
$data= json_encode($params);
Expand Down
2 changes: 1 addition & 1 deletion src/LoggyServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function register()
{
$this->mergeConfigFrom(__DIR__.'/../config/loggy.php', 'loggy');

$this->app->bind('loggy', function(){
$this->app->singleton('loggy', function(){
return new Loggy(config('loggy.url'), config('loggy.key'));
});
}
Expand Down
20 changes: 20 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,24 @@ function loggy($message)
{
return Loggy::send($message);
}
}

if (! function_exists('loggy_start')) {
/**
* Start a time measurement
*/
function loggy_start($name = 'Measurement')
{
return Loggy::startMeasurement($name);
}
}

if (! function_exists('loggy_stop')) {
/**
* Stop and send a time measurement
*/
function loggy_stop($name = 'Measurement')
{
return Loggy::stopMeasurement($name);
}
}

0 comments on commit fee54d8

Please sign in to comment.