Skip to content

Commit

Permalink
Add keynote-benchmarks (#2252)
Browse files Browse the repository at this point in the history
  • Loading branch information
Centril authored Feb 11, 2025
1 parent 2f30dc1 commit fe68fe6
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ members = [
"crates/update",
"crates/vm",
"modules/benchmarks",
"modules/keynote-benchmarks",
"modules/perf-test",
"modules/module-test",
"modules/quickstart-chat",
Expand Down
17 changes: 17 additions & 0 deletions modules/keynote-benchmarks/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Cargo
# will have compiled files and executables
debug/
target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

# Spacetime ignore
/.spacetime
11 changes: 11 additions & 0 deletions modules/keynote-benchmarks/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "keynote-benchmarks"
version = "0.1.0"
edition.workspace = true

[lib]
crate-type = ["cdylib"]

[dependencies]
spacetimedb = { path = "../../crates/bindings" }
log = "0.4"
75 changes: 75 additions & 0 deletions modules/keynote-benchmarks/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use core::ops::AddAssign;
use spacetimedb::{log_stopwatch::LogStopwatch, rand::Rng, reducer, table, DbContext, ReducerContext, Table};

#[derive(Clone, Copy, Debug)]
#[table(name = position, public)]
#[repr(C)]
pub struct Position {
#[primary_key]
#[index(direct)]
id: u32,
x: f32,
y: f32,
z: f32,
}

#[derive(Clone, Copy, Debug)]
#[table(name = velocity, public)]
#[repr(C)]
pub struct Velocity {
#[primary_key]
#[index(direct)]
id: u32,
dx: f32,
dy: f32,
dz: f32,
}

impl AddAssign<Velocity> for Position {
fn add_assign(&mut self, vel: Velocity) {
self.x += vel.dx;
self.y += vel.dy;
self.z += vel.dz;
}
}

#[reducer(init)]
fn init(ctx: &ReducerContext) {
let _stopwatch = LogStopwatch::new("init");

// Insert 10^6 randomized positions and velocities,
// but with incrementing and corresponding ids.
let db = ctx.db();
let mut rng = ctx.rng();
for id in 0..1_000_000 {
let (x, y, z) = rng.gen();
let (dx, dy, dz) = rng.gen();
db.position().insert(Position { id, x, y, z });
db.velocity().insert(Velocity { id, dx, dy, dz });
}
}

#[reducer]
fn update_positions_by_collect(ctx: &ReducerContext) {
let _stopwatch = LogStopwatch::new("update_positions_by_collect");

let mut pos_vec = ctx.db.position().iter().collect::<Vec<_>>();
let mut vel_vec = ctx.db.velocity().iter().collect::<Vec<_>>();

pos_vec.sort_unstable_by_key(|pos| pos.id);
vel_vec.sort_unstable_by_key(|vel| vel.id);

for (pos, vel) in pos_vec.iter_mut().zip(&vel_vec) {
*pos += *vel;
}

for pos in pos_vec {
ctx.db.position().id().update(pos);
}
}

#[reducer]
fn roundtrip(ctx: &ReducerContext) {
let _stopwatch = LogStopwatch::new("index_roundtrip");
ctx.db().velocity().id().find(333_333);
}

0 comments on commit fe68fe6

Please sign in to comment.