1use std::hint::black_box;
2use std::time::{Duration, Instant};
3
4const MIN_BENCH_TIME: u64 = 2000;
5const MESSAGES: usize = 100_000;
6
7macro_rules! run {
8 ($name: expr, $code: expr) => {
9 let mut sum_elapsed = Duration::default();
10 let mut count = 0u32;
11 loop {
12 let now = Instant::now();
13 for _ in 1..=MESSAGES {
14 black_box($code)
15 }
16 let elapsed = now.elapsed();
17 ftlog::logger().flush();
18
19 sum_elapsed += elapsed;
20 count += 1;
21 if sum_elapsed >= Duration::from_millis(MIN_BENCH_TIME) && count > 10 {
22 break;
23 }
24 }
25 println!(
26 "{},{}ns,{}/s",
27 $name,
28 (sum_elapsed / count / MESSAGES as u32).as_nanos(),
29 ((count as f64 * MESSAGES as f64) / sum_elapsed.as_secs_f64()).round()
30 );
31 };
32}
33fn main() {
34 let _guard = ftlog::Builder::new()
35 .root(std::io::sink())
36 .bounded(100_000, false)
37 .build()
39 .unwrap()
40 .init()
41 .unwrap();
42 run!("static string", {
43 ftlog::info!("ftlog message");
44 });
45 run!("with i32", {
46 ftlog::info!("ftlog: {}", 0i32);
47 });
48 run!("limit with i32", {
49 ftlog::info!(limit=3i64; "ftlog: {}", 0i32);
50 });
51}