forked from tokio-rs/tokio-metrics
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
task: also instrument streams (tokio-rs#31)
- Loading branch information
Showing
3 changed files
with
135 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use std::time::Duration; | ||
|
||
use futures::{stream::FuturesUnordered, StreamExt}; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { | ||
let metrics_monitor = tokio_metrics::TaskMonitor::new(); | ||
|
||
// print task metrics every 500ms | ||
{ | ||
let metrics_monitor = metrics_monitor.clone(); | ||
tokio::spawn(async move { | ||
for deltas in metrics_monitor.intervals() { | ||
// pretty-print the metric deltas | ||
println!("{:?}", deltas); | ||
// wait 500ms | ||
tokio::time::sleep(Duration::from_millis(500)).await; | ||
} | ||
}) | ||
}; | ||
|
||
// instrument a stream and await it | ||
let mut stream = | ||
metrics_monitor.instrument((0..3).map(|_| do_work()).collect::<FuturesUnordered<_>>()); | ||
while stream.next().await.is_some() {} | ||
|
||
println!("{:?}", metrics_monitor.cumulative()); | ||
|
||
Ok(()) | ||
} | ||
|
||
async fn do_work() { | ||
for _ in 0..25 { | ||
tokio::task::yield_now().await; | ||
tokio::time::sleep(Duration::from_millis(100)).await; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters