|
| 1 | +//! STDB module used for benchmarks based on "realistic" workloads we are focusing in improving. |
| 2 | + |
| 3 | +import { type Load, newLoad, blackBox } from './load'; |
| 4 | +import { spacetimedb, type Entity, type Circle, type Food } from './schema'; |
| 5 | +import { Timestamp } from 'spacetimedb'; |
| 6 | +import { t } from 'spacetimedb/server'; |
| 7 | + |
| 8 | +function newEntity(id: number, x: number, y: number, mass: number): Entity { |
| 9 | + return { |
| 10 | + id, |
| 11 | + position: { x, y }, |
| 12 | + mass, |
| 13 | + }; |
| 14 | +} |
| 15 | + |
| 16 | +function newCircle( |
| 17 | + entity_id: number, |
| 18 | + player_id: number, |
| 19 | + x: number, |
| 20 | + y: number, |
| 21 | + magnitude: number, |
| 22 | + last_split_time: Timestamp |
| 23 | +): Circle { |
| 24 | + return { |
| 25 | + entity_id, |
| 26 | + player_id, |
| 27 | + direction: { x, y }, |
| 28 | + magnitude, |
| 29 | + last_split_time, |
| 30 | + }; |
| 31 | +} |
| 32 | + |
| 33 | +function newFood(entity_id: number): Food { |
| 34 | + return { |
| 35 | + entity_id, |
| 36 | + }; |
| 37 | +} |
| 38 | + |
| 39 | +function massToRadius(mass: number): number { |
| 40 | + return Math.sqrt(mass); |
| 41 | +} |
| 42 | + |
| 43 | +function isOverlapping(entity1: Entity, entity2: Entity): boolean { |
| 44 | + const entity1Radius = massToRadius(entity1.mass); |
| 45 | + const entity2Radius = massToRadius(entity2.mass); |
| 46 | + const distance = Math.sqrt( |
| 47 | + (entity1.position.x - entity2.position.x) ** 2 + |
| 48 | + (entity1.position.y - entity2.position.y) ** 2 |
| 49 | + ); |
| 50 | + return distance < Math.max(entity1Radius, entity2Radius); |
| 51 | +} |
| 52 | + |
| 53 | +// ---------- insert bulk ---------- |
| 54 | + |
| 55 | +const insertBulkEntity = spacetimedb.reducer('insert_bulk_entity', { count: t.u32() }, (ctx, { count }) => { |
| 56 | + for (let id = 0; id < count; id++) { |
| 57 | + ctx.db.entity.insert(newEntity(0, id, id + 5, id * 5)); |
| 58 | + } |
| 59 | + console.info(`INSERT ENTITY: ${count}`); |
| 60 | +}); |
| 61 | + |
| 62 | +const insertBulkCircle = spacetimedb.reducer('insert_bulk_circle', { count: t.u32() }, (ctx, { count }) => { |
| 63 | + for (let id = 0; id < count; id++) { |
| 64 | + ctx.db.circle.insert(newCircle(id, id, id, id + 5, id * 5, ctx.timestamp)); |
| 65 | + } |
| 66 | + console.info(`INSERT CIRCLE: ${count}`); |
| 67 | +}); |
| 68 | + |
| 69 | +const insertBulkFood = spacetimedb.reducer('insert_bulk_food', { count: t.u32() }, (ctx, { count }) => { |
| 70 | + for (let id = 1; id <= count; id++) { |
| 71 | + ctx.db.food.insert(newFood(id)); |
| 72 | + } |
| 73 | + console.info(`INSERT FOOD: ${count}`); |
| 74 | +}); |
| 75 | + |
| 76 | +// Simulate |
| 77 | +// ``` |
| 78 | +// SELECT * FROM Circle, Entity, Food |
| 79 | +// ``` |
| 80 | +const crossJoinAll = spacetimedb.reducer('cross_join_all', { expected: t.u32() }, (ctx, { expected }) => { |
| 81 | + let count: number = 0; |
| 82 | + |
| 83 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 84 | + for (const circle of ctx.db.circle.iter()) { |
| 85 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 86 | + for (const entity of ctx.db.entity.iter()) { |
| 87 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 88 | + for (const food of ctx.db.food.iter()) { |
| 89 | + count += 1; |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + console.info(`CROSS JOIN ALL: ${expected}, processed: ${count}`); |
| 95 | +}); |
| 96 | + |
| 97 | +// Simulate |
| 98 | +// ``` |
| 99 | +// SELECT * FROM Circle JOIN ENTITY USING(entity_id), Food JOIN ENTITY USING(entity_id) |
| 100 | +// ``` |
| 101 | +const crossJoinCircleFood = spacetimedb.reducer( |
| 102 | + 'cross_join_circle_food', |
| 103 | + { expected: t.u32() }, |
| 104 | + (ctx, { expected }) => { |
| 105 | + let count: number = 0; |
| 106 | + |
| 107 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 108 | + for (const circle of ctx.db.circle.iter()) { |
| 109 | + const entityId = ctx.db.entity.id; |
| 110 | + const circleEntity = entityId.find(circle.entity_id); |
| 111 | + if (circleEntity == null) { |
| 112 | + continue; |
| 113 | + } |
| 114 | + |
| 115 | + for (const food of ctx.db.food.iter()) { |
| 116 | + count += 1; |
| 117 | + |
| 118 | + const foodEntity = entityId.find(food.entity_id); |
| 119 | + if (foodEntity == null) { |
| 120 | + let string = JSON.stringify(circleEntity); |
| 121 | + throw new Error(`Entity not found: ${food.entity_id}`); |
| 122 | + } |
| 123 | + |
| 124 | + blackBox(isOverlapping(circleEntity, foodEntity)); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + console.info(`CROSS JOIN CIRCLE FOOD: ${expected}, processed: ${count}`); |
| 129 | + } |
| 130 | +); |
| 131 | + |
| 132 | +spacetimedb.reducer( |
| 133 | + 'init_game_circles', |
| 134 | + { initial_load: t.u32() }, |
| 135 | + (ctx, { initial_load }) => { |
| 136 | + const load = newLoad(initial_load); |
| 137 | + |
| 138 | + insertBulkFood(ctx, { count: load.initialLoad }); |
| 139 | + insertBulkEntity(ctx, { count: load.initialLoad }); |
| 140 | + insertBulkCircle(ctx, { count: load.smallTable }); |
| 141 | + } |
| 142 | +); |
| 143 | + |
| 144 | +spacetimedb.reducer( |
| 145 | + 'run_game_circles', |
| 146 | + { initial_load: t.u32() }, |
| 147 | + (ctx, { initial_load }) => { |
| 148 | + const load = newLoad(initial_load); |
| 149 | + |
| 150 | + crossJoinCircleFood(ctx, { expected: initial_load * load.smallTable }); |
| 151 | + crossJoinAll(ctx, { |
| 152 | + expected: initial_load * initial_load * load.smallTable, |
| 153 | + }); |
| 154 | + } |
| 155 | +); |
0 commit comments