mirror of
https://github.com/aljazceru/turso.git
synced 2025-12-26 20:44:23 +01:00
I wanted to assist the current development in an up-to-date fashion, this PR adds drop table(which is being implemented currently in https://github.com/tursodatabase/limbo/pull/897) testing support to the generator. Unfortunately, we don't have feature flags in the simulator yet, so the users should manually fix the generation probability in `simulator/generation/plan.rs#L644` and `simulator/generation/property.rs#L629`. Closes #949
117 lines
3.6 KiB
Rust
117 lines
3.6 KiB
Rust
use std::path::Path;
|
|
use std::rc::Rc;
|
|
use std::sync::Arc;
|
|
|
|
use limbo_core::{Connection, Database};
|
|
use rand::{Rng, SeedableRng};
|
|
use rand_chacha::ChaCha8Rng;
|
|
|
|
use crate::model::table::Table;
|
|
|
|
use crate::runner::io::SimulatorIO;
|
|
|
|
use super::cli::SimulatorCLI;
|
|
|
|
#[derive(Clone)]
|
|
pub(crate) struct SimulatorEnv {
|
|
pub(crate) opts: SimulatorOpts,
|
|
pub(crate) tables: Vec<Table>,
|
|
pub(crate) connections: Vec<SimConnection>,
|
|
pub(crate) io: Arc<SimulatorIO>,
|
|
pub(crate) db: Arc<Database>,
|
|
pub(crate) rng: ChaCha8Rng,
|
|
}
|
|
|
|
impl SimulatorEnv {
|
|
pub(crate) fn new(seed: u64, cli_opts: &SimulatorCLI, db_path: &Path) -> Self {
|
|
let mut rng = ChaCha8Rng::seed_from_u64(seed);
|
|
|
|
let (create_percent, read_percent, write_percent, delete_percent, drop_percent) = {
|
|
let total = 100.0;
|
|
|
|
let read_percent = rng.gen_range(0.0..=total);
|
|
let write_percent = total - read_percent;
|
|
|
|
// Create percent should be 5-15% of the write percent
|
|
let create_percent = rng.gen_range(0.05..=0.15) * write_percent;
|
|
// Drop percent should be 2-5% of the write percent
|
|
let drop_percent = rng.gen_range(0.02..=0.05) * write_percent;
|
|
// Delete percent should be 10-20% of the write percent
|
|
let delete_percent = rng.gen_range(0.1..=0.2) * write_percent;
|
|
|
|
let write_percent = write_percent - create_percent - delete_percent - drop_percent;
|
|
|
|
(
|
|
create_percent,
|
|
read_percent,
|
|
write_percent,
|
|
delete_percent,
|
|
drop_percent,
|
|
)
|
|
};
|
|
|
|
let opts = SimulatorOpts {
|
|
ticks: rng.gen_range(cli_opts.minimum_size..=cli_opts.maximum_size),
|
|
max_connections: 1, // TODO: for now let's use one connection as we didn't implement
|
|
// correct transactions processing
|
|
max_tables: rng.gen_range(0..128),
|
|
create_percent,
|
|
read_percent,
|
|
write_percent,
|
|
delete_percent,
|
|
drop_percent,
|
|
page_size: 4096, // TODO: randomize this too
|
|
max_interactions: rng.gen_range(cli_opts.minimum_size..=cli_opts.maximum_size),
|
|
max_time_simulation: cli_opts.maximum_time,
|
|
};
|
|
|
|
let io = Arc::new(SimulatorIO::new(seed, opts.page_size).unwrap());
|
|
|
|
// Remove existing database file if it exists
|
|
if db_path.exists() {
|
|
std::fs::remove_file(db_path).unwrap();
|
|
}
|
|
|
|
let db = match Database::open_file(io.clone(), db_path.to_str().unwrap()) {
|
|
Ok(db) => db,
|
|
Err(e) => {
|
|
panic!("error opening simulator test file {:?}: {:?}", db_path, e);
|
|
}
|
|
};
|
|
|
|
let connections = vec![SimConnection::Disconnected; opts.max_connections];
|
|
|
|
SimulatorEnv {
|
|
opts,
|
|
tables: Vec::new(),
|
|
connections,
|
|
rng,
|
|
io,
|
|
db,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub(crate) enum SimConnection {
|
|
Connected(Rc<Connection>),
|
|
Disconnected,
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub(crate) struct SimulatorOpts {
|
|
pub(crate) ticks: usize,
|
|
pub(crate) max_connections: usize,
|
|
pub(crate) max_tables: usize,
|
|
// this next options are the distribution of workload where read_percent + write_percent +
|
|
// delete_percent == 100%
|
|
pub(crate) create_percent: f64,
|
|
pub(crate) read_percent: f64,
|
|
pub(crate) write_percent: f64,
|
|
pub(crate) delete_percent: f64,
|
|
pub(crate) drop_percent: f64,
|
|
pub(crate) max_interactions: usize,
|
|
pub(crate) page_size: usize,
|
|
pub(crate) max_time_simulation: usize,
|
|
}
|