Skip to content

Commit

Permalink
Miscellaneous improvements to make the project more idiomatic (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
Molter73 authored Aug 14, 2023
1 parent 0bad201 commit 571f8b2
Show file tree
Hide file tree
Showing 15 changed files with 557 additions and 274 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ concurrency:
cancel-in-progress: true

jobs:
lint:
lint-and-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand All @@ -22,6 +22,9 @@ jobs:
- name: Run clippy
run: cargo clippy

- name: Run tests
run: cargo test

build:
runs-on: ubuntu-latest
steps:
Expand Down
33 changes: 17 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ log = "0.4.6"
env_logger = "0.9.0"
config = "0.13.3"
syscalls = "0.6.13"
serde = { version = "1.0.171", features = ["derive"] }
186 changes: 170 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,182 @@
use serde::Deserialize;

pub mod worker;

#[derive(Debug, Copy, Clone)]
#[derive(Debug, Copy, Clone, Deserialize)]
#[serde(tag = "distribution")]
pub enum Distribution {
Zipfian,
Uniform,
#[serde(alias = "zipf")]
Zipfian { n_ports: u64, exponent: f64 },
#[serde(alias = "uniform")]
Uniform { lower: u64, upper: u64 },
}

#[derive(Debug, Copy, Clone)]
#[derive(Debug, Copy, Clone, Deserialize)]
#[serde(rename_all = "lowercase", tag = "type")]
pub enum Workload {
Endpoints,
Processes,
Syscalls,
Endpoints {
#[serde(flatten)]
distribution: Distribution,
},
Processes {
arrival_rate: f64,
departure_rate: f64,
random_process: bool,
},
Syscalls {
arrival_rate: f64,
},
}

#[derive(Debug, Copy, Clone)]
#[derive(Debug, Copy, Clone, Deserialize)]
pub struct WorkloadConfig {
pub restart_interval: u64,
pub endpoints_dist: Distribution,
pub workload: Workload,
pub zipf_exponent: f64,
pub n_ports: u64,
pub uniform_lower: u64,
pub uniform_upper: u64,
pub arrival_rate: f64,
pub departure_rate: f64,
pub random_process: bool,
}

#[cfg(test)]
mod tests {
use super::*;
use config::{Config, File, FileFormat};

#[test]
fn test_processes() {
let input = r#"
restart_interval = 10
[workload]
type = "processes"
arrival_rate = 10.0
departure_rate = 200.0
random_process = true
"#;

let config = Config::builder()
.add_source(File::from_str(input, FileFormat::Toml))
.build()
.expect("failed to parse configuration")
.try_deserialize::<WorkloadConfig>()
.expect("failed to deserialize into WorkloadConfig");

let WorkloadConfig {
restart_interval,
workload,
} = config;
assert_eq!(restart_interval, 10);
if let Workload::Processes {
arrival_rate,
departure_rate,
random_process,
} = workload
{
assert_eq!(arrival_rate, 10.0);
assert_eq!(departure_rate, 200.0);
assert!(random_process);
} else {
panic!("wrong workload type found");
}
}

#[test]
fn test_endpoints_zipf() {
let input = r#"
restart_interval = 10
[workload]
type = "endpoints"
distribution = "zipf"
n_ports = 200
exponent = 1.4
"#;

let config = Config::builder()
.add_source(File::from_str(input, FileFormat::Toml))
.build()
.expect("failed to parse configuration")
.try_deserialize::<WorkloadConfig>()
.expect("failed to deserialize into WorkloadConfig");

let WorkloadConfig {
restart_interval,
workload,
} = config;
assert_eq!(restart_interval, 10);

if let Workload::Endpoints { distribution, .. } = workload {
if let Distribution::Zipfian { n_ports, exponent } = distribution {
assert_eq!(n_ports, 200);
assert_eq!(exponent, 1.4);
} else {
panic!("wrong distribution type found");
}
} else {
panic!("wrong workload type found");
}
}

#[test]
fn test_endpoints_uniform() {
let input = r#"
restart_interval = 10
[workload]
type = "endpoints"
distribution = "uniform"
upper = 100
lower = 1
"#;

let config = Config::builder()
.add_source(File::from_str(input, FileFormat::Toml))
.build()
.expect("failed to parse configuration")
.try_deserialize::<WorkloadConfig>()
.expect("failed to deserialize into WorkloadConfig");

let WorkloadConfig {
restart_interval,
workload,
} = config;
assert_eq!(restart_interval, 10);

if let Workload::Endpoints { distribution } = workload {
if let Distribution::Uniform { lower, upper } = distribution {
assert_eq!(lower, 1);
assert_eq!(upper, 100);
} else {
panic!("wrong distribution type found");
}
} else {
panic!("wrong workload type found");
}
}

#[test]
fn test_syscalls() {
let input = r#"
restart_interval = 10
[workload]
type = "syscalls"
arrival_rate = 10.0
"#;

let config = Config::builder()
.add_source(File::from_str(input, FileFormat::Toml))
.build()
.expect("failed to parse configuration")
.try_deserialize::<WorkloadConfig>()
.expect("failed to deserialize into WorkloadConfig");

let WorkloadConfig {
restart_interval,
workload,
} = config;
assert_eq!(restart_interval, 10);
if let Workload::Syscalls { arrival_rate } = workload {
assert_eq!(arrival_rate, 10.0);
} else {
panic!("wrong workload type found");
}
}
}
Loading

0 comments on commit 571f8b2

Please sign in to comment.