Skip to content
Merged
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
25 changes: 23 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ impl<'a, T> ProfilerBuilder<'a, T> {
let _active_profile = ACTIVE_PROFILE
.try_lock()
.expect("only one profiler can be active at a time");
// Elapsed time and fuel consumption restart at zero for each session.
*LAST_WEIGHT.lock().unwrap() = 0;
let ticker = ticker::Ticker::new(self.frequency, self.store.engine().clone()).unwrap();
unsafe {
TICKER = Some(ticker);
Expand Down Expand Up @@ -161,11 +163,14 @@ fn unmangle_name(name: &str) -> String {

#[cfg(test)]
mod tests {
use std::{thread, time::Duration};
use std::{sync::Mutex, thread, time::Duration};

use wasmtime::{Config, Engine, Store};

use super::{unmangle_name, ProfilerBuilder};
use super::{unmangle_name, ProfilerBuilder, LAST_WEIGHT};

// Tests that start profiles must not overlap with each other.
static PROFILE_TEST_LOCK: Mutex<()> = Mutex::new(());

#[test]
fn unmangles_rust_symbols() {
Expand Down Expand Up @@ -193,8 +198,24 @@ mod tests {
assert_eq!(unmangle_name("_Z"), "_Z");
}

#[test]
fn resets_weight_baseline_between_profiles() {
let _test = PROFILE_TEST_LOCK.lock().unwrap();
let engine = Engine::default();
let mut store = Store::new(&engine, ());

ProfilerBuilder::new(&mut store).profile(|_| {
*LAST_WEIGHT.lock().unwrap() = 100;
});
ProfilerBuilder::new(&mut store).profile(|_| {
let last_weight = *LAST_WEIGHT.lock().unwrap();
assert_eq!(last_weight, 0);
});
}

#[test]
fn rejects_concurrent_profile_calls() {
let _test = PROFILE_TEST_LOCK.lock().unwrap();
let mut config = Config::new();
config.epoch_interruption(true);
let engine = Engine::new(&config).unwrap();
Expand Down
Loading