|
| 1 | +#![allow(clippy::disallowed_macros)] |
| 2 | + |
| 3 | +use anyhow::{bail, ensure, Context, Result}; |
| 4 | +use serde_json::Value; |
| 5 | +use spacetimedb_guard::{ensure_binaries_built, SpacetimeDbGuard}; |
| 6 | +use std::{ |
| 7 | + fs, |
| 8 | + path::{Path, PathBuf}, |
| 9 | + process::Command, |
| 10 | +}; |
| 11 | + |
| 12 | +const DATABASE_NAME: &str = "test-1"; |
| 13 | +const KEYNOTE_DIR: &str = "templates/keynote-2"; |
| 14 | +const KEYNOTE_BINDINGS_DIR: &str = "templates/keynote-2/module_bindings"; |
| 15 | +const BENCH_SECONDS: &str = "60"; |
| 16 | +const BENCH_CONCURRENCY: &str = "64"; |
| 17 | +const MAX_INFLIGHT_PER_WORKER: &str = "96"; |
| 18 | +const SEED_ACCOUNTS: &str = "100000"; |
| 19 | +const SEED_INITIAL_BALANCE: &str = "1000000000000"; |
| 20 | + |
| 21 | +struct BenchmarkModule { |
| 22 | + label: &'static str, |
| 23 | + module_dir: &'static str, |
| 24 | + min_tps: f64, |
| 25 | +} |
| 26 | + |
| 27 | +const BENCHMARK_MODULES: &[BenchmarkModule] = &[ |
| 28 | + BenchmarkModule { |
| 29 | + label: "TypeScript", |
| 30 | + module_dir: "templates/keynote-2/spacetimedb", |
| 31 | + min_tps: 275_000.0, |
| 32 | + }, |
| 33 | + BenchmarkModule { |
| 34 | + label: "Rust", |
| 35 | + module_dir: "templates/keynote-2/rust_module", |
| 36 | + min_tps: 300_000.0, |
| 37 | + }, |
| 38 | +]; |
| 39 | + |
| 40 | +pub fn run() -> Result<()> { |
| 41 | + let cli_path = ensure_binaries_built(); |
| 42 | + let server = SpacetimeDbGuard::spawn_in_temp_data_dir(); |
| 43 | + let cli_config_dir = tempfile::tempdir().context("failed to create temporary CLI config directory")?; |
| 44 | + let cli_config_path = cli_config_dir.path().join("config.toml"); |
| 45 | + |
| 46 | + for module in BENCHMARK_MODULES { |
| 47 | + run_module_benchmark(module, &cli_path, &cli_config_path, &server.host_url)?; |
| 48 | + } |
| 49 | + |
| 50 | + Ok(()) |
| 51 | +} |
| 52 | + |
| 53 | +fn run_module_benchmark(module: &BenchmarkModule, cli_path: &Path, config_path: &Path, server_url: &str) -> Result<()> { |
| 54 | + eprintln!( |
| 55 | + "Running keynote benchmark against {} module ({})...", |
| 56 | + module.label, module.module_dir |
| 57 | + ); |
| 58 | + |
| 59 | + publish_module(module, cli_path, config_path, server_url)?; |
| 60 | + generate_module_bindings(module, cli_path, config_path)?; |
| 61 | + seed_accounts(cli_path, config_path, server_url)?; |
| 62 | + let runs_dir = tempfile::tempdir().context("failed to create temporary benchmark runs directory")?; |
| 63 | + run_benchmark(module, server_url, runs_dir.path())?; |
| 64 | + |
| 65 | + let result_path = find_result_json(runs_dir.path())?; |
| 66 | + let result_json = fs::read_to_string(&result_path) |
| 67 | + .with_context(|| format!("failed to read benchmark result {}", result_path.display()))?; |
| 68 | + let tps = result_tps(&result_json)?; |
| 69 | + |
| 70 | + if tps < module.min_tps { |
| 71 | + eprintln!( |
| 72 | + "Keynote perf regression for {} module: throughput {tps:.0} TPS < {:.0} TPS\n\nResult JSON:\n{}", |
| 73 | + module.label, module.min_tps, result_json |
| 74 | + ); |
| 75 | + bail!( |
| 76 | + "keynote benchmark throughput for {} module is below threshold", |
| 77 | + module.label |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + println!( |
| 82 | + "Keynote perf check passed for {} module: throughput {tps:.0} TPS >= {:.0} TPS ({})", |
| 83 | + module.label, |
| 84 | + module.min_tps, |
| 85 | + result_path.display() |
| 86 | + ); |
| 87 | + Ok(()) |
| 88 | +} |
| 89 | + |
| 90 | +fn publish_module(module: &BenchmarkModule, cli_path: &Path, config_path: &Path, server_url: &str) -> Result<()> { |
| 91 | + let label = format!("spacetime publish keynote {} module", module.label); |
| 92 | + run_cli( |
| 93 | + cli_path, |
| 94 | + config_path, |
| 95 | + &[ |
| 96 | + "publish", |
| 97 | + "--server", |
| 98 | + server_url, |
| 99 | + "--module-path", |
| 100 | + module.module_dir, |
| 101 | + "--yes", |
| 102 | + "--clear-database", |
| 103 | + DATABASE_NAME, |
| 104 | + ], |
| 105 | + &label, |
| 106 | + ) |
| 107 | +} |
| 108 | + |
| 109 | +fn generate_module_bindings(module: &BenchmarkModule, cli_path: &Path, config_path: &Path) -> Result<()> { |
| 110 | + let label = format!("spacetime generate keynote {} TypeScript bindings", module.label); |
| 111 | + run_cli( |
| 112 | + cli_path, |
| 113 | + config_path, |
| 114 | + &[ |
| 115 | + "generate", |
| 116 | + "--lang", |
| 117 | + "typescript", |
| 118 | + "--out-dir", |
| 119 | + KEYNOTE_BINDINGS_DIR, |
| 120 | + "--module-path", |
| 121 | + module.module_dir, |
| 122 | + "--yes", |
| 123 | + ], |
| 124 | + &label, |
| 125 | + ) |
| 126 | +} |
| 127 | + |
| 128 | +fn seed_accounts(cli_path: &Path, config_path: &Path, server_url: &str) -> Result<()> { |
| 129 | + run_cli( |
| 130 | + cli_path, |
| 131 | + config_path, |
| 132 | + &[ |
| 133 | + "call", |
| 134 | + "--server", |
| 135 | + server_url, |
| 136 | + DATABASE_NAME, |
| 137 | + "seed", |
| 138 | + SEED_ACCOUNTS, |
| 139 | + SEED_INITIAL_BALANCE, |
| 140 | + ], |
| 141 | + "spacetime call seed", |
| 142 | + ) |
| 143 | +} |
| 144 | + |
| 145 | +fn run_cli(cli_path: &Path, config_path: &Path, args: &[&str], label: &str) -> Result<()> { |
| 146 | + let mut cmd = Command::new(cli_path); |
| 147 | + cmd.arg("--config-path").arg(config_path).args(args); |
| 148 | + run_command(&mut cmd, label) |
| 149 | +} |
| 150 | + |
| 151 | +fn run_benchmark(module: &BenchmarkModule, server_url: &str, runs_dir: &Path) -> Result<()> { |
| 152 | + let mut cmd = Command::new("pnpm"); |
| 153 | + cmd.args([ |
| 154 | + "run", |
| 155 | + "bench", |
| 156 | + DATABASE_NAME, |
| 157 | + "--seconds", |
| 158 | + BENCH_SECONDS, |
| 159 | + "--concurrency", |
| 160 | + BENCH_CONCURRENCY, |
| 161 | + "--connectors", |
| 162 | + "spacetimedb", |
| 163 | + ]) |
| 164 | + .current_dir(KEYNOTE_DIR) |
| 165 | + .env("NODE_ENV", "production") |
| 166 | + .env("BENCH_PIPELINED", "1") |
| 167 | + .env("MAX_INFLIGHT_PER_WORKER", MAX_INFLIGHT_PER_WORKER) |
| 168 | + .env("BENCH_RUNS_DIR", runs_dir) |
| 169 | + .env("STDB_URL", server_url) |
| 170 | + .env("STDB_MODULE", DATABASE_NAME) |
| 171 | + .env("SEED_ACCOUNTS", SEED_ACCOUNTS) |
| 172 | + .env("SEED_INITIAL_BALANCE", SEED_INITIAL_BALANCE); |
| 173 | + let label = format!("keynote SpacetimeDB benchmark against {} module", module.label); |
| 174 | + run_command(&mut cmd, &label) |
| 175 | +} |
| 176 | + |
| 177 | +fn run_command(cmd: &mut Command, label: &str) -> Result<()> { |
| 178 | + let status = cmd.status().with_context(|| format!("failed to spawn {label}"))?; |
| 179 | + ensure!(status.success(), "{label} failed with status {status}"); |
| 180 | + Ok(()) |
| 181 | +} |
| 182 | + |
| 183 | +fn find_result_json(runs_dir: &Path) -> Result<PathBuf> { |
| 184 | + let mut matches = Vec::new(); |
| 185 | + for entry in fs::read_dir(runs_dir).with_context(|| format!("failed to read {}", runs_dir.display()))? { |
| 186 | + let entry = entry?; |
| 187 | + let path = entry.path(); |
| 188 | + let Some(name) = path.file_name().and_then(|name| name.to_str()) else { |
| 189 | + continue; |
| 190 | + }; |
| 191 | + if name.starts_with("test-1-spacetimedb-") && name.ends_with(".json") { |
| 192 | + matches.push(path); |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + match matches.len() { |
| 197 | + 0 => bail!( |
| 198 | + "benchmark did not write a test-1-spacetimedb result JSON in {}", |
| 199 | + runs_dir.display() |
| 200 | + ), |
| 201 | + 1 => Ok(matches.remove(0)), |
| 202 | + _ => bail!( |
| 203 | + "benchmark wrote multiple test-1-spacetimedb result JSON files in {}: {:?}", |
| 204 | + runs_dir.display(), |
| 205 | + matches |
| 206 | + ), |
| 207 | + } |
| 208 | +} |
| 209 | + |
| 210 | +fn result_tps(result_json: &str) -> Result<f64> { |
| 211 | + let value: Value = serde_json::from_str(result_json).context("failed to parse benchmark result JSON")?; |
| 212 | + value |
| 213 | + .pointer("/results/0/res/tps") |
| 214 | + .and_then(Value::as_f64) |
| 215 | + .context("benchmark result JSON is missing results[0].res.tps") |
| 216 | +} |
0 commit comments