Skip to content

Commit

Permalink
Detailed read stats
Browse files Browse the repository at this point in the history
  • Loading branch information
Nadyita committed Mar 7, 2024
1 parent ed2bddd commit 271d614
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
12 changes: 9 additions & 3 deletions src/Client/Statistics.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
namespace AO\Client;

class Statistics {
/** @param array<int,int> $packagesWritten */
/**
* @param array<int,int> $packagesRead
* @param array<int,int> $packagesWritten
*/
public function __construct(
public int $packagesRead=0,
public array $packagesRead=[],
public int $bytesRead=0,
public array $packagesWritten=[],
public int $bytesWritten=0,
Expand All @@ -14,14 +17,17 @@ 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,
);
foreach ($statistic->packagesWritten as $type => $count) {
$combinedStats->packagesWritten[$type] += $count;
}
foreach ($statistic->packagesRead as $type => $count) {
$combinedStats->packagesRead[$type] += $count;
}
return $combinedStats;
}
}
3 changes: 2 additions & 1 deletion src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,15 @@ 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(
type: Type::from($header['type']),
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;
}
Expand Down

0 comments on commit 271d614

Please sign in to comment.