Skip to content

Commit 571f8b2

Browse files
authored
Miscellaneous improvements to make the project more idiomatic (#2)
1 parent 0bad201 commit 571f8b2

15 files changed

+557
-274
lines changed

.github/workflows/main.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ concurrency:
1111
cancel-in-progress: true
1212

1313
jobs:
14-
lint:
14+
lint-and-test:
1515
runs-on: ubuntu-latest
1616
steps:
1717
- uses: actions/checkout@v3
@@ -22,6 +22,9 @@ jobs:
2222
- name: Run clippy
2323
run: cargo clippy
2424

25+
- name: Run tests
26+
run: cargo test
27+
2528
build:
2629
runs-on: ubuntu-latest
2730
steps:

Cargo.lock

+17-16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ log = "0.4.6"
1818
env_logger = "0.9.0"
1919
config = "0.13.3"
2020
syscalls = "0.6.13"
21+
serde = { version = "1.0.171", features = ["derive"] }

src/lib.rs

+170-16
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,182 @@
1+
use serde::Deserialize;
2+
13
pub mod worker;
24

3-
#[derive(Debug, Copy, Clone)]
5+
#[derive(Debug, Copy, Clone, Deserialize)]
6+
#[serde(tag = "distribution")]
47
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 },
712
}
813

9-
#[derive(Debug, Copy, Clone)]
14+
#[derive(Debug, Copy, Clone, Deserialize)]
15+
#[serde(rename_all = "lowercase", tag = "type")]
1016
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+
},
1429
}
1530

16-
#[derive(Debug, Copy, Clone)]
31+
#[derive(Debug, Copy, Clone, Deserialize)]
1732
pub struct WorkloadConfig {
1833
pub restart_interval: u64,
19-
pub endpoints_dist: Distribution,
2034
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+
}
28182
}

0 commit comments

Comments
 (0)