Skip to content

Commit

Permalink
Add total for all stats objects
Browse files Browse the repository at this point in the history
  • Loading branch information
rklaehn committed Sep 28, 2023
1 parent ea9dae4 commit 1de2016
Showing 1 changed file with 145 additions and 1 deletion.
146 changes: 145 additions & 1 deletion src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@ pub struct Stats {
pub duration: Duration,
}

impl std::ops::Add<Stats> 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<Stats> 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 {
Expand All @@ -35,6 +52,29 @@ pub struct SizeAndStats {
pub stats: Stats,
}

impl From<Stats> for SizeAndStats {
fn from(stats: Stats) -> Self {
Self { size: 0, stats }
}
}

impl std::ops::Add<SizeAndStats> 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<SizeAndStats> 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 {
Expand All @@ -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<StreamWriterStats> 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<StreamWriterStats> 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<W> {
Expand Down Expand Up @@ -102,10 +173,33 @@ impl<W: AsyncStreamWriter> AsyncStreamWriter for TrackingStreamWriter<W> {
/// 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<StreamReaderStats> for StreamReaderStats {
type Output = StreamReaderStats;

fn add(self, rhs: StreamReaderStats) -> Self::Output {
Self {
read: self.read + rhs.read,
}
}
}

impl std::ops::AddAssign<StreamReaderStats> 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<W> {
Expand Down Expand Up @@ -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<SliceReaderStats> 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<SliceReaderStats> 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<R> {
Expand Down Expand Up @@ -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<SliceWriterStats> 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<SliceWriterStats> 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<W> {
Expand Down

0 comments on commit 1de2016

Please sign in to comment.