Skip to content

Commit

Permalink
Expose benchmarker timer.
Browse files Browse the repository at this point in the history
  • Loading branch information
FiveMovesAhead committed May 12, 2024
1 parent 1b9e310 commit 8fe4354
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
42 changes: 35 additions & 7 deletions tig-benchmarker/src/benchmarker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,28 @@ pub struct QueryData {
}

#[derive(Serialize, Clone, Debug)]
pub struct Duration {
pub struct Timer {
pub start: u64,
pub end: u64,
pub now: u64,
}
impl Timer {
fn new(ms: u64) -> Self {
let now = time();
Timer {
start: now,
end: now + ms,
now,
}
}
fn update(&mut self) -> &Self {
self.now = time();
self
}
fn finished(&self) -> bool {
self.now >= self.end
}
}

#[derive(Serialize, Debug, Clone)]
pub struct Job {
Expand Down Expand Up @@ -101,6 +118,7 @@ pub enum Status {
#[derive(Serialize, Debug, Clone)]
pub struct State {
pub status: Status,
pub time_left: Option<Timer>,
pub query_data: QueryData,
pub selected_algorithms: HashMap<String, String>,
pub job: Option<Job>,
Expand Down Expand Up @@ -138,7 +156,7 @@ async fn update_status(status: &str) {
}
}

async fn run_once(num_workers: u32) -> Result<()> {
async fn run_once(num_workers: u32, ms_per_benchmark: u32) -> Result<()> {
update_status("Querying latest data").await;
// retain only benchmarks that are within the lifespan period
// preserves solution_meta_data and solution_data
Expand Down Expand Up @@ -217,9 +235,12 @@ async fn run_once(num_workers: u32) -> Result<()> {
solutions_data.clone(),
)
.await;
let start = time();
loop {
{
let mut state = state().lock().await;
let now = time();
(*state).time_left = Some(Timer::new(ms_per_benchmark as u64));
}
loop {
{
// transfers solutions computed by workers to benchmark state
let num_solutions =
Expand All @@ -231,7 +252,13 @@ async fn run_once(num_workers: u32) -> Result<()> {
nonce_iter.attempts()
))
.await;
if now - start >= 15000 || nonce_iter.is_empty() {
let State {
status, time_left, ..
} = &mut (*state().lock().await);
if time_left.as_mut().unwrap().update().finished()
|| nonce_iter.is_empty()
|| *status == Status::Stopping
{
break;
}
}
Expand Down Expand Up @@ -319,7 +346,7 @@ async fn drain_solutions(
to_update.extend((*solutions_data).drain(..));
to_update.len() as u32
}
pub async fn start(num_workers: u32) {
pub async fn start(num_workers: u32, ms_per_benchmark: u32) {
{
let mut state = (*state()).lock().await;
if state.status != Status::Stopped {
Expand All @@ -339,7 +366,7 @@ pub async fn start(num_workers: u32) {
state.status = Status::Stopped;
}
}
if let Err(e) = run_once(num_workers).await {
if let Err(e) = run_once(num_workers, ms_per_benchmark).await {
update_status(&format!("Error: {:?}", e)).await;
sleep(5000).await;
}
Expand Down Expand Up @@ -368,6 +395,7 @@ pub async fn setup(api_url: String, api_key: String, player_id: String) {
STATE.get_or_init(|| {
Mutex::new(State {
status: Status::Stopped,
time_left: None,
query_data,
selected_algorithms: HashMap::new(),
job: None,
Expand Down
4 changes: 2 additions & 2 deletions tig-benchmarker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ mod exports {
}

#[wasm_bindgen]
pub async fn start(num_workers: u32) {
benchmarker::start(num_workers).await;
pub async fn start(num_workers: u32, ms_per_benchmark: u32) {
benchmarker::start(num_workers, ms_per_benchmark).await;
}

#[wasm_bindgen]
Expand Down

0 comments on commit 8fe4354

Please sign in to comment.