Skip to content

Commit 7f74c7b

Browse files
committed
throughput function
1 parent 248d496 commit 7f74c7b

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed

src/log/logging_stream.rs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ impl<S: SessionStream> LoggingStream<S> {
7070
enable_stats: true
7171
}
7272
}
73+
74+
/// Returns throughput in bps.
75+
pub fn throughput(&self) -> f64 {
76+
let total_duration_secs = self.total_duration.as_secs_f64();
77+
if total_duration_secs > 0.0 {
78+
(self.total_read as f64) / total_duration_secs
79+
} else {
80+
0.0
81+
}
82+
}
7383
}
7484

7585
impl<S: SessionStream> AsyncRead for LoggingStream<S> {
@@ -110,38 +120,24 @@ impl<S: SessionStream> AsyncWrite for LoggingStream<S> {
110120
cx: &mut std::task::Context<'_>,
111121
buf: &[u8],
112122
) -> Poll<std::io::Result<usize>> {
113-
let log_message = format!("{}: WRITING {}", self.tag, buf.len());
114-
115-
let projected = self.project();
116-
projected.events.emit(Event {
117-
id: 0,
118-
typ: EventType::Info(log_message),
119-
});
120-
121-
projected.inner.poll_write(cx, buf)
123+
self.project().inner.poll_write(cx, buf)
122124
}
123125

124126
fn poll_flush(
125127
self: Pin<&mut Self>,
126128
cx: &mut std::task::Context<'_>,
127129
) -> Poll<std::io::Result<()>> {
128-
let projected = self.project();
130+
let throughput = self.throughput();
129131

132+
let projected = self.project();
130133
if let Some(first_read_timestamp) = projected.first_read_timestamp.take() {
131134
let duration = projected.last_read_timestamp.duration_since(first_read_timestamp);
132135

133136
*projected.total_read = projected.total_read.saturating_add(*projected.span_read);
134137
*projected.span_read = 0;
135138
*projected.total_duration = projected.total_duration.saturating_add(duration);
136139

137-
let total_duration_secs = projected.total_duration.as_secs_f64();
138-
let throughput = if total_duration_secs > 0.0 {
139-
(*projected.total_read as f64) / total_duration_secs
140-
} else {
141-
0.0
142-
};
143-
144-
let log_message = format!("{}: FLUSH: read={}, duration={}, {} kbps", projected.tag, *projected.total_read, total_duration_secs, throughput * 8e-3);
140+
let log_message = format!("{}: FLUSH: {} kbps", projected.tag, throughput * 8e-3);
145141

146142
projected.events.emit(Event {
147143
id: 0,

0 commit comments

Comments
 (0)