11//! STDB module used for benchmarks based on "realistic" workloads we are focusing in improving.
22use crate :: Load ;
3- use spacetimedb:: { log, SpacetimeType , Timestamp } ;
3+ use spacetimedb:: { log, ReducerContext , SpacetimeType , Table , Timestamp } ;
44use std:: hint:: black_box;
55
66#[ derive( SpacetimeType , Debug , Clone , Copy ) ]
@@ -79,25 +79,29 @@ fn is_overlapping(entity1: &Entity, entity2: &Entity) -> bool {
7979
8080// ---------- insert bulk ----------
8181#[ spacetimedb:: reducer]
82- pub fn insert_bulk_entity ( count : u32 ) {
82+ pub fn insert_bulk_entity ( ctx : & ReducerContext , count : u32 ) {
8383 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+ . entity ( )
86+ . insert ( Entity :: new ( 0 , id as f32 , ( id + 5 ) as f32 , id * 5 ) ) ;
8587 }
8688 log:: info!( "INSERT ENTITY: {count}" ) ;
8789}
8890
8991#[ spacetimedb:: reducer]
90- pub fn insert_bulk_circle ( count : u32 ) {
92+ pub fn insert_bulk_circle ( ctx : & ReducerContext , count : u32 ) {
9193 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+ . circle ( )
96+ . insert ( Circle :: new ( id, id, id as f32 , ( id + 5 ) as f32 , ( id * 5 ) as f32 ) ) ;
9397 }
9498 log:: info!( "INSERT CIRCLE: {count}" ) ;
9599}
96100
97101#[ spacetimedb:: reducer]
98- pub fn insert_bulk_food ( count : u32 ) {
102+ pub fn insert_bulk_food ( ctx : & ReducerContext , count : u32 ) {
99103 for id in 1 ..=count {
100- Food :: insert ( Food :: new ( id) ) . unwrap ( ) ;
104+ ctx . db . food ( ) . insert ( Food :: new ( id) ) ;
101105 }
102106 log:: info!( "INSERT FOOD: {count}" ) ;
103107}
@@ -107,11 +111,11 @@ pub fn insert_bulk_food(count: u32) {
107111// SELECT * FROM Circle, Entity, Food
108112// ```
109113#[ spacetimedb:: reducer]
110- pub fn cross_join_all ( expected : u32 ) {
114+ pub fn cross_join_all ( ctx : & ReducerContext , expected : u32 ) {
111115 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 . circle ( ) . iter ( ) {
117+ for _entity in ctx . db . entity ( ) . iter ( ) {
118+ for _food in ctx . db . food ( ) . iter ( ) {
115119 count += 1 ;
116120 }
117121 }
@@ -125,16 +129,20 @@ pub fn cross_join_all(expected: u32) {
125129// SELECT * FROM Circle JOIN ENTITY USING(entity_id), Food JOIN ENTITY USING(entity_id)
126130// ```
127131#[ spacetimedb:: reducer]
128- pub fn cross_join_circle_food ( expected : u32 ) {
132+ pub fn cross_join_circle_food ( ctx : & ReducerContext , expected : u32 ) {
129133 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 . circle ( ) . iter ( ) {
135+ let Some ( circle_entity) = ctx . db . entity ( ) . id ( ) . find ( circle. entity_id ) else {
132136 continue ;
133137 } ;
134138
135- for food in Food :: iter ( ) {
139+ for food in ctx . db . food ( ) . iter ( ) {
136140 count += 1 ;
137- let food_entity = Entity :: filter_by_id ( & food. entity_id )
141+ let food_entity = ctx
142+ . db
143+ . entity ( )
144+ . id ( )
145+ . find ( food. entity_id )
138146 . unwrap_or_else ( || panic ! ( "Entity not found: {})" , food. entity_id) ) ;
139147 black_box ( is_overlapping ( & circle_entity, & food_entity) ) ;
140148 }
@@ -144,18 +152,18 @@ pub fn cross_join_circle_food(expected: u32) {
144152}
145153
146154#[ spacetimedb:: reducer]
147- pub fn init_game_circles ( initial_load : u32 ) {
155+ pub fn init_game_circles ( ctx : & ReducerContext , initial_load : u32 ) {
148156 let load = Load :: new ( initial_load) ;
149157
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 ) ;
153161}
154162
155163#[ spacetimedb:: reducer]
156- pub fn run_game_circles ( initial_load : u32 ) {
164+ pub fn run_game_circles ( ctx : & ReducerContext , initial_load : u32 ) {
157165 let load = Load :: new ( initial_load) ;
158166
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 ) ;
161169}
0 commit comments