Skip to content

Commit

Permalink
Add ctx.elapsed_secs() function
Browse files Browse the repository at this point in the history
Fixes pkolaczk#51

(cherry picked from commit 9c70af0)
  • Loading branch information
pkolaczk authored and vponomaryov committed Oct 29, 2024
1 parent 3059d4d commit cae8e19
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 9 additions & 1 deletion src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Instant>,
// NOTE: 'session' is defined as optional for being able to test methods
// which don't 'depend on'/'use' the 'session' object.
session: Option<Arc<scylla::Session>>,
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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()),
})
}

Expand Down Expand Up @@ -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();
Expand All @@ -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();
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/workload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<CassError>().unwrap();
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit cae8e19

Please sign in to comment.