Skip to content

Commit 4c7c8a1

Browse files
authored
chore: Improving liteprotocolteseter stats (#2750)
1 parent dfc979a commit 4c7c8a1

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

apps/liteprotocoltester/statistics.nim

+15-11
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type
1717
prevArrivedAt: Moment
1818
lostIndices: HashSet[uint32]
1919
seenIndices: HashSet[uint32]
20+
maxIndex: uint32
2021

2122
Statistics* = object
2223
allMessageCount*: uint32
@@ -42,6 +43,7 @@ func `$`*(a: Duration): string {.inline.} =
4243

4344
proc init*(T: type Statistics, expectedMessageCount: int = 1000): T =
4445
result.helper.prevIndex = 0
46+
result.helper.maxIndex = 0
4547
result.helper.seenIndices.init(expectedMessageCount)
4648
result.minLatency = nanos(0)
4749
result.maxLatency = nanos(0)
@@ -100,6 +102,7 @@ proc addMessage*(self: var Statistics, msg: ProtocolTesterMessage) =
100102
warn "Negative latency detected",
101103
index = msg.index, expected = expectedDelay, actual = delaySincePrevArrived
102104

105+
self.helper.maxIndex = max(self.helper.maxIndex, msg.index)
103106
self.helper.prevIndex = msg.index
104107
self.helper.prevArrivedAt = currentArrivedAt
105108
inc(self.receivedMessages)
@@ -114,7 +117,7 @@ proc addMessage*(
114117
self[peerId].addMessage(msg)
115118

116119
proc lossCount*(self: Statistics): uint32 =
117-
self.allMessageCount - self.receivedMessages
120+
self.helper.maxIndex - self.receivedMessages
118121

119122
proc averageLatency*(self: Statistics): Duration =
120123
if self.receivedMessages == 0:
@@ -123,15 +126,15 @@ proc averageLatency*(self: Statistics): Duration =
123126

124127
proc echoStat*(self: Statistics) =
125128
let printable = catch:
126-
"""*-----------------------------------------------------------------------------*
127-
| Expected | Reveived | Loss | Misorder | Late | Duplicate |
128-
|{self.allMessageCount:>11} |{self.receivedMessages:>11} |{self.lossCount():>11} |{self.misorderCount:>11} |{self.lateCount:>11} |{self.duplicateCount:>11} |
129-
*-----------------------------------------------------------------------------*
130-
| Latency stat: |
131-
| avg latency: {$self.averageLatency():<60}|
132-
| min latency: {$self.maxLatency:<60}|
133-
| max latency: {$self.minLatency:<60}|
134-
*-----------------------------------------------------------------------------*""".fmt()
129+
"""*------------------------------------------------------------------------------------------*
130+
| Expected | Received | Target | Loss | Misorder | Late | Duplicate |
131+
|{self.helper.maxIndex:>11} |{self.receivedMessages:>11} |{self.allMessageCount:>11} |{self.lossCount():>11} |{self.misorderCount:>11} |{self.lateCount:>11} |{self.duplicateCount:>11} |
132+
*------------------------------------------------------------------------------------------*
133+
| Latency stat: |
134+
| avg latency: {$self.averageLatency():<73}|
135+
| min latency: {$self.maxLatency:<73}|
136+
| max latency: {$self.minLatency:<73}|
137+
*------------------------------------------------------------------------------------------*""".fmt()
135138

136139
if printable.isErr():
137140
echo "Error while printing statistics: " & printable.error().msg
@@ -140,8 +143,9 @@ proc echoStat*(self: Statistics) =
140143

141144
proc jsonStat*(self: Statistics): string =
142145
let json = catch:
143-
"""{{"expected":{self.allMessageCount},
146+
"""{{"expected":{self.helper.maxIndex},
144147
"received": {self.receivedMessages},
148+
"target": {self.allMessageCount},
145149
"loss": {self.lossCount()},
146150
"misorder": {self.misorderCount},
147151
"late": {self.lateCount},

0 commit comments

Comments
 (0)