Skip to content

Commit

Permalink
Benchmark modules in release mode
Browse files Browse the repository at this point in the history
Previously module benchmarks were running against modules compiled in debug mode, which made them much slower and not represent real-world performance.

This PR fixes that and adds explicit `CompilationMode` enum that needs to be passed to `CompiledModule::compile` to prevent accidental debug builds in other places.
  • Loading branch information
RReverser committed Oct 10, 2023
1 parent 91a2962 commit 1778a70
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 39 deletions.
4 changes: 2 additions & 2 deletions crates/bench/src/spacetime_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use spacetimedb::db::datastore::traits::TableSchema;
use spacetimedb::db::{Config, FsyncPolicy, Storage};
use spacetimedb_lib::sats::product;
use spacetimedb_lib::{sats::ArrayValue, AlgebraicValue, ProductValue};
use spacetimedb_testing::modules::{start_runtime, CompiledModule, ModuleHandle};
use spacetimedb_testing::modules::{start_runtime, CompilationMode, CompiledModule, ModuleHandle};
use tokio::runtime::Runtime;

use crate::{
Expand All @@ -14,7 +14,7 @@ use crate::{

lazy_static::lazy_static! {
pub static ref BENCHMARKS_MODULE: CompiledModule =
CompiledModule::compile("benchmarks");
CompiledModule::compile("benchmarks", CompilationMode::Release);
}

/// A benchmark backend that invokes a spacetime module.
Expand Down
10 changes: 8 additions & 2 deletions crates/testing/src/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,15 @@ pub struct CompiledModule {
program_bytes: OnceLock<Vec<u8>>,
}

#[derive(Debug, PartialEq, Eq)]
pub enum CompilationMode {
Debug,
Release,
}

impl CompiledModule {
pub fn compile(name: &str) -> Self {
let path = spacetimedb_cli::build(&module_path(name), false, true).unwrap();
pub fn compile(name: &str, mode: CompilationMode) -> Self {
let path = spacetimedb_cli::build(&module_path(name), false, mode == CompilationMode::Debug).unwrap();
Self {
name: name.to_owned(),
path,
Expand Down
4 changes: 2 additions & 2 deletions crates/testing/src/sdk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::thread::JoinHandle;
use std::{collections::HashSet, fs::create_dir_all, sync::Mutex};

use crate::invoke_cli;
use crate::modules::{module_path, CompiledModule};
use crate::modules::{module_path, CompilationMode, CompiledModule};
use std::path::Path;
use tempfile::TempDir;

Expand Down Expand Up @@ -110,7 +110,7 @@ impl Test {
pub fn run(&self) {
ensure_standalone_process();

let compiled = CompiledModule::compile(&self.module_name);
let compiled = CompiledModule::compile(&self.module_name, CompilationMode::Debug);

generate_bindings(
&self.generate_language,
Expand Down
72 changes: 39 additions & 33 deletions crates/testing/tests/standalone_integration_test.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
use serde_json::Value;
use serial_test::serial;
use spacetimedb_testing::modules::{CompiledModule, DEFAULT_CONFIG};
use spacetimedb_testing::modules::{CompilationMode, CompiledModule, DEFAULT_CONFIG};

// The tests MUST be run in sequence because they read the OS environment
// and can cause a race when run in parallel.

fn test_calling_a_reducer_in_module(module_name: &'static str) {
CompiledModule::compile(module_name).with_module_async(DEFAULT_CONFIG, |module| async move {
let json = r#"{"call": {"fn": "add", "args": ["Tyrion"]}}"#.to_string();
module.send(json).await.unwrap();
let json = r#"{"call": {"fn": "say_hello", "args": []}}"#.to_string();
module.send(json).await.unwrap();

let lines = module.read_log(Some(10)).await;
let lines: Vec<&str> = lines.trim().split('\n').collect();

assert_eq!(lines.len(), 4);

let json: Value = serde_json::from_str(lines[2]).unwrap();
assert_eq!(json["message"], Value::String("Hello, Tyrion!".to_string()));
let json: Value = serde_json::from_str(lines[3]).unwrap();
assert_eq!(json["message"], Value::String("Hello, World!".to_string()));
});
CompiledModule::compile(module_name, CompilationMode::Debug).with_module_async(
DEFAULT_CONFIG,
|module| async move {
let json = r#"{"call": {"fn": "add", "args": ["Tyrion"]}}"#.to_string();
module.send(json).await.unwrap();
let json = r#"{"call": {"fn": "say_hello", "args": []}}"#.to_string();
module.send(json).await.unwrap();

let lines = module.read_log(Some(10)).await;
let lines: Vec<&str> = lines.trim().split('\n').collect();

assert_eq!(lines.len(), 4);

let json: Value = serde_json::from_str(lines[2]).unwrap();
assert_eq!(json["message"], Value::String("Hello, Tyrion!".to_string()));
let json: Value = serde_json::from_str(lines[3]).unwrap();
assert_eq!(json["message"], Value::String("Hello, World!".to_string()));
},
);
}

#[test]
Expand All @@ -39,20 +42,23 @@ fn test_calling_a_reducer_csharp() {
#[test]
#[serial]
fn test_calling_a_reducer_with_private_table() {
CompiledModule::compile("rust-wasm-test").with_module_async(DEFAULT_CONFIG, |module| async move {
let json = r#"{"call": {"fn": "add_private", "args": ["Tyrion"]}}"#.to_string();
module.send(json).await.unwrap();
let json = r#"{"call": {"fn": "query_private", "args": []}}"#.to_string();
module.send(json).await.unwrap();

let lines = module.read_log(Some(10)).await;
let lines: Vec<&str> = lines.trim().split('\n').collect();

assert_eq!(lines.len(), 8);

let json: Value = serde_json::from_str(lines[6]).unwrap();
assert_eq!(json["message"], Value::String("Private, Tyrion!".to_string()));
let json: Value = serde_json::from_str(lines[7]).unwrap();
assert_eq!(json["message"], Value::String("Private, World!".to_string()));
});
CompiledModule::compile("rust-wasm-test", CompilationMode::Debug).with_module_async(
DEFAULT_CONFIG,
|module| async move {
let json = r#"{"call": {"fn": "add_private", "args": ["Tyrion"]}}"#.to_string();
module.send(json).await.unwrap();
let json = r#"{"call": {"fn": "query_private", "args": []}}"#.to_string();
module.send(json).await.unwrap();

let lines = module.read_log(Some(10)).await;
let lines: Vec<&str> = lines.trim().split('\n').collect();

assert_eq!(lines.len(), 8);

let json: Value = serde_json::from_str(lines[6]).unwrap();
assert_eq!(json["message"], Value::String("Private, Tyrion!".to_string()));
let json: Value = serde_json::from_str(lines[7]).unwrap();
assert_eq!(json["message"], Value::String("Private, World!".to_string()));
},
);
}

0 comments on commit 1778a70

Please sign in to comment.