From 1de2016699e8edbe85c9c2b5318dda85e96ad54b Mon Sep 17 00:00:00 2001 From: Ruediger Klaehn Date: Thu, 28 Sep 2023 17:02:10 +0300 Subject: [PATCH] Add total for all stats objects --- src/stats.rs | 146 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 145 insertions(+), 1 deletion(-) diff --git a/src/stats.rs b/src/stats.rs index da1cb5b..f7421af 100644 --- a/src/stats.rs +++ b/src/stats.rs @@ -26,6 +26,23 @@ pub struct Stats { pub duration: Duration, } +impl std::ops::Add for Stats { + type Output = Stats; + + fn add(self, rhs: Stats) -> Self::Output { + Self { + count: self.count.saturating_add(rhs.count), + duration: self.duration.saturating_add(rhs.duration), + } + } +} + +impl std::ops::AddAssign for Stats { + fn add_assign(&mut self, rhs: Stats) { + *self = *self + rhs; + } +} + /// Statistics about a tracked operation. #[derive(Debug, Clone, Copy, Default)] pub struct SizeAndStats { @@ -35,6 +52,29 @@ pub struct SizeAndStats { pub stats: Stats, } +impl From for SizeAndStats { + fn from(stats: Stats) -> Self { + Self { size: 0, stats } + } +} + +impl std::ops::Add for SizeAndStats { + type Output = SizeAndStats; + + fn add(self, rhs: SizeAndStats) -> Self::Output { + Self { + size: self.size.saturating_add(rhs.size), + stats: self.stats + rhs.stats, + } + } +} + +impl std::ops::AddAssign for SizeAndStats { + fn add_assign(&mut self, rhs: SizeAndStats) { + *self = *self + rhs; + } +} + /// Statistics about a stream writer. #[derive(Debug, Clone, Copy, Default)] pub struct StreamWriterStats { @@ -46,6 +86,37 @@ pub struct StreamWriterStats { pub sync: Stats, } +impl StreamWriterStats { + /// Gives the total stats for this writer. + /// + /// This adds the count and duration from all three operations, + /// and the total number of bytes written from write and wite_bytes. + /// + /// It is important to also add the sync stats, because some buffered + /// writers will do most actual io in sync. + pub fn total(&self) -> SizeAndStats { + self.write + self.write_bytes + self.sync.into() + } +} + +impl std::ops::Add for StreamWriterStats { + type Output = StreamWriterStats; + + fn add(self, rhs: StreamWriterStats) -> Self::Output { + Self { + write: self.write + rhs.write, + write_bytes: self.write_bytes + rhs.write_bytes, + sync: self.sync + rhs.sync, + } + } +} + +impl std::ops::AddAssign for StreamWriterStats { + fn add_assign(&mut self, rhs: StreamWriterStats) { + *self = *self + rhs; + } +} + /// A stream writer that tracks the time spent in write operations. #[derive(Debug, Clone)] pub struct TrackingStreamWriter { @@ -102,10 +173,33 @@ impl AsyncStreamWriter for TrackingStreamWriter { /// Statistics about a stream writer. #[derive(Debug, Clone, Copy, Default)] pub struct StreamReaderStats { - /// Statistics about the `write` operation. + /// Statistics about the `read` operation. pub read: SizeAndStats, } +impl StreamReaderStats { + /// Gives the total stats for this reader. + pub fn total(&self) -> SizeAndStats { + self.read + } +} + +impl std::ops::Add for StreamReaderStats { + type Output = StreamReaderStats; + + fn add(self, rhs: StreamReaderStats) -> Self::Output { + Self { + read: self.read + rhs.read, + } + } +} + +impl std::ops::AddAssign for StreamReaderStats { + fn add_assign(&mut self, rhs: StreamReaderStats) { + *self = *self + rhs; + } +} + /// A stream writer that tracks the time spent in write operations. #[derive(Debug, Clone)] pub struct TrackingStreamReader { @@ -146,6 +240,30 @@ pub struct SliceReaderStats { pub len: Stats, } +impl SliceReaderStats { + /// Gives the total stats for this reader. + pub fn total(&self) -> SizeAndStats { + self.read_at + self.len.into() + } +} + +impl std::ops::Add for SliceReaderStats { + type Output = SliceReaderStats; + + fn add(self, rhs: SliceReaderStats) -> Self::Output { + Self { + read_at: self.read_at + rhs.read_at, + len: self.len + rhs.len, + } + } +} + +impl std::ops::AddAssign for SliceReaderStats { + fn add_assign(&mut self, rhs: SliceReaderStats) { + *self = *self + rhs; + } +} + /// A slice reader that tracks the time spent in read operations. #[derive(Debug, Clone)] pub struct TrackingSliceReader { @@ -196,6 +314,32 @@ pub struct SliceWriterStats { pub sync: Stats, } +impl SliceWriterStats { + /// Gives the total stats for this writer. + pub fn total(&self) -> SizeAndStats { + self.write_at + self.write_bytes_at + self.set_len.into() + self.sync.into() + } +} + +impl std::ops::Add for SliceWriterStats { + type Output = SliceWriterStats; + + fn add(self, rhs: SliceWriterStats) -> Self::Output { + Self { + write_at: self.write_at + rhs.write_at, + write_bytes_at: self.write_bytes_at + rhs.write_bytes_at, + set_len: self.set_len + rhs.set_len, + sync: self.sync + rhs.sync, + } + } +} + +impl std::ops::AddAssign for SliceWriterStats { + fn add_assign(&mut self, rhs: SliceWriterStats) { + *self = *self + rhs; + } +} + /// A slice writer that tracks the time spent in write operations. #[derive(Debug, Clone)] pub struct TrackingSliceWriter {