From fee54d8be9217666ab63523004ad9d0a53a405b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Scho=CC=88ps?= Date: Tue, 14 Apr 2020 23:34:31 +0200 Subject: [PATCH] Add time measurements --- README.md | 9 +++++++++ src/Loggy.php | 27 ++++++++++++++++++++++++++- src/LoggyServiceProvider.php | 2 +- src/helpers.php | 20 ++++++++++++++++++++ 4 files changed, 56 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d771ed8..96801e6 100644 --- a/README.md +++ b/README.md @@ -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! diff --git a/src/Loggy.php b/src/Loggy.php index 2dab28c..04ed37a 100644 --- a/src/Loggy.php +++ b/src/Loggy.php @@ -9,6 +9,8 @@ class Loggy protected $url; protected $key; + protected $measurements = []; + public function __construct($url, $key) { if(strlen($key) != config('loggy.key-length')) { @@ -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); diff --git a/src/LoggyServiceProvider.php b/src/LoggyServiceProvider.php index 4d304d7..627ab55 100644 --- a/src/LoggyServiceProvider.php +++ b/src/LoggyServiceProvider.php @@ -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')); }); } diff --git a/src/helpers.php b/src/helpers.php index 7773847..d9b7b23 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -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); + } } \ No newline at end of file