Skip to content

Commit

Permalink
Update message latency calc
Browse files Browse the repository at this point in the history
- since clock drift is an averaged value, latency can go a few ms below zero
  • Loading branch information
dvsku committed Jun 22, 2024
1 parent b9f25e3 commit c82b34e
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions include/libnetwrk/net/messages/message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ namespace libnetwrk {
using message_t = message<Desc>;
using command_t = typename Desc::command_t;

using milliseconds_t = std::chrono::milliseconds;

public:
message_head_t head;
dynamic_buffer data;
Expand All @@ -42,17 +40,18 @@ namespace libnetwrk {
}

/*
Get milliseconds it took from sending the message to receiving the message.
Get end-to-end latency.
*/
milliseconds_t latency() {
std::chrono::milliseconds latency() {
return latency(head.recv_timestamp);
}

/*
Get milliseconds it took from receiving the message to a relative timestamp.
Get send timestamp to relative timestamp latency.
Relative timestamp is assumed to be in ms.
*/
milliseconds_t latency(uint64_t timestamp) {
return milliseconds_t(timestamp - head.send_timestamp);
std::chrono::milliseconds latency(uint64_t timestamp) {
return std::chrono::milliseconds(std::max((int64_t)0, (int64_t)(timestamp - head.send_timestamp)));
}

template <typename T>
Expand Down

0 comments on commit c82b34e

Please sign in to comment.