|
1 | 1 | use anyhow::Result;
|
2 | 2 | use imbl::OrdMap;
|
3 |
| -use rand::prelude::*; |
| 3 | +use rand::{distributions::WeightedIndex, prelude::*}; |
4 | 4 | use std::time::Duration;
|
5 | 5 | use tempfile::TempDir;
|
6 | 6 | use tokio::time::{error::Elapsed, timeout};
|
@@ -28,31 +28,39 @@ struct Biases {
|
28 | 28 | /// When generating a key, whether it should be one that was appeared somewhere or a brand new
|
29 | 29 | /// key.
|
30 | 30 | 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 |
36 | 34 | /// 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, |
38 | 41 | }
|
39 | 42 |
|
40 | 43 | impl Biases {
|
41 | 44 | fn new(
|
42 | 45 | delete: u8,
|
43 | 46 | overflow: u8,
|
44 | 47 | new_key: u8,
|
45 |
| - crash: u8, |
| 48 | + commit_crash: u8, |
46 | 49 | rollback: u8,
|
47 | 50 | rollback_crash: u8,
|
48 | 51 | ) -> 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 | + |
49 | 56 | Self {
|
50 | 57 | delete: (delete as f64) / 100.0,
|
51 | 58 | overflow: (overflow as f64) / 100.0,
|
52 | 59 | 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, |
56 | 64 | }
|
57 | 65 | }
|
58 | 66 | }
|
@@ -310,22 +318,22 @@ impl Workload {
|
310 | 318 | let rr = agent.rr().clone();
|
311 | 319 | trace!("run_iteration");
|
312 | 320 |
|
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!(), |
326 | 335 | }
|
327 | 336 |
|
328 |
| - self.exercise_commit(&rr).await?; |
329 | 337 | Ok(())
|
330 | 338 | }
|
331 | 339 |
|
@@ -646,7 +654,7 @@ impl Workload {
|
646 | 654 | assert!(self.agent.is_none());
|
647 | 655 | controller::spawn_agent_into(&mut self.agent).await?;
|
648 | 656 | 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; |
650 | 658 | self.agent
|
651 | 659 | .as_mut()
|
652 | 660 | .unwrap()
|
|
0 commit comments