node: add RTT and bandwidth measurement holders

Signed-off-by: Csaba Kiraly <csaba.kiraly@gmail.com>
This commit is contained in:
Csaba Kiraly 2024-10-07 21:33:45 +02:00
parent b8bcb2d08d
commit f3eec2a202
No known key found for this signature in database
GPG Key ID: 0FE274EE8C95166E

View File

@ -20,6 +20,9 @@ import
export stint
const
avgSmoothingFactor = 0.9
type
NodeId* = UInt256
@ -27,6 +30,12 @@ type
ip*: ValidIpAddress
port*: Port
Stats* = object
rttMin*: float #millisec
rttAvg*: float #millisec
bwAvg*: float #bps
bwMax*: float #bps
Node* = ref object
id*: NodeId
pubkey*: PublicKey
@ -35,6 +44,7 @@ type
seen*: bool ## Indicates if there was at least one successful
## request-response with this node, or if the nde was verified
## through the underlying transport mechanisms.
stats*: Stats # traffic measurements and statistics
func toNodeId*(pid: PeerId): NodeId =
## Convert public key to a node identifier.
@ -182,3 +192,21 @@ func shortLog*(address: Address): string =
$address
chronicles.formatIt(Address): shortLog(it)
# collecting performane metrics
func registerRtt*(n: Node, rtt: Duration) =
## register an RTT measurement
let rttMs = rtt.nanoseconds.float / 1e6
n.stats.rttMin =
if n.stats.rttMin == 0: rttMs
else: min(n.stats.rttMin, rttMs)
n.stats.rttAvg =
if n.stats.rttAvg == 0: rttMs
else: avgSmoothingFactor * n.stats.rttAvg + (1.0 - avgSmoothingFactor) * rttMs
func registerBw*(n: Node, bw: float) =
## register an bandwidth measurement
n.stats.bwMax = max(n.stats.bwMax, bw)
n.stats.bwAvg =
if n.stats.bwAvg == 0: bw
else: avgSmoothingFactor * n.stats.bwAvg + (1.0 - avgSmoothingFactor) * bw