summaryrefslogtreecommitdiffstats
path: root/vendor/countme/examples/bench.rs
blob: 1f9cc967f82898d70eafd35fe8f971a6f24ac049 (plain)
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
use std::thread;

use countme::{get, Count};

#[derive(Default)]
struct Foo {
    _c: Count<Self>,
}

#[derive(Default)]
struct Bar {
    _c: Count<Self>,
    _x: i32,
}

mod deeply {
    pub(crate) mod nested {
        pub(crate) mod module {
            use countme::Count;

            #[derive(Default)]
            pub(crate) struct Quux {
                _c: Count<Self>,
            }
        }
    }
}

fn main() {
    countme::enable(true);
    let t = std::time::Instant::now();
    let n = 5;
    let m = 1_000_000;

    let mut threads = Vec::new();
    for _ in 0..n {
        threads.push(thread::spawn(move || {
            for _ in 0..m {
                Foo::default();
            }
        }));
        threads.push(thread::spawn(move || {
            let mut xs = Vec::with_capacity(m);
            for _ in 0..m {
                xs.push(Bar::default())
            }
        }));
        threads.push(thread::spawn(move || {
            for _ in 0..m {
                deeply::nested::module::Quux::default();
            }
        }));
    }
    for t in threads {
        t.join().unwrap();
    }

    let foo = get::<Foo>();
    assert_eq!(foo.total, m * n);
    assert!(foo.max_live >= 1);
    assert_eq!(foo.live, 0);

    let bar = get::<Bar>();
    assert_eq!(bar.total, m * n);
    assert!(bar.max_live >= m);
    assert_eq!(bar.live, 0);

    println!("{:?}", t.elapsed());
}