From bf84caa02c00a5ef3f280b35f8bd8a199235ff28 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Thu, 6 Apr 2023 15:05:40 +0100 Subject: [PATCH] Timings: record peak tick time and active ticks this information is useful for determining the sizes of lag spikes, and giving more accurate average times. --- src/timings/TimingsHandler.php | 4 +++- src/timings/TimingsRecord.php | 20 ++++++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/timings/TimingsHandler.php b/src/timings/TimingsHandler.php index 231325c3e..e20adb672 100644 --- a/src/timings/TimingsHandler.php +++ b/src/timings/TimingsHandler.php @@ -58,7 +58,9 @@ class TimingsHandler{ "Violations: " . $timings->getViolations(), "RecordId: " . $timings->getId(), "ParentRecordId: " . ($timings->getParentId() ?? "none"), - "TimerId: " . $timings->getTimerId() + "TimerId: " . $timings->getTimerId(), + "Ticks: " . $timings->getTicksActive(), + "Peak: " . $timings->getPeakTime(), ]); } $result = []; diff --git a/src/timings/TimingsRecord.php b/src/timings/TimingsRecord.php index 84b2c8a2b..5254d2e7d 100644 --- a/src/timings/TimingsRecord.php +++ b/src/timings/TimingsRecord.php @@ -59,11 +59,17 @@ final class TimingsRecord{ public static function tick(bool $measure = true) : void{ if($measure){ foreach(self::$records as $record){ - if($record->curTickTotal > Server::TARGET_NANOSECONDS_PER_TICK){ - $record->violations += (int) floor($record->curTickTotal / Server::TARGET_NANOSECONDS_PER_TICK); + if($record->curCount > 0){ + if($record->curTickTotal > Server::TARGET_NANOSECONDS_PER_TICK){ + $record->violations += (int) floor($record->curTickTotal / Server::TARGET_NANOSECONDS_PER_TICK); + } + if($record->curTickTotal > $record->peakTime){ + $record->peakTime = $record->curTickTotal; + } + $record->curTickTotal = 0; + $record->curCount = 0; + $record->ticksActive++; } - $record->curTickTotal = 0; - $record->curCount = 0; } }else{ foreach(self::$records as $record){ @@ -82,6 +88,8 @@ final class TimingsRecord{ private int $totalTime = 0; private int $curTickTotal = 0; private int $violations = 0; + private int $ticksActive = 0; + private int $peakTime = 0; public function __construct( //I'm not the biggest fan of this cycle, but it seems to be the most effective way to avoid leaking anything. @@ -113,6 +121,10 @@ final class TimingsRecord{ public function getViolations() : int{ return $this->violations; } + public function getTicksActive() : int{ return $this->ticksActive; } + + public function getPeakTime() : float{ return $this->peakTime; } + public function startTiming(int $now) : void{ $this->start = $now; self::$currentRecord = $this;