|
| 1 | +use serde::Deserialize; |
| 2 | + |
1 | 3 | pub mod worker;
|
2 | 4 |
|
3 |
| -#[derive(Debug, Copy, Clone)] |
| 5 | +#[derive(Debug, Copy, Clone, Deserialize)] |
| 6 | +#[serde(tag = "distribution")] |
4 | 7 | pub enum Distribution {
|
5 |
| - Zipfian, |
6 |
| - Uniform, |
| 8 | + #[serde(alias = "zipf")] |
| 9 | + Zipfian { n_ports: u64, exponent: f64 }, |
| 10 | + #[serde(alias = "uniform")] |
| 11 | + Uniform { lower: u64, upper: u64 }, |
7 | 12 | }
|
8 | 13 |
|
9 |
| -#[derive(Debug, Copy, Clone)] |
| 14 | +#[derive(Debug, Copy, Clone, Deserialize)] |
| 15 | +#[serde(rename_all = "lowercase", tag = "type")] |
10 | 16 | pub enum Workload {
|
11 |
| - Endpoints, |
12 |
| - Processes, |
13 |
| - Syscalls, |
| 17 | + Endpoints { |
| 18 | + #[serde(flatten)] |
| 19 | + distribution: Distribution, |
| 20 | + }, |
| 21 | + Processes { |
| 22 | + arrival_rate: f64, |
| 23 | + departure_rate: f64, |
| 24 | + random_process: bool, |
| 25 | + }, |
| 26 | + Syscalls { |
| 27 | + arrival_rate: f64, |
| 28 | + }, |
14 | 29 | }
|
15 | 30 |
|
16 |
| -#[derive(Debug, Copy, Clone)] |
| 31 | +#[derive(Debug, Copy, Clone, Deserialize)] |
17 | 32 | pub struct WorkloadConfig {
|
18 | 33 | pub restart_interval: u64,
|
19 |
| - pub endpoints_dist: Distribution, |
20 | 34 | pub workload: Workload,
|
21 |
| - pub zipf_exponent: f64, |
22 |
| - pub n_ports: u64, |
23 |
| - pub uniform_lower: u64, |
24 |
| - pub uniform_upper: u64, |
25 |
| - pub arrival_rate: f64, |
26 |
| - pub departure_rate: f64, |
27 |
| - pub random_process: bool, |
| 35 | +} |
| 36 | + |
| 37 | +#[cfg(test)] |
| 38 | +mod tests { |
| 39 | + use super::*; |
| 40 | + use config::{Config, File, FileFormat}; |
| 41 | + |
| 42 | + #[test] |
| 43 | + fn test_processes() { |
| 44 | + let input = r#" |
| 45 | + restart_interval = 10 |
| 46 | +
|
| 47 | + [workload] |
| 48 | + type = "processes" |
| 49 | + arrival_rate = 10.0 |
| 50 | + departure_rate = 200.0 |
| 51 | + random_process = true |
| 52 | + "#; |
| 53 | + |
| 54 | + let config = Config::builder() |
| 55 | + .add_source(File::from_str(input, FileFormat::Toml)) |
| 56 | + .build() |
| 57 | + .expect("failed to parse configuration") |
| 58 | + .try_deserialize::<WorkloadConfig>() |
| 59 | + .expect("failed to deserialize into WorkloadConfig"); |
| 60 | + |
| 61 | + let WorkloadConfig { |
| 62 | + restart_interval, |
| 63 | + workload, |
| 64 | + } = config; |
| 65 | + assert_eq!(restart_interval, 10); |
| 66 | + if let Workload::Processes { |
| 67 | + arrival_rate, |
| 68 | + departure_rate, |
| 69 | + random_process, |
| 70 | + } = workload |
| 71 | + { |
| 72 | + assert_eq!(arrival_rate, 10.0); |
| 73 | + assert_eq!(departure_rate, 200.0); |
| 74 | + assert!(random_process); |
| 75 | + } else { |
| 76 | + panic!("wrong workload type found"); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + #[test] |
| 81 | + fn test_endpoints_zipf() { |
| 82 | + let input = r#" |
| 83 | + restart_interval = 10 |
| 84 | +
|
| 85 | + [workload] |
| 86 | + type = "endpoints" |
| 87 | + distribution = "zipf" |
| 88 | + n_ports = 200 |
| 89 | + exponent = 1.4 |
| 90 | + "#; |
| 91 | + |
| 92 | + let config = Config::builder() |
| 93 | + .add_source(File::from_str(input, FileFormat::Toml)) |
| 94 | + .build() |
| 95 | + .expect("failed to parse configuration") |
| 96 | + .try_deserialize::<WorkloadConfig>() |
| 97 | + .expect("failed to deserialize into WorkloadConfig"); |
| 98 | + |
| 99 | + let WorkloadConfig { |
| 100 | + restart_interval, |
| 101 | + workload, |
| 102 | + } = config; |
| 103 | + assert_eq!(restart_interval, 10); |
| 104 | + |
| 105 | + if let Workload::Endpoints { distribution, .. } = workload { |
| 106 | + if let Distribution::Zipfian { n_ports, exponent } = distribution { |
| 107 | + assert_eq!(n_ports, 200); |
| 108 | + assert_eq!(exponent, 1.4); |
| 109 | + } else { |
| 110 | + panic!("wrong distribution type found"); |
| 111 | + } |
| 112 | + } else { |
| 113 | + panic!("wrong workload type found"); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + #[test] |
| 118 | + fn test_endpoints_uniform() { |
| 119 | + let input = r#" |
| 120 | + restart_interval = 10 |
| 121 | +
|
| 122 | + [workload] |
| 123 | + type = "endpoints" |
| 124 | + distribution = "uniform" |
| 125 | + upper = 100 |
| 126 | + lower = 1 |
| 127 | + "#; |
| 128 | + |
| 129 | + let config = Config::builder() |
| 130 | + .add_source(File::from_str(input, FileFormat::Toml)) |
| 131 | + .build() |
| 132 | + .expect("failed to parse configuration") |
| 133 | + .try_deserialize::<WorkloadConfig>() |
| 134 | + .expect("failed to deserialize into WorkloadConfig"); |
| 135 | + |
| 136 | + let WorkloadConfig { |
| 137 | + restart_interval, |
| 138 | + workload, |
| 139 | + } = config; |
| 140 | + assert_eq!(restart_interval, 10); |
| 141 | + |
| 142 | + if let Workload::Endpoints { distribution } = workload { |
| 143 | + if let Distribution::Uniform { lower, upper } = distribution { |
| 144 | + assert_eq!(lower, 1); |
| 145 | + assert_eq!(upper, 100); |
| 146 | + } else { |
| 147 | + panic!("wrong distribution type found"); |
| 148 | + } |
| 149 | + } else { |
| 150 | + panic!("wrong workload type found"); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + #[test] |
| 155 | + fn test_syscalls() { |
| 156 | + let input = r#" |
| 157 | + restart_interval = 10 |
| 158 | +
|
| 159 | + [workload] |
| 160 | + type = "syscalls" |
| 161 | + arrival_rate = 10.0 |
| 162 | + "#; |
| 163 | + |
| 164 | + let config = Config::builder() |
| 165 | + .add_source(File::from_str(input, FileFormat::Toml)) |
| 166 | + .build() |
| 167 | + .expect("failed to parse configuration") |
| 168 | + .try_deserialize::<WorkloadConfig>() |
| 169 | + .expect("failed to deserialize into WorkloadConfig"); |
| 170 | + |
| 171 | + let WorkloadConfig { |
| 172 | + restart_interval, |
| 173 | + workload, |
| 174 | + } = config; |
| 175 | + assert_eq!(restart_interval, 10); |
| 176 | + if let Workload::Syscalls { arrival_rate } = workload { |
| 177 | + assert_eq!(arrival_rate, 10.0); |
| 178 | + } else { |
| 179 | + panic!("wrong workload type found"); |
| 180 | + } |
| 181 | + } |
28 | 182 | }
|
0 commit comments