Skip to content

Commit 571b02e

Browse files
authored
Calculate averages correctly and preserve totals like before (#429)
* Reset totals after avg calculation * like it used to be
1 parent 159eb89 commit 571b02e

File tree

3 files changed

+54
-13
lines changed

3 files changed

+54
-13
lines changed

Cargo.lock

+16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ rustls = { version = "0.21", features = ["dangerous_configuration"] }
4545
trust-dns-resolver = "0.22.0"
4646
tokio-test = "0.4.2"
4747
serde_json = "1"
48+
itertools = "0.10"
4849

4950
[target.'cfg(not(target_env = "msvc"))'.dependencies]
5051
jemallocator = "0.5.0"

src/stats/address.rs

+37-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use log::warn;
21
use std::sync::atomic::*;
32
use std::sync::Arc;
43

@@ -13,6 +12,16 @@ pub struct AddressStats {
1312
pub total_query_time: Arc<AtomicU64>,
1413
pub total_wait_time: Arc<AtomicU64>,
1514
pub total_errors: Arc<AtomicU64>,
15+
16+
pub old_total_xact_count: Arc<AtomicU64>,
17+
pub old_total_query_count: Arc<AtomicU64>,
18+
pub old_total_received: Arc<AtomicU64>,
19+
pub old_total_sent: Arc<AtomicU64>,
20+
pub old_total_xact_time: Arc<AtomicU64>,
21+
pub old_total_query_time: Arc<AtomicU64>,
22+
pub old_total_wait_time: Arc<AtomicU64>,
23+
pub old_total_errors: Arc<AtomicU64>,
24+
1625
pub avg_query_count: Arc<AtomicU64>,
1726
pub avg_query_time: Arc<AtomicU64>,
1827
pub avg_recv: Arc<AtomicU64>,
@@ -104,16 +113,16 @@ impl AddressStats {
104113
}
105114

106115
pub fn update_averages(&self) {
107-
let (totals, averages) = self.fields_iterators();
108-
for data in totals.iter().zip(averages.iter()) {
109-
let (total, average) = data;
110-
if let Err(err) = average.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |avg| {
111-
let total = total.load(Ordering::Relaxed);
112-
let avg = (total - avg) / (crate::stats::STAT_PERIOD / 1_000); // Avg / second
113-
Some(avg)
114-
}) {
115-
warn!("Could not update averages for addresses stats, {:?}", err);
116-
}
116+
let (totals, averages, old_totals) = self.fields_iterators();
117+
for data in itertools::izip!(totals, averages, old_totals) {
118+
let (total, average, old_total) = data;
119+
let total = total.load(Ordering::Relaxed);
120+
let old = old_total.load(Ordering::Relaxed);
121+
average.store(
122+
(total - old) / (crate::stats::STAT_PERIOD / 1_000),
123+
Ordering::Relaxed,
124+
); // Avg / second
125+
old_total.store(total, Ordering::Relaxed);
117126
}
118127
}
119128

@@ -123,27 +132,42 @@ impl AddressStats {
123132
}
124133
}
125134

126-
fn fields_iterators(&self) -> (Vec<Arc<AtomicU64>>, Vec<Arc<AtomicU64>>) {
135+
fn fields_iterators(
136+
&self,
137+
) -> (
138+
Vec<Arc<AtomicU64>>,
139+
Vec<Arc<AtomicU64>>,
140+
Vec<Arc<AtomicU64>>,
141+
) {
127142
let mut totals: Vec<Arc<AtomicU64>> = Vec::new();
128143
let mut averages: Vec<Arc<AtomicU64>> = Vec::new();
144+
let mut old_totals: Vec<Arc<AtomicU64>> = Vec::new();
129145

130146
totals.push(self.total_xact_count.clone());
147+
old_totals.push(self.old_total_xact_count.clone());
131148
averages.push(self.avg_xact_count.clone());
132149
totals.push(self.total_query_count.clone());
150+
old_totals.push(self.old_total_query_count.clone());
133151
averages.push(self.avg_query_count.clone());
134152
totals.push(self.total_received.clone());
153+
old_totals.push(self.old_total_received.clone());
135154
averages.push(self.avg_recv.clone());
136155
totals.push(self.total_sent.clone());
156+
old_totals.push(self.old_total_sent.clone());
137157
averages.push(self.avg_sent.clone());
138158
totals.push(self.total_xact_time.clone());
159+
old_totals.push(self.old_total_xact_time.clone());
139160
averages.push(self.avg_xact_time.clone());
140161
totals.push(self.total_query_time.clone());
162+
old_totals.push(self.old_total_query_time.clone());
141163
averages.push(self.avg_query_time.clone());
142164
totals.push(self.total_wait_time.clone());
165+
old_totals.push(self.old_total_wait_time.clone());
143166
averages.push(self.avg_wait_time.clone());
144167
totals.push(self.total_errors.clone());
168+
old_totals.push(self.old_total_errors.clone());
145169
averages.push(self.avg_errors.clone());
146170

147-
(totals, averages)
171+
(totals, averages, old_totals)
148172
}
149173
}

0 commit comments

Comments
 (0)