Skip to content

Commit b0709c7

Browse files
fix freq
Signed-off-by: Little-Wallace <[email protected]>
1 parent 28803f4 commit b0709c7

File tree

4 files changed

+24
-25
lines changed

4 files changed

+24
-25
lines changed

src/engine.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,9 @@ where
583583
return Box::pin(async move {
584584
let mut stats = r.await?;
585585
// transfer micro to sec
586-
stats.avg_write_cost = write_cost / write_count;
586+
if write_count > 0 {
587+
stats.avg_write_cost = write_cost / write_count;
588+
}
587589
stats.max_write_cost = max_write_cost;
588590
Ok(stats)
589591
});

src/log_batch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ mod tests {
673673
entries
674674
.encode_to::<Entry>(&mut encoded, &mut entries_size1)
675675
.unwrap();
676-
for idx in entries.entries_index.borrow_mut().iter_mut() {
676+
for idx in entries.entries_index.iter_mut() {
677677
idx.file_num = file_num;
678678
}
679679
let (mut s, mut entries_size2) = (encoded.as_slice(), 0);

src/util.rs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,8 @@ impl<T: Clone> Drop for Worker<T> {
324324

325325
#[derive(Clone, Debug, Copy, PartialEq, Default)]
326326
pub struct Statistic {
327-
pub wal_cost: usize,
328-
pub sync_cost: usize,
327+
pub avg_wal_cost: usize,
328+
pub avg_sync_cost: usize,
329329
pub avg_write_cost: usize,
330330
pub max_wal_cost: usize,
331331
pub max_sync_cost: usize,
@@ -342,18 +342,9 @@ fn max(left: usize, right: usize) -> usize {
342342
}
343343

344344
impl Statistic {
345-
pub fn add(&mut self, other: &Self) {
346-
self.wal_cost += other.wal_cost;
347-
self.sync_cost += other.sync_cost;
348-
self.freq += other.freq;
349-
self.max_wal_cost = max(self.max_wal_cost, other.max_wal_cost);
350-
self.max_write_cost = max(self.max_write_cost, other.max_write_cost);
351-
self.max_sync_cost = max(self.max_sync_cost, other.max_sync_cost);
352-
}
353-
354345
pub fn clear(&mut self) {
355-
self.wal_cost = 0;
356-
self.sync_cost = 0;
346+
self.avg_wal_cost = 0;
347+
self.avg_sync_cost = 0;
357348
self.avg_write_cost = 0;
358349
self.max_wal_cost = 0;
359350
self.max_sync_cost = 0;
@@ -363,18 +354,20 @@ impl Statistic {
363354

364355
#[inline]
365356
pub fn add_wal(&mut self, wal: usize) {
366-
self.wal_cost += wal;
357+
self.avg_wal_cost += wal;
367358
self.max_wal_cost = max(self.max_wal_cost, wal);
368359
}
369360

370361
#[inline]
371362
pub fn add_sync(&mut self, sync: usize) {
372-
self.sync_cost += sync;
363+
self.avg_sync_cost += sync;
373364
self.max_sync_cost = max(self.max_sync_cost, sync);
374365
}
375366

376-
#[inline]
377-
pub fn add_one(&mut self) {
378-
self.freq += 1;
367+
pub fn divide(&mut self) {
368+
if self.freq > 0 {
369+
self.avg_wal_cost /= self.freq;
370+
self.avg_sync_cost /= self.freq;
371+
}
379372
}
380373
}

src/wal.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ where
5858
return Ok(());
5959
}
6060
LogMsg::Metric(cb) => {
61-
let _ = cb.send(self.statistic.clone());
62-
self.statistic.clear();
61+
self.report(cb);
6362
continue;
6463
}
6564
};
@@ -79,8 +78,7 @@ where
7978
return Ok(());
8079
}
8180
LogMsg::Metric(cb) => {
82-
let _ = cb.send(self.statistic.clone());
83-
self.statistic.clear();
81+
self.report(cb);
8482
continue;
8583
}
8684
};
@@ -118,12 +116,18 @@ where
118116
self.statistic.add_wal(wal_cost as usize);
119117
self.statistic
120118
.add_sync((wal_cost - before_sync_cost) as usize);
121-
self.statistic.add_one();
119+
self.statistic.freq += 1;
122120
for (offset, sender) in write_ret.drain(..) {
123121
let _ = sender.send((file_num, base_offset + offset, tracker.clone()));
124122
}
125123
write_buffer.clear();
126124
}
127125
Ok(())
128126
}
127+
128+
pub fn report(&mut self, cb: Sender<Statistic>) {
129+
self.statistic.divide();
130+
let _ = cb.send(self.statistic.clone());
131+
self.statistic.clear();
132+
}
129133
}

0 commit comments

Comments
 (0)