Skip to content
Open
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
20 changes: 20 additions & 0 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 @@ -36,6 +36,7 @@ documented = "0.9.2"
dirs = "6.0.0"
edit = "0.1.5"
erased-serde = "0.4.10"
context_manager = "0.1.3"

[dev-dependencies]
assert_cmd = "2.2.2"
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub mod region;
pub mod settings;
pub mod simulation;
pub mod time_slice;
pub mod timeit;
pub mod units;

#[cfg(test)]
Expand Down
3 changes: 3 additions & 0 deletions src/simulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ use crate::model::Model;
use crate::output::DataWriter;
use crate::process::ProcessMap;
use crate::simulation::prices::{Prices, calculate_prices};
use crate::timeit::TimingContext;
use crate::units::Capacity;
use anyhow::{Context, Result};
use context_manager;
use log::info;
use std::path::Path;
use std::rc::Rc;
Expand Down Expand Up @@ -158,6 +160,7 @@ pub fn run(model: &Model, output_path: &Path, debug_model: bool) -> Result<()> {
}

// Run dispatch to get flows and prices for a milestone year
#[context_manager::wrap(TimingContext)]
fn run_dispatch_for_year(
model: &Model,
assets: &[AssetRef],
Expand Down
3 changes: 3 additions & 0 deletions src/simulation/investment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ use crate::output::DataWriter;
use crate::region::RegionID;
use crate::simulation::prices::Prices;
use crate::time_slice::{TimeSliceID, TimeSliceInfo, TimeSliceLevel, TimeSliceSelection};
use crate::timeit::TimingContext;
use crate::units::{ActivityPerCapacity, Capacity, Dimensionless, Flow, FlowPerCapacity};
use anyhow::{Result, ensure};
use context_manager;
use indexmap::IndexMap;
use itertools::Itertools;
use log::{debug, warn};
Expand Down Expand Up @@ -43,6 +45,7 @@ pub type AllDemandMap = IndexMap<(CommodityID, RegionID, TimeSliceID), Flow>;
///
/// The assets selected (including retained commissioned assets) for the given planning `year` or an
/// error.
#[context_manager::wrap(TimingContext)]
pub fn perform_agent_investment(
model: &Model,
year: u32,
Expand Down
26 changes: 26 additions & 0 deletions src/timeit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//! Implement a context manager for timing the decorated function
use context_manager::{CallerContext, SyncWrapContext};
use log::info;
use std::time::Instant;

/// Context for time a function call
pub struct TimingContext {
start: Instant,
}

impl<T> SyncWrapContext<T> for TimingContext {
fn new() -> Self {
Self {
start: Instant::now(),
}
}

fn after(self, caller_context: &CallerContext, _result: &T) {
let elapsed = self.start.elapsed();
info!(
"[Timer] '{}' completed in {:.3}ms",
caller_context.fn_name(),
elapsed.as_secs_f64() * 1000.0,
);
}
}
Loading