From 271d6140ef5e6451541c964a4546f9be345348a0 Mon Sep 17 00:00:00 2001 From: Nadyita Date: Thu, 7 Mar 2024 10:08:16 +0100 Subject: [PATCH] Detailed read stats --- src/Client/Statistics.php | 12 +++++++++--- src/Connection.php | 3 ++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/Client/Statistics.php b/src/Client/Statistics.php index 3ca757f..72dfc87 100644 --- a/src/Client/Statistics.php +++ b/src/Client/Statistics.php @@ -3,9 +3,12 @@ namespace AO\Client; class Statistics { - /** @param array $packagesWritten */ + /** + * @param array $packagesRead + * @param array $packagesWritten + */ public function __construct( - public int $packagesRead=0, + public array $packagesRead=[], public int $bytesRead=0, public array $packagesWritten=[], public int $bytesWritten=0, @@ -14,7 +17,7 @@ public function __construct( public function add(self $statistic): self { $combinedStats = new self( - packagesRead: $this->packagesRead + $statistic->packagesRead, + packagesRead: $this->packagesRead, packagesWritten: $this->packagesWritten, bytesRead: $this->bytesRead + $statistic->bytesRead, bytesWritten: $this->bytesWritten + $statistic->bytesWritten, @@ -22,6 +25,9 @@ public function add(self $statistic): self { foreach ($statistic->packagesWritten as $type => $count) { $combinedStats->packagesWritten[$type] += $count; } + foreach ($statistic->packagesRead as $type => $count) { + $combinedStats->packagesRead[$type] += $count; + } return $combinedStats; } } diff --git a/src/Connection.php b/src/Connection.php index 28c1e78..6a26fba 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -41,7 +41,6 @@ public function read(?Cancellation $cancellation=null): ?BinaryPackage\In { if ($binPackage === null) { return null; } - $this->statistics->packagesRead++; $this->statistics->bytesRead += \strlen($binPackage); $header = unpack("ntype/nlength", $binPackage); $package = new BinaryPackage\In( @@ -49,6 +48,8 @@ public function read(?Cancellation $cancellation=null): ?BinaryPackage\In { length: $header['length'], body: substr($binPackage, 4), ); + $this->statistics->packagesRead[$package->type->value] ??= 0; + $this->statistics->packagesRead[$package->type->value]++; $this->logger?->debug("Received {package}", ["package" => $package]); return $package; }