Skip to content

Commit

Permalink
Made basic-infection and births-deaths into libraries so they could s…
Browse files Browse the repository at this point in the history
…hare code with the benchmarks.
  • Loading branch information
RobertJacobsonCDC committed Feb 3, 2025
1 parent 4963e92 commit 6f86acc
Show file tree
Hide file tree
Showing 33 changed files with 167 additions and 106 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ profile.json
# Generated by Cargo
# will have compiled files and executables
debug/
target/
**/target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Expand Down
10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ rand_distr = "^0.4.3"
tempfile = "^3.15.0"
ordered-float = "^4.6.0"
assert_cmd = "^2.0.16"
criterion = "^0.5.1"

# Example Libraries
ixa_example_basic_infection = { path = "examples/basic-infection" }
ixa_example_births_deaths = { path = "examples/births-deaths" }

[lints.clippy]
pedantic = { level = "warn", priority = -1 }
Expand All @@ -47,6 +52,11 @@ path = "tests/bin/runner_test_custom_args.rs"
name = "runner_test_debug"
path = "tests/bin/runner_test_debug.rs"

[[bench]]
name = "example_basic_infection"
path = "benches/example_basic_infection/example_basic_infection.rs"
harness = false

[[bench]]
name = "example_births_deaths"
path = "benches/example_births_deaths/example_births_deaths.rs"
Expand Down
20 changes: 13 additions & 7 deletions benches/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ For general Rust profiling information, see: https://nnethercote.github.io/perf-

# Generating Flamegraphs

You can use `samply` (Linux and macOS) to capture stack samples from your application and generate a flamegraph, helping you quickly identify and analyze performance hotspots.
You can use `samply` (Linux and macOS) to capture stack samples from your application and generate
a flamegraph, helping you quickly identify and analyze performance hotspots.

## Prerequisites

Expand Down Expand Up @@ -36,10 +37,10 @@ cargo build --example basic-infection --release && samply record -- target/relea

When it completes, `samply` will automatically open a browser with the generated report.


## `flamegraph` Alternative

You can use `flamegraph` if you prefer. It requires root privileges, but don't use `sudo cargo...`. Do this:
You can use `flamegraph` if you prefer. It requires root privileges, but don't use
`sudo cargo...`. Do this:

```bash
cargo flamegraph --root --example basic-infection
Expand All @@ -49,12 +50,16 @@ This will generate an SVG of the flamegraph in the current directory.

# Benchmarking Ixa

Ixa uses [Criterion.rs](https://bheisler.github.io/criterion.rs/book/index.html) for statistical benchmarking.
Ixa uses [Criterion.rs](https://bheisler.github.io/criterion.rs/book/index.html) for statistical
benchmarking.

## Optional Prerequisites

- [`gnuplot`](http://www.gnuplot.info/): The [plotters crate](https://github.com/38/plotters) will be used as a fallback if `gnuplot` is not found.
- [cargo-criterion](https://bheisler.github.io/criterion.rs/book/cargo_criterion/cargo_criterion.html): This is the upcoming "next generation" of Criterion.rs. Eventually it will reduce compilation times and offer more features, but for now it only has feature parity.
- [`gnuplot`](http://www.gnuplot.info/): The [plotters crate](https://github.com/38/plotters) will
be used as a fallback if `gnuplot` is not found.
- [cargo-criterion](https://bheisler.github.io/criterion.rs/book/cargo_criterion/cargo_criterion.html):
This is the upcoming "next generation" of Criterion.rs. Eventually it will reduce compilation
times and offer more features, but for now it only has feature parity.

```bash
cargo install cargo-criterion
Expand Down Expand Up @@ -110,4 +115,5 @@ An HTML report is created at `target/criterion/report/index.html`. On macOS:
open target/criterion/report/index.html
```

On Linux platforms, replace `open` with `xdg-open`, `gnome-open`, or `kde-open`, depending on your system configuration, or just open the file in a browser.
On Linux platforms, replace `open` with `xdg-open`, `gnome-open`, or `kde-open`, depending on your
system configuration, or just open the file in a browser.
7 changes: 3 additions & 4 deletions benches/example_basic_infection/example_basic_infection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ use criterion::{criterion_group, criterion_main, Criterion};
use ixa::context::Context;
use ixa::random::ContextRandomExt;

mod incidence_report;
mod infection_manager;
mod people;
mod transmission_manager;
use ixa_example_basic_infection::{
incidence_report, infection_manager, people, transmission_manager,
};

static POPULATION: u64 = 1000;
static SEED: u64 = 123;
Expand Down
1 change: 0 additions & 1 deletion benches/example_basic_infection/incidence_report.rs

This file was deleted.

1 change: 0 additions & 1 deletion benches/example_basic_infection/infection_manager.rs

This file was deleted.

1 change: 0 additions & 1 deletion benches/example_basic_infection/people.rs

This file was deleted.

1 change: 0 additions & 1 deletion benches/example_basic_infection/transmission_manager.rs

This file was deleted.

1 change: 0 additions & 1 deletion benches/example_births_deaths/demographics_report.rs

This file was deleted.

13 changes: 5 additions & 8 deletions benches/example_births_deaths/example_births_deaths.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
use crate::parameters_loader::Parameters;
use criterion::{criterion_group, criterion_main, Criterion};
use ixa::{Context, ContextGlobalPropertiesExt, ContextRandomExt};
use std::path::Path;

mod demographics_report;
mod incidence_report;
mod infection_manager;
mod parameters_loader;
mod population_manager;
mod transmission_manager;
use ixa_example_births_deaths::{
demographics_report, incidence_report, infection_manager, parameters_loader,
population_manager, transmission_manager,
};

fn births_deaths() -> Context {
let mut context = Context::new();
Expand All @@ -19,7 +16,7 @@ fn births_deaths() -> Context {
.expect("failed to load parameters");

let parameters = context
.get_global_property_value(Parameters)
.get_global_property_value(parameters_loader::Parameters)
.unwrap()
.clone();
context.init_random(parameters.seed);
Expand Down
1 change: 0 additions & 1 deletion benches/example_births_deaths/incidence_report.rs

This file was deleted.

1 change: 0 additions & 1 deletion benches/example_births_deaths/infection_manager.rs

This file was deleted.

1 change: 0 additions & 1 deletion benches/example_births_deaths/input.json

This file was deleted.

15 changes: 15 additions & 0 deletions benches/example_births_deaths/input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"population": 100,
"max_time": 780.0,
"seed": 123,
"birth_rate": 0.1,
"death_rate": 0.1,
"foi_groups": [
{"group_name": "NewBorn", "foi": 0.2},
{"group_name": "General", "foi": 0.1},
{"group_name": "OldAdult", "foi": 0.15}
],
"infection_duration": 5.0,
"output_file": "incidence",
"demographic_output_file": "people_report.csv"
}
1 change: 0 additions & 1 deletion benches/example_births_deaths/parameters_loader.rs

This file was deleted.

1 change: 0 additions & 1 deletion benches/example_births_deaths/population_manager.rs

This file was deleted.

1 change: 0 additions & 1 deletion benches/example_births_deaths/transmission_manager.rs

This file was deleted.

2 changes: 1 addition & 1 deletion criterion.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
# This is used to configure the plotting backend used by cargo-criterion.
# Options are "gnuplot" and "plotters", or "auto", which will use gnuplot if it's
# available or plotters if it isn't.
ploting_backend = "auto"
plotting_backend = "auto"

# The colors table allows users to configure the colors used by the charts
# cargo-criterion generates.
Expand Down
21 changes: 21 additions & 0 deletions examples/basic-infection/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "ixa_example_basic_infection"
version = "0.1.0"
edition = "2021"

publish = false

[dependencies]

ixa = { path = "../../../ixa" }
ixa-derive = { path = "../../ixa-derive" }

csv = "^1.3.1"
serde = "^1.0.217"
rand = "^0.8.5"
rand_distr = "^0.4.3"
paste = "^1.0.15"

[[bin]]
name = "basic_infection"
path = "main.rs"
31 changes: 1 addition & 30 deletions examples/basic-infection/main.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,4 @@
use ixa::context::Context;
use ixa::error::IxaError;
use ixa::random::ContextRandomExt;

mod incidence_report;
mod infection_manager;
mod people;
mod transmission_manager;

static POPULATION: u64 = 1000;
static SEED: u64 = 123;
static MAX_TIME: f64 = 303.0;
static FOI: f64 = 0.1;
static INFECTION_DURATION: f64 = 5.0;

fn initialize() -> Result<Context, IxaError> {
let mut context = Context::new();

context.init_random(SEED);

people::init(&mut context);
transmission_manager::init(&mut context);
infection_manager::init(&mut context);
incidence_report::init(&mut context)?;

context.add_plan(MAX_TIME, |context| {
context.shutdown();
});
Ok(context)
}
use ixa_example_basic_infection::initialize;

fn main() {
let mut context = initialize().expect("Error adding report.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn init(context: &mut Context) -> Result<(), IxaError> {
// examples.
context
.report_options()
.directory(PathBuf::from("./examples/basic-infection/"))
.directory(PathBuf::from("../"))
.overwrite(true);
context.add_report::<IncidenceReportItem>("incidence")?;
context.subscribe_to_event::<InfectionStatusEvent>(handle_infection_status_change);
Expand Down
30 changes: 30 additions & 0 deletions examples/basic-infection/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use ixa::context::Context;
use ixa::error::IxaError;
use ixa::random::ContextRandomExt;

pub mod incidence_report;
pub mod infection_manager;
pub mod people;
pub mod transmission_manager;

static POPULATION: u64 = 1000;
static SEED: u64 = 123;
static MAX_TIME: f64 = 303.0;
static FOI: f64 = 0.1;
static INFECTION_DURATION: f64 = 5.0;

pub fn initialize() -> Result<Context, IxaError> {
let mut context = Context::new();

context.init_random(SEED);

people::init(&mut context);
transmission_manager::init(&mut context);
infection_manager::init(&mut context);
incidence_report::init(&mut context)?;

context.add_plan(MAX_TIME, |context| {
context.shutdown();
});
Ok(context)
}
File renamed without changes.
22 changes: 22 additions & 0 deletions examples/births-deaths/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
name = "ixa_example_births_deaths"
version = "0.1.0"
edition = "2021"

publish = false

[dependencies]

ixa = { path = "../../../ixa" }
ixa-derive = { path = "../../ixa-derive" }

csv = "^1.3.1"
ctor = "^0.2.8"
serde = "^1.0.217"
rand = "^0.8.5"
rand_distr = "^0.4.3"
paste = "^1.0.15"

[[bin]]
name = "births_deaths"
path = "main.rs"
42 changes: 1 addition & 41 deletions examples/births-deaths/main.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,6 @@
use ixa::error::IxaError;
use ixa::random::ContextRandomExt;
use ixa::{context::Context, global_properties::ContextGlobalPropertiesExt};
use std::path::Path;

mod demographics_report;
mod incidence_report;
mod infection_manager;
mod parameters_loader;
mod population_manager;
mod transmission_manager;

use crate::parameters_loader::Parameters;

fn initialize() -> Result<Context, IxaError> {
let mut context = Context::new();
let current_dir = Path::new(file!()).parent().unwrap();
let file_path = current_dir.join("input.json");

parameters_loader::init_parameters(&mut context, &file_path)?;

let parameters = context
.get_global_property_value(Parameters)
.unwrap()
.clone();
context.init_random(parameters.seed);

demographics_report::init(&mut context)?;
incidence_report::init(&mut context)?;

population_manager::init(&mut context);
transmission_manager::init(&mut context);
infection_manager::init(&mut context);

context.add_plan(parameters.max_time, |context| {
context.shutdown();
});
println!("{parameters:?}");
Ok(context)
}
use ixa_example_births_deaths::initialize;

fn main() {
let mut context = initialize().expect("Could not initialize context.");

context.execute();
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use ixa::{
};
use serde::{Deserialize, Serialize};
use std::path::Path;
use std::path::PathBuf;

#[derive(Serialize, Deserialize, Clone)]
struct PersonReportItem {
Expand Down Expand Up @@ -72,9 +71,10 @@ pub fn init(context: &mut Context) -> Result<(), IxaError> {
.clone();

let current_dir = Path::new(file!()).parent().unwrap();
let current_dir = current_dir.join(Path::new(".."));
context
.report_options()
.directory(PathBuf::from(current_dir))
.directory(current_dir)
.overwrite(true); // Not recommended for production. See `basic-infection/incidence-report`.

context.add_report::<PersonReportItem>(&parameters.demographic_output_file)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub fn init(context: &mut Context) -> Result<(), IxaError> {
.unwrap()
.clone();
let current_dir = Path::new(file!()).parent().unwrap();
let current_dir = current_dir.join(Path::new(".."));
context
.report_options()
.directory(PathBuf::from(current_dir))
Expand Down
File renamed without changes.
Loading

0 comments on commit 6f86acc

Please sign in to comment.