Skip to main content

performance/
performance.rs

1//! Performance check
2
3use casual_logger::{Extension, Log, Opt};
4use std::sync::mpsc;
5use std::thread;
6use std::time::Instant;
7// use sys_info::mem_info;
8
9fn main() {
10    let stopwatch = Instant::now();
11    Log::set_file_name("performance-check");
12    Log::set_file_ext(Extension::Log);
13    Log::set_retention_days(2);
14    Log::remove_old_logs();
15    Log::set_opt(Opt::Development);
16    println!("Notice          | Start!");
17
18    // Multi thread test.
19    let size = 100_000;
20    let (sender1, receiver1) = mpsc::channel();
21    thread::spawn(move || {
22        let mut count_1 = 0;
23        for i in 0..size {
24            Log::infoln(&format!("Good morning! {}", i + 1));
25            count_1 += 1;
26        }
27        if let Err(msg) = sender1.send(count_1) {
28            panic!(msg);
29        }
30    });
31
32    let (sender2, receiver2) = mpsc::channel();
33    thread::spawn(move || {
34        let mut count_2 = 0;
35        for i in 0..size {
36            Log::infoln(&format!("Good afternoon! {}", i + 1));
37            count_2 += 1;
38        }
39        if let Err(msg) = sender2.send(count_2) {
40            panic!(msg);
41        }
42    });
43
44    let (sender3, receiver3) = mpsc::channel();
45    thread::spawn(move || {
46        let mut count_3 = 0;
47        for i in 0..size {
48            Log::infoln(&format!("Good night! {}", i + 1));
49            count_3 += 1;
50        }
51        if let Err(msg) = sender3.send(count_3) {
52            panic!(msg);
53        }
54    });
55
56    let mut count_0 = 0;
57    // Single thread test.
58    let size = 300_000;
59    for i in 0..size {
60        Log::infoln(&format!("Hello, world!! {}", i + 1));
61        count_0 += 1;
62    }
63
64    // Wait for logging to complete or to timeout.
65    Log::flush();
66
67    // Block.
68    let count_1 = if let Ok(count_1) = receiver1.recv() {
69        count_1
70    } else {
71        0
72    };
73    let count_2 = if let Ok(count_2) = receiver2.recv() {
74        count_2
75    } else {
76        0
77    };
78    let count_3 = if let Ok(count_3) = receiver3.recv() {
79        count_3
80    } else {
81        0
82    };
83
84    /*
85    // Fatal is Panic! Can be used as the first argument of.
86    panic!(Log::fatal(&format!(
87        "Panic successful. Performance: {} ms.",
88        stopwatch.elapsed().as_millis()
89    )));
90    */
91
92    println!(
93        "Performance     |{}|{}|{}|{}| records, {} ms.",
94        count_0,
95        count_1,
96        count_2,
97        count_3,
98        stopwatch.elapsed().as_millis(),
99        /*
100        if let Ok(mem) = mem_info() {
101            format!(
102                "Mem=|Total {}|Avail {}|Buffers {}|Cached {}|Free {}|SwapFree {}|SwapTotal {}|",
103                mem.total,
104                mem.avail,
105                mem.buffers,
106                mem.cached,
107                mem.free,
108                mem.swap_free,
109                mem.swap_total
110            )
111        } else {
112            "".to_string()
113        }
114        */
115    )
116}