1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*[toml]
[features]
# Use a spinlock internally (may be faster on some platforms)
spin = []
select = []
async = ["futures-sink", "futures-core"]
eventual-fairness = ["select", "nanorand"]
default = ["async", "select", "eventual-fairness", "profiling"]
profiling = ["thag_profiler/full_profiling"]
[dependencies]
async-std = { version = "1.13.0", features = ["attributes"] }
spin1 = { package = "spin", version = "0.9.8", features = ["mutex"] }
futures-sink = { version = "0.3", default-features = false, optional = true }
futures-core = { version = "0.3", default-features = false, optional = true }
nanorand = { version = "0.7", features = ["getrandom"], optional = true }
thag_profiler = { version = "0.1, thag-auto", features = ["full_profiling"] }
*/
/// Published example from the `flume` channel crate.
/// Must be run with --multimain (-m) option to allow multiple main methods.
///
/// Refactored and profiled to test and demonstrate profiling of non-tokio
/// async functions with `thag_profiler`.
//# Purpose: demo and test profiling of non-tokio async functions with `thag_profiler`.
//# Categories: async, crates, proc_macros, profiling, technique
use flume;
use thag_profiler::{self, enable_profiling, end, profile, profiled};
#[cfg(feature = "async")]
#[async_std::main]
#[enable_profiling(runtime)]
async fn main() {
// Check if profiling is enabled
println!(
"is_profiling_enabled() = {}",
thag_profiler::is_profiling_enabled()
);
if cfg!(feature = "profiling") {
println!("Profiling feature is enabled");
} else {
println!("Profiling feature is disabled");
}
let _ = perform().await;
}
#[profiled]
async fn perform() {
let (tx, rx) = flume::bounded(1);
// #[cfg(feature = "profiling")]
profile!(async_operation, time, mem_summary, async_fn);
let t = async_std::task::spawn(async move {
while let Ok(msg) = rx.recv_async().await {
println!("Received: {}", msg);
}
});
// #[cfg(feature = "profiling")]
end!(async_operation);
// #[cfg(feature = "profiling")]
profile!(send_async, time, mem_detail, async_fn);
tx.send_async("Hello, world!").await.unwrap();
tx.send_async("How are you today?").await.unwrap();
// #[cfg(feature = "profiling")]
end!(send_async);
drop(tx);
t.await;
}
#[cfg(not(feature = "async"))]
#[enable_profiling]
fn main() {
println!(r#"Run with flume "async" feature activated in toml block to enable this demo"#);
}