diff --git a/README.md b/README.md index 114b85e..11d8405 100644 --- a/README.md +++ b/README.md @@ -330,3 +330,7 @@ Errors during execution of a workload script are divided into three classes: errors terminate the benchmark immediately. Overload errors (e.g. timeouts) that happen during the main run phase are counted and reported in the benchmark report. + +### Other functions + +- `ctx.elapsed_secs()` – returns the number of seconds elapsed since starting the workload, as float diff --git a/src/context.rs b/src/context.rs index 2faa5d6..89a5ca3 100644 --- a/src/context.rs +++ b/src/context.rs @@ -603,6 +603,7 @@ impl RowDistributionPreset { /// It also tracks query execution metrics such as number of requests, rows, response times etc. #[derive(Any)] pub struct Context { + start_time: TryLock, // NOTE: 'session' is defined as optional for being able to test methods // which don't 'depend on'/'use' the 'session' object. session: Option>, @@ -638,6 +639,7 @@ impl Context { retry_interval: RetryInterval, ) -> Context { Context { + start_time: TryLock::new(Instant::now()), session: session.map(Arc::new), page_size, statements: HashMap::new(), @@ -669,6 +671,7 @@ impl Context { load_cycle_count: self.load_cycle_count, preferred_datacenter: self.preferred_datacenter.clone(), data: deserialized, + start_time: TryLock::new(*self.start_time.try_lock().unwrap()), }) } @@ -1037,6 +1040,10 @@ impl Context { } } + pub fn elapsed_secs(&self) -> f64 { + self.start_time.try_lock().unwrap().elapsed().as_secs_f64() + } + /// Returns the current accumulated request stats snapshot and resets the stats. pub fn take_session_stats(&self) -> SessionStats { let mut stats = self.stats.try_lock().unwrap(); @@ -1046,8 +1053,9 @@ impl Context { } /// Resets query and request counters - pub fn reset_session_stats(&self) { + pub fn reset(&self) { self.stats.try_lock().unwrap().reset(); + *self.start_time.try_lock().unwrap() = Instant::now(); } } diff --git a/src/workload.rs b/src/workload.rs index 6bd334e..8152f2f 100644 --- a/src/workload.rs +++ b/src/workload.rs @@ -119,6 +119,9 @@ impl Program { context_module .async_inst_fn("execute_prepared", Context::execute_prepared) .unwrap(); + context_module + .inst_fn("elapsed_secs", Context::elapsed_secs) + .unwrap(); let mut err_module = Module::default(); err_module.ty::().unwrap(); @@ -504,7 +507,7 @@ impl Workload { let mut state = self.state.try_lock().unwrap(); state.fn_stats = FnStats::default(); state.start_time = start_time; - self.context.reset_session_stats(); + self.context.reset(); } /// Returns statistics of the operations invoked by this workload so far.