1
1
//! STDB module used for benchmarks based on "realistic" workloads we are focusing in improving.
2
2
use crate :: Load ;
3
- use spacetimedb:: { log, SpacetimeType , Timestamp } ;
3
+ use spacetimedb:: { log, ReducerContext , SpacetimeType , Table , Timestamp } ;
4
4
use std:: hint:: black_box;
5
5
6
6
#[ derive( SpacetimeType , Debug , Clone , Copy ) ]
@@ -79,25 +79,29 @@ fn is_overlapping(entity1: &Entity, entity2: &Entity) -> bool {
79
79
80
80
// ---------- insert bulk ----------
81
81
#[ spacetimedb:: reducer]
82
- pub fn insert_bulk_entity ( count : u32 ) {
82
+ pub fn insert_bulk_entity ( ctx : & ReducerContext , count : u32 ) {
83
83
for id in 0 ..count {
84
- Entity :: insert ( Entity :: new ( 0 , id as f32 , ( id + 5 ) as f32 , id * 5 ) ) . unwrap ( ) ;
84
+ ctx. db
85
+ . entities ( )
86
+ . insert ( Entity :: new ( 0 , id as f32 , ( id + 5 ) as f32 , id * 5 ) ) ;
85
87
}
86
88
log:: info!( "INSERT ENTITY: {count}" ) ;
87
89
}
88
90
89
91
#[ spacetimedb:: reducer]
90
- pub fn insert_bulk_circle ( count : u32 ) {
92
+ pub fn insert_bulk_circle ( ctx : & ReducerContext , count : u32 ) {
91
93
for id in 0 ..count {
92
- Circle :: insert ( Circle :: new ( id, id, id as f32 , ( id + 5 ) as f32 , ( id * 5 ) as f32 ) ) . unwrap ( ) ;
94
+ ctx. db
95
+ . circles ( )
96
+ . insert ( Circle :: new ( id, id, id as f32 , ( id + 5 ) as f32 , ( id * 5 ) as f32 ) ) ;
93
97
}
94
98
log:: info!( "INSERT CIRCLE: {count}" ) ;
95
99
}
96
100
97
101
#[ spacetimedb:: reducer]
98
- pub fn insert_bulk_food ( count : u32 ) {
102
+ pub fn insert_bulk_food ( ctx : & ReducerContext , count : u32 ) {
99
103
for id in 1 ..=count {
100
- Food :: insert ( Food :: new ( id) ) . unwrap ( ) ;
104
+ ctx . db . food ( ) . insert ( Food :: new ( id) ) ;
101
105
}
102
106
log:: info!( "INSERT FOOD: {count}" ) ;
103
107
}
@@ -107,11 +111,11 @@ pub fn insert_bulk_food(count: u32) {
107
111
// SELECT * FROM Circle, Entity, Food
108
112
// ```
109
113
#[ spacetimedb:: reducer]
110
- pub fn cross_join_all ( expected : u32 ) {
114
+ pub fn cross_join_all ( ctx : & ReducerContext , expected : u32 ) {
111
115
let mut count = 0 ;
112
- for _circle in Circle :: iter ( ) {
113
- for _entity in Entity :: iter ( ) {
114
- for _food in Food :: iter ( ) {
116
+ for _circle in ctx . db . circles ( ) . iter ( ) {
117
+ for _entity in ctx . db . entities ( ) . iter ( ) {
118
+ for _food in ctx . db . food ( ) . iter ( ) {
115
119
count += 1 ;
116
120
}
117
121
}
@@ -125,16 +129,20 @@ pub fn cross_join_all(expected: u32) {
125
129
// SELECT * FROM Circle JOIN ENTITY USING(entity_id), Food JOIN ENTITY USING(entity_id)
126
130
// ```
127
131
#[ spacetimedb:: reducer]
128
- pub fn cross_join_circle_food ( expected : u32 ) {
132
+ pub fn cross_join_circle_food ( ctx : & ReducerContext , expected : u32 ) {
129
133
let mut count = 0 ;
130
- for circle in Circle :: iter ( ) {
131
- let Some ( circle_entity) = Entity :: filter_by_id ( & circle. entity_id ) else {
134
+ for circle in ctx . db . circles ( ) . iter ( ) {
135
+ let Some ( circle_entity) = ctx . db . entities ( ) . id ( ) . find ( circle. entity_id ) else {
132
136
continue ;
133
137
} ;
134
138
135
- for food in Food :: iter ( ) {
139
+ for food in ctx . db . food ( ) . iter ( ) {
136
140
count += 1 ;
137
- let food_entity = Entity :: filter_by_id ( & food. entity_id )
141
+ let food_entity = ctx
142
+ . db
143
+ . entities ( )
144
+ . id ( )
145
+ . find ( food. entity_id )
138
146
. unwrap_or_else ( || panic ! ( "Entity not found: {})" , food. entity_id) ) ;
139
147
black_box ( is_overlapping ( & circle_entity, & food_entity) ) ;
140
148
}
@@ -144,18 +152,18 @@ pub fn cross_join_circle_food(expected: u32) {
144
152
}
145
153
146
154
#[ spacetimedb:: reducer]
147
- pub fn init_game_circles ( initial_load : u32 ) {
155
+ pub fn init_game_circles ( ctx : & ReducerContext , initial_load : u32 ) {
148
156
let load = Load :: new ( initial_load) ;
149
157
150
- insert_bulk_food ( load. initial_load ) ;
151
- insert_bulk_entity ( load. initial_load ) ;
152
- insert_bulk_circle ( load. small_table ) ;
158
+ insert_bulk_food ( ctx , load. initial_load ) ;
159
+ insert_bulk_entity ( ctx , load. initial_load ) ;
160
+ insert_bulk_circle ( ctx , load. small_table ) ;
153
161
}
154
162
155
163
#[ spacetimedb:: reducer]
156
- pub fn run_game_circles ( initial_load : u32 ) {
164
+ pub fn run_game_circles ( ctx : & ReducerContext , initial_load : u32 ) {
157
165
let load = Load :: new ( initial_load) ;
158
166
159
- cross_join_circle_food ( initial_load * load. small_table ) ;
160
- cross_join_all ( initial_load * initial_load * load. small_table ) ;
167
+ cross_join_circle_food ( ctx , initial_load * load. small_table ) ;
168
+ cross_join_all ( ctx , initial_load * initial_load * load. small_table ) ;
161
169
}
0 commit comments