Skip to content

Miscellaneous improvements to make the project more idiomatic #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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