Skip to content

Commit

Permalink
Add ctx.elapsed_secs() function
Browse files Browse the repository at this point in the history
  • Loading branch information
pkolaczk committed Aug 3, 2024
1 parent 7e1dae9 commit 2e6d2ef
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,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
14 changes: 10 additions & 4 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ pub fn get_exponential_retry_interval(
/// It also tracks query execution metrics such as number of requests, rows, response times etc.
#[derive(Any)]
pub struct Context {
start_time: TryLock<Instant>,
session: Arc<scylla::Session>,
statements: HashMap<String, Arc<PreparedStatement>>,
stats: TryLock<SessionStats>,
Expand All @@ -412,6 +413,7 @@ unsafe impl Sync for Context {}
impl Context {
pub fn new(session: scylla::Session, retry_number: u64, retry_interval: RetryDelay) -> Context {
Context {
start_time: TryLock::new(Instant::now()),
session: Arc::new(session),
statements: HashMap::new(),
stats: TryLock::new(SessionStats::new()),
Expand All @@ -433,10 +435,9 @@ impl Context {
session: self.session.clone(),
statements: self.statements.clone(),
stats: TryLock::new(SessionStats::default()),
retry_number: self.retry_number,
retry_interval: self.retry_interval,
load_cycle_count: self.load_cycle_count,
data: deserialized,
start_time: TryLock::new(*self.start_time.try_lock().unwrap()),
..*self
})
}

Expand Down Expand Up @@ -532,6 +533,10 @@ impl Context {
rs
}

pub fn elapsed_secs(&self) -> f64 {
self.start_time.try_lock().unwrap().elapsed().as_secs_f64()
}

fn should_retry<R>(result: &Result<R, QueryError>) -> bool {
matches!(
result,
Expand All @@ -555,8 +560,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 @@ -110,6 +110,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 @@ -495,7 +498,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 2e6d2ef

Please sign in to comment.