Skip to content

Commit ac8ce47

Browse files
committed
refactor(torture): probabilities handling
Effectively make the options 'commit-crash,' 'rollback,' and 'rollback-crash' reflect the actual probability of those actions occurring at each iteration.
1 parent ab355e7 commit ac8ce47

File tree

2 files changed

+44
-29
lines changed

2 files changed

+44
-29
lines changed

torture/src/supervisor/cli.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -63,25 +63,32 @@ pub struct WorkloadParams {
6363
#[arg(long = "random-size")]
6464
pub random_size: bool,
6565

66-
/// When exercising a new commit, the probability of causing it to crash.
66+
/// When executing a workload iteration, this is the probability of executing a commit
67+
/// and causing it to crash.
68+
///
69+
/// The sum of `commit-crash`, `rollback` and `rollback-crash` must be lower than 100.
6770
///
6871
/// Accepted values are in the range of 0 to 100
6972
#[clap(default_value = "20")]
7073
#[clap(value_parser=clap::value_parser!(u8).range(0..=100))]
7174
#[arg(long = "commit-crash")]
7275
pub crash: u8,
7376

74-
/// Instead of exercising a new commit, this is the probability of executing a rollback.
77+
/// When executing a workload iteration ,this is the probability of executing a rollback.
78+
///
79+
/// The sum of `commit-crash`, `rollback` and `rollback-crash` must be lower than 100.
7580
///
7681
/// Accepted values are in the range of 0 to 100
7782
#[clap(default_value = "5")]
7883
#[clap(value_parser=clap::value_parser!(u8).range(0..=100))]
7984
#[arg(long = "rollback")]
8085
pub rollback: u8,
8186

82-
/// Instead of exercising a new commit, this is the probability of executing a rollback
87+
/// When executing a workload iteration, this is the probability of executing a rollback
8388
/// and causing it to crash.
8489
///
90+
/// The sum of `commit-crash`, `rollback` and `rollback-crash` must be lower than 100.
91+
///
8592
/// Accepted values are in the range of 0 to 100
8693
#[clap(default_value = "5")]
8794
#[clap(value_parser=clap::value_parser!(u8).range(0..=100))]

torture/src/supervisor/workload.rs

+34-26
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use anyhow::Result;
22
use imbl::OrdMap;
3-
use rand::prelude::*;
3+
use rand::{distributions::WeightedIndex, prelude::*};
44
use std::time::Duration;
55
use tempfile::TempDir;
66
use tokio::time::{error::Elapsed, timeout};
@@ -28,31 +28,39 @@ struct Biases {
2828
/// When generating a key, whether it should be one that was appeared somewhere or a brand new
2929
/// key.
3030
new_key: f64,
31-
/// When exercising a new commit, the probability of causing it to crash.
32-
crash: f64,
33-
/// Instead of exercising a new commit, this is the probability of executing a rollback.
34-
rollback: f64,
35-
/// Instead of exercising a new commit, this is the probability of executing a rollback
31+
/// When executing a workload iteration, this is the probability of executing a commit.
32+
commit: u8,
33+
/// When executing a workload iteration, this is the probability of executing a commit
3634
/// and causing it to crash.
37-
rollback_crash: f64,
35+
commit_crash: u8,
36+
/// When executing a workload iteration ,this is the probability of executing a rollback.
37+
rollback: u8,
38+
/// When executing a workload iteration, this is the probability of executing a rollback
39+
/// and causing it to crash.
40+
rollback_crash: u8,
3841
}
3942

4043
impl Biases {
4144
fn new(
4245
delete: u8,
4346
overflow: u8,
4447
new_key: u8,
45-
crash: u8,
48+
commit_crash: u8,
4649
rollback: u8,
4750
rollback_crash: u8,
4851
) -> Self {
52+
let Some(commit) = 100u8.checked_sub(commit_crash + rollback + rollback_crash) else {
53+
panic!("Sum of `commit-crash`, `rollback` and `rollback-crash` must be lower than 100");
54+
};
55+
4956
Self {
5057
delete: (delete as f64) / 100.0,
5158
overflow: (overflow as f64) / 100.0,
5259
new_key: (new_key as f64) / 100.0,
53-
crash: (crash as f64) / 100.0,
54-
rollback: (rollback as f64) / 100.0,
55-
rollback_crash: (rollback_crash as f64) / 100.0,
60+
commit,
61+
commit_crash,
62+
rollback,
63+
rollback_crash,
5664
}
5765
}
5866
}
@@ -310,22 +318,22 @@ impl Workload {
310318
let rr = agent.rr().clone();
311319
trace!("run_iteration");
312320

313-
if self.state.rng.gen_bool(self.state.biases.rollback_crash) {
314-
self.exercise_rollback_crashing(&rr).await?;
315-
return Ok(());
316-
}
317-
318-
if self.state.rng.gen_bool(self.state.biases.rollback) {
319-
self.exercise_rollback(&rr).await?;
320-
return Ok(());
321-
}
322-
323-
if self.state.rng.gen_bool(self.state.biases.crash) {
324-
self.exercise_commit_crashing(&rr).await?;
325-
return Ok(());
321+
let dist = WeightedIndex::new([
322+
self.state.biases.commit,
323+
self.state.biases.commit_crash,
324+
self.state.biases.rollback,
325+
self.state.biases.rollback_crash,
326+
])
327+
.unwrap();
328+
329+
match self.state.rng.sample(dist) {
330+
0 => self.exercise_commit(&rr).await?,
331+
1 => self.exercise_commit_crashing(&rr).await?,
332+
2 => self.exercise_rollback(&rr).await?,
333+
3 => self.exercise_rollback_crashing(&rr).await?,
334+
_ => unreachable!(),
326335
}
327336

328-
self.exercise_commit(&rr).await?;
329337
Ok(())
330338
}
331339

@@ -646,7 +654,7 @@ impl Workload {
646654
assert!(self.agent.is_none());
647655
controller::spawn_agent_into(&mut self.agent).await?;
648656
let workdir = self.workdir.path().display().to_string();
649-
let rollback = self.state.biases.rollback > 0.0;
657+
let rollback = self.state.biases.rollback > 0;
650658
self.agent
651659
.as_mut()
652660
.unwrap()

0 commit comments

Comments
 (0)