Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Avoid division by zero.
  • Loading branch information
Roman S. Borschel committed Aug 11, 2020
commit 6c96875d2a77371fb323eaa5a5f62ac66fdb05cf
17 changes: 10 additions & 7 deletions client/informant/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,16 @@ impl<B: BlockT> InformantDisplay<B> {
self.last_update = now;
self.last_number = Some(best_number);

let avg_bytes_per_sec_inbound = (total_bytes_inbound - self.last_total_bytes_inbound) / elapsed;
let avg_bytes_per_sec_outbound = (total_bytes_outbound - self.last_total_bytes_outbound) / elapsed;

if elapsed > 0 {
self.last_total_bytes_inbound = total_bytes_inbound;
self.last_total_bytes_outbound = total_bytes_outbound;
}
let diff_bytes_inbound = total_bytes_inbound - self.last_total_bytes_inbound;
let diff_bytes_outbound = total_bytes_outbound - self.last_total_bytes_outbound;
let (avg_bytes_per_sec_inbound, avg_bytes_per_sec_outbound) =
if elapsed > 0 {
self.last_total_bytes_inbound = total_bytes_inbound;
self.last_total_bytes_outbound = total_bytes_outbound;
(diff_bytes_inbound / elapsed, diff_bytes_outbound / elapsed)
} else {
(diff_bytes_inbound, diff_bytes_outbound)
};

let (level, status, target) = match (net_status.sync_state, net_status.best_seen_block) {
(SyncState::Idle, _) => ("💤", "Idle".into(), "".into()),
Expand Down
8 changes: 4 additions & 4 deletions client/rpc/src/system/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ fn api<T: Into<Option<Status>>>(sync: T) -> System<Block> {
external_addresses: Default::default(),
connected_peers: Default::default(),
not_connected_peers: Default::default(),
average_download_per_sec: 0,
average_upload_per_sec: 0,
total_bytes_inbound: 0,
total_bytes_outbound: 0,
peerset: serde_json::Value::Null,
}).unwrap());
},
Expand Down Expand Up @@ -282,8 +282,8 @@ fn system_network_state() {
external_addresses: Default::default(),
connected_peers: Default::default(),
not_connected_peers: Default::default(),
average_download_per_sec: 0,
average_upload_per_sec: 0,
total_bytes_inbound: 0,
total_bytes_outbound: 0,
peerset: serde_json::Value::Null,
}
);
Expand Down
22 changes: 13 additions & 9 deletions client/service/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ impl MetricsService {
net_status: &NetworkStatus<T>,
) {
let now = Instant::now();
let elapsed = (now - self.last_update).as_secs();

let best_number = info.chain.best_number.saturated_into::<u64>();
let best_hash = info.chain.best_hash;
Expand All @@ -164,13 +165,16 @@ impl MetricsService {
.best_seen_block
.map(|num: NumberFor<T>| num.unique_saturated_into() as u64);

let elapsed = (now - self.last_update).as_secs();
let inbound_per_sec = (total_bytes_inbound - self.last_total_bytes_inbound) / elapsed;
let outbound_per_sec = (total_bytes_outbound - self.last_total_bytes_outbound) / elapsed;
if elapsed > 0 {
self.last_total_bytes_inbound = total_bytes_inbound;
self.last_total_bytes_outbound = total_bytes_outbound;
}
let diff_bytes_inbound = total_bytes_inbound - self.last_total_bytes_inbound;
let diff_bytes_outbound = total_bytes_outbound - self.last_total_bytes_outbound;
let (avg_bytes_per_sec_inbound, avg_bytes_per_sec_outbound) =
if elapsed > 0 {
self.last_total_bytes_inbound = total_bytes_inbound;
self.last_total_bytes_outbound = total_bytes_outbound;
(diff_bytes_inbound / elapsed, diff_bytes_outbound / elapsed)
} else {
(diff_bytes_inbound, diff_bytes_outbound)
};
self.last_update = now;

telemetry!(
Expand All @@ -182,8 +186,8 @@ impl MetricsService {
"txcount" => txpool_status.ready,
"finalized_height" => finalized_number,
"finalized_hash" => ?info.chain.finalized_hash,
"bandwidth_download" => inbound_per_sec,
"bandwidth_upload" => outbound_per_sec,
"bandwidth_download" => avg_bytes_per_sec_inbound,
"bandwidth_upload" => avg_bytes_per_sec_outbound,
"used_state_cache_size" => info.usage.as_ref()
.map(|usage| usage.memory.state_cache.as_bytes())
.unwrap_or(0),
Expand Down