Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tasks): add --timings option for run command #1536

Merged
merged 1 commit into from
Jan 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 37 additions & 4 deletions src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ pub struct Run {
#[clap(long, short, verbatim_doc_comment)]
pub raw: bool,

/// Shows elapsed time after each task
#[clap(long, alias = "timing", verbatim_doc_comment)]
pub timings: bool,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after looking at this I am considering if maybe we just want to always display the timings. Not sure though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i feel in either case it should interfere less with the actual command output by maybe showing the entire line (after prefix) dimmed?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still undecided on this. I think I'm leaning towards always displaying though curious if you all have any thoughts.

Copy link
Owner

@jdx jdx Jan 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm maybe not. Let's keep it optional for now, I think we should err on the side of less noise.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the other choice is optional, but default to enabled. Seems reasonable however to just take user feedback and make a final decision before tasks are a GA feature.


#[clap(skip)]
pub is_linear: bool,
}
Expand Down Expand Up @@ -166,13 +170,17 @@ impl Run {
env.insert("FORCE_COLOR".into(), "1".into());
}

let tasks = Mutex::new(Deps::new(config, tasks)?);
self.is_linear = tasks.lock().unwrap().is_linear();

for task in tasks.lock().unwrap().all() {
let tasks = Deps::new(config, tasks)?;
for task in tasks.all() {
self.validate_task(task)?;
}

let num_tasks = tasks.all().count();
self.is_linear = tasks.is_linear();

let tasks = Mutex::new(tasks);
let timer = std::time::Instant::now();

let pool = rayon::ThreadPoolBuilder::new()
.num_threads(self.jobs() + 1)
.build()?;
Expand All @@ -195,6 +203,12 @@ impl Run {
run(&task);
}
});

if self.timings && num_tasks > 1 {
let msg = format!("finished in {}", format_duration(timer.elapsed()));
info!("{}", style::edim(msg));
};

Ok(())
}

Expand All @@ -211,6 +225,8 @@ impl Run {
.map(|(k, v)| (k.clone(), v.clone()))
.collect();

let timer = std::time::Instant::now();

if let Some(file) = &task.file {
self.exec_file(file, task, &env, &prefix)?;
} else {
Expand All @@ -223,6 +239,14 @@ impl Run {
}
}

if self.timings {
miseprintln!(
"{} finished in {}",
prefix,
format_duration(timer.elapsed())
);
}

self.save_checksum(task)?;

Ok(())
Expand Down Expand Up @@ -531,6 +555,15 @@ fn get_color() -> Color {
static COLOR_IDX: AtomicUsize = AtomicUsize::new(0);
COLORS[COLOR_IDX.fetch_add(1, Ordering::Relaxed) % COLORS.len()]
}

fn format_duration(dur: std::time::Duration) -> String {
if dur < std::time::Duration::from_secs(1) {
format!("{:.0?}", dur)
} else {
format!("{:.2?}", dur)
}
}

#[cfg(test)]
mod tests {
use crate::file;
Expand Down
Loading