Skip to content

Commit

Permalink
Debug and improvement of timing
Browse files Browse the repository at this point in the history
  • Loading branch information
Andygrond committed Dec 28, 2023
1 parent 94f056a commit 31bc8b9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
13 changes: 11 additions & 2 deletions lib/Helpers/Duration.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,26 @@ public function stop(string $name)
}

// get array of all duration times
public function timeLen(): array
public function timing(): array
{
$this->times['run']['duration'] = microtime(true) - $this->times['run']['start'];

$len = [];
if ($this->times) {
foreach ($this->times as $name => $frame) {
$len[] = $name .':' .round(1000 * $frame['duration']);
$len[$name] = $this->msToTime($frame['duration']);
}
}
return $len;
}

// Time distance given in a user-friendly form
private static function msToTime($delta)
{
$lo = ($delta > 10)? 1 : (($delta > 1)? 2 : 3);
$answer = ($delta > 0.8)? round($delta, $lo) .' s' : 1000*round($delta, 3) .' ms';
$info = ($delta > 90)? round($delta/60, 1) .' min' : $answer;
return $info;
}

}
8 changes: 4 additions & 4 deletions lib/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static function __callStatic(string $level, array $args)
public static function close()
{
if (self::$logger) {
self::$logger->debug('Duration', self::$duration->timeLen());
self::$logger->debug('Duration', self::$duration->timing());
self::$logger->flush();
}
}
Expand All @@ -93,7 +93,7 @@ public static function done(string $name)
if ($name == $lastName) {
self::$duration->stop($name);
} else {
self::$logger->log('warning', "Job $name is interlacing with $lastName which should be ended first using Log::done.");
self::$logger->log('warning', "Job $name is interlacing with $lastName which should be ended first using Log::done('$lastName')");
}
}

Expand Down Expand Up @@ -126,9 +126,9 @@ public static function enable(string $name)
}

// measured time lengths
public static function times()
public static function timing()
{
self::$duration->timeLen();
return self::$duration->timing();
}

// prevented instantiating
Expand Down

0 comments on commit 31bc8b9

Please sign in to comment.