summaryrefslogtreecommitdiff
path: root/ecs/benches/query.rs
blob: 0f18f5c02cebd253ab057d9f3e97184fdc3c133b (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
use std::hint::black_box;
use std::path::PathBuf;

use criterion::{criterion_group, criterion_main, Criterion};
use ecs::{Component, World};

#[derive(Component)]
struct Foo
{
    _text: String,
}

#[derive(Component)]
struct Bar
{
    _path: PathBuf,
    _num: u64,
}

#[derive(Component)]
struct Position
{
    _x: f32,
    _y: f32,
    _z: f32,
}

#[derive(Component)]
struct PosA
{
    _x: f32,
    _y: f32,
    _z: f32,
}

#[derive(Component)]
struct PosB
{
    _x: f32,
    _y: f32,
    _z: f32,
}

#[derive(Component)]
struct PosC
{
    _x: f32,
    _y: f32,
    _z: f32,
}

#[derive(Component)]
struct MoreText
{
    _more_text: String,
}

#[derive(Component)]
struct EvenMoreText
{
    _even_more_text: String,
}

fn spawn_1000_entities(world: &mut World)
{
    for _ in 0..300 {
        world.create_entity((
            Bar {
                _path: "/dev/zero".into(),
                _num: 65789,
            },
            Position { _x: 13.98, _y: 27.0, _z: 0.2 },
            Foo { _text: "Hello there".to_string() },
            PosA {
                _x: 1183.98,
                _y: 272628.0,
                _z: 3306.2,
            },
            PosB {
                _x: 171183.98,
                _y: 28.0,
                _z: 336.2901,
            },
            PosC { _x: 8273.98, _y: 28.0, _z: 336.2901 },
            MoreText {
                _more_text: "Lorem ipsum".to_string(),
            },
            EvenMoreText {
                _even_more_text: "Wow so much text".to_string(),
            },
        ));
    }

    for _ in 0..700 {
        world.create_entity((
            Bar {
                _path: "/dev/null".into(),
                _num: 65789,
            },
            Position { _x: 88.11, _y: 9.0, _z: 36.11 },
            Foo { _text: "Hey".to_string() },
            PosA {
                _x: 118310.98,
                _y: 272628.0,
                _z: 3306.2,
            },
            PosB { _x: 11323.98, _y: 28.0, _z: 336.2901 },
            PosC {
                _x: 8273.98,
                _y: 21818.0,
                _z: 336.2901,
            },
            MoreText {
                _more_text: "Lorem ipsum".to_string(),
            },
            EvenMoreText {
                _even_more_text: "Wow much text".to_string(),
            },
        ));
    }
}

fn benchbark(criterion: &mut Criterion)
{
    criterion.bench_function("Iterate 1000 entities", |bencher| {
        let mut world = World::new();

        spawn_1000_entities(&mut world);

        let query = world.query::<(Bar, Position, Foo), ()>();

        bencher.iter(|| {
            for comps in query.iter() {
                black_box(comps);
            }
        })
    });
}

criterion_group!(benches, benchbark);
criterion_main!(benches);