Change formatting for durations (#3198)

This commit is contained in:
WorksButNotTested 2025-05-06 16:24:30 +01:00 committed by GitHub
parent feedca9de8
commit 3b6a350b24
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 37 additions and 13 deletions

View File

@ -6,7 +6,7 @@ use alloc::{borrow::Cow, string::String};
use core::time::Duration; use core::time::Duration;
use hashbrown::HashMap; use hashbrown::HashMap;
use libafl_bolts::{ClientId, Error, current_time, format_duration_hms}; use libafl_bolts::{ClientId, Error, current_time, format_duration};
#[cfg(feature = "std")] #[cfg(feature = "std")]
use serde_json::Value; use serde_json::Value;
@ -171,7 +171,7 @@ impl ClientStatsManager {
// Time-related data are always re-computed, since it is related with current time. // Time-related data are always re-computed, since it is related with current time.
let cur_time = current_time(); let cur_time = current_time();
global_stats.run_time = cur_time - self.start_time; global_stats.run_time = cur_time - self.start_time;
global_stats.run_time_pretty = format_duration_hms(&global_stats.run_time); global_stats.run_time_pretty = format_duration(&global_stats.run_time);
global_stats.execs_per_sec = self global_stats.execs_per_sec = self
.client_stats .client_stats
.iter_mut() .iter_mut()

View File

@ -26,7 +26,7 @@ use crossterm::{
terminal::{EnterAlternateScreen, LeaveAlternateScreen, disable_raw_mode, enable_raw_mode}, terminal::{EnterAlternateScreen, LeaveAlternateScreen, disable_raw_mode, enable_raw_mode},
}; };
use hashbrown::HashMap; use hashbrown::HashMap;
use libafl_bolts::{ClientId, Error, current_time, format_big_number, format_duration_hms}; use libafl_bolts::{ClientId, Error, current_time, format_big_number, format_duration};
use ratatui::{Terminal, backend::CrosstermBackend}; use ratatui::{Terminal, backend::CrosstermBackend};
use typed_builder::TypedBuilder; use typed_builder::TypedBuilder;

View File

@ -17,7 +17,7 @@ use ratatui::{
use super::{ use super::{
Duration, ItemGeometry, ProcessTiming, String, TimedStats, TuiContext, current_time, Duration, ItemGeometry, ProcessTiming, String, TimedStats, TuiContext, current_time,
format_duration_hms, format_duration,
}; };
#[derive(Default, Debug)] #[derive(Default, Debug)]
@ -318,9 +318,9 @@ impl TuiUi {
} }
let start = stats.series.front().unwrap().time; let start = stats.series.front().unwrap().time;
let end = stats.series.back().unwrap().time; let end = stats.series.back().unwrap().time;
let min_lbl_x = format_duration_hms(&start); let min_lbl_x = format_duration(&start);
let med_lbl_x = format_duration_hms(&((end - start) / 2)); let med_lbl_x = format_duration(&((end - start) / 2));
let max_lbl_x = format_duration_hms(&end); let max_lbl_x = format_duration(&end);
let x_labels = vec![ let x_labels = vec![
Span::styled(min_lbl_x, Style::default().add_modifier(Modifier::BOLD)), Span::styled(min_lbl_x, Style::default().add_modifier(Modifier::BOLD)),
@ -538,7 +538,7 @@ impl TuiUi {
let items = vec![ let items = vec![
Row::new(vec![ Row::new(vec![
Cell::from(Span::raw("run time")), Cell::from(Span::raw("run time")),
Cell::from(Span::raw(format_duration_hms(&(current_time() - tup.0)))), Cell::from(Span::raw(format_duration(&(current_time() - tup.0)))),
]), ]),
Row::new(vec![ Row::new(vec![
Cell::from(Span::raw("exec speed")), Cell::from(Span::raw("exec speed")),
@ -550,11 +550,11 @@ impl TuiUi {
]), ]),
Row::new(vec![ Row::new(vec![
Cell::from(Span::raw("last new entry")), Cell::from(Span::raw("last new entry")),
Cell::from(Span::raw(format_duration_hms(&(tup.1.last_new_entry)))), Cell::from(Span::raw(format_duration(&(tup.1.last_new_entry)))),
]), ]),
Row::new(vec![ Row::new(vec![
Cell::from(Span::raw("last solution")), Cell::from(Span::raw("last solution")),
Cell::from(Span::raw(format_duration_hms(&(tup.1.last_saved_solution)))), Cell::from(Span::raw(format_duration(&(tup.1.last_saved_solution)))),
]), ]),
]; ];

View File

@ -952,9 +952,33 @@ pub fn current_milliseconds() -> u64 {
/// Format a `Duration` into a HMS string /// Format a `Duration` into a HMS string
#[cfg(feature = "alloc")] #[cfg(feature = "alloc")]
#[must_use] #[must_use]
pub fn format_duration_hms(duration: &time::Duration) -> String { pub fn format_duration(duration: &time::Duration) -> String {
let secs = duration.as_secs(); const MINS_PER_HOUR: u64 = 60;
format!("{}h-{}m-{}s", (secs / 60) / 60, (secs / 60) % 60, secs % 60) const HOURS_PER_DAY: u64 = 24;
const SECS_PER_MINUTE: u64 = 60;
const SECS_PER_HOUR: u64 = SECS_PER_MINUTE * MINS_PER_HOUR;
const SECS_PER_DAY: u64 = SECS_PER_HOUR * HOURS_PER_DAY;
let total_secs = duration.as_secs();
let secs = total_secs % SECS_PER_MINUTE;
if total_secs < SECS_PER_MINUTE {
format!("{secs}s")
} else {
let mins = (total_secs / SECS_PER_MINUTE) % MINS_PER_HOUR;
if total_secs < SECS_PER_HOUR {
format!("{mins}m-{secs}s")
} else {
let hours = (total_secs / SECS_PER_HOUR) % HOURS_PER_DAY;
if total_secs < SECS_PER_DAY {
format!("{hours}h-{mins}m-{secs}s")
} else {
let days = total_secs / SECS_PER_DAY;
format!("{days}days {hours}h-{mins}m-{secs}s")
}
}
}
} }
/// Format a number with thousands separators /// Format a number with thousands separators