Skip to content

Commit 7bc759c

Browse files
committed
Avoid cluttering tree with stdout/stderr log files
Write output log files to a tmpfile location and remove them on success runs by default. On failure or when using the `-k` option to keep log files, output the path to the logs after the benhmark run.
1 parent c35548e commit 7bc759c

File tree

2 files changed

+85
-27
lines changed

2 files changed

+85
-27
lines changed

crates/cli/src/benchmark.rs

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ pub struct BenchmarkCommand {
125125
/// `cpu_affinity` in the `sightglass-recorder` crate for more information.
126126
#[structopt(long)]
127127
pin: bool,
128+
129+
/// Keep log files after successful benchmark runs. By default, logs are
130+
/// only kept on failures.
131+
#[structopt(short = "k", long = "keep-logs")]
132+
keep_logs: bool,
128133
}
129134

130135
impl BenchmarkCommand {
@@ -190,10 +195,15 @@ impl BenchmarkCommand {
190195
wasm_file.hash(&mut hasher);
191196
hasher.finish()
192197
};
193-
let stdout = format!("stdout-{:x}-{}.log", wasm_hash, std::process::id());
194-
let stdout = Path::new(&stdout);
195-
let stderr = format!("stderr-{:x}-{}.log", wasm_hash, std::process::id());
196-
let stderr = Path::new(&stderr);
198+
199+
// Create log files in temp directory
200+
let log_dir = std::env::temp_dir().join("sightglass-logs");
201+
std::fs::create_dir_all(&log_dir).context("Failed to create log directory")?;
202+
203+
let stdout =
204+
log_dir.join(format!("stdout-{wasm_hash:x}-{}.log", std::process::id()));
205+
let stderr =
206+
log_dir.join(format!("stderr-{wasm_hash:x}-{}.log", std::process::id()));
197207
let stdin = None;
198208

199209
let mut measurements = Measurements::new(this_arch(), engine, wasm_file);
@@ -209,8 +219,8 @@ impl BenchmarkCommand {
209219
let engine = Engine::new(
210220
&mut bench_api,
211221
&working_dir,
212-
stdout,
213-
stderr,
222+
&stdout,
223+
&stderr,
214224
stdin,
215225
&mut measurements,
216226
&mut measure,
@@ -248,7 +258,31 @@ impl BenchmarkCommand {
248258
}
249259
}
250260

251-
self.check_output(Path::new(wasm_file), stdout, stderr)?;
261+
let check_result = self.check_output(Path::new(wasm_file), &stdout, &stderr);
262+
263+
// Handle log cleanup based on success/failure and --keep-logs flag
264+
match check_result {
265+
Ok(()) => {
266+
if !self.keep_logs {
267+
let _ = fs::remove_file(&stdout);
268+
let _ = fs::remove_file(&stderr);
269+
} else {
270+
log::info!(
271+
"Kept log files: {} and {}",
272+
stdout.display(),
273+
stderr.display()
274+
);
275+
}
276+
}
277+
Err(e) => {
278+
// Failure: keep logs and inform user
279+
eprintln!("Benchmark output check failed. Log files preserved:");
280+
eprintln!(" stdout: {}", stdout.display());
281+
eprintln!(" stderr: {}", stderr.display());
282+
return Err(e);
283+
}
284+
}
285+
252286
engine
253287
.as_mut()
254288
.map(|e| e.measurements())
@@ -377,6 +411,10 @@ impl BenchmarkCommand {
377411
command.arg("--pin");
378412
}
379413

414+
if self.keep_logs {
415+
command.arg("--keep-logs");
416+
}
417+
380418
if self.small_workloads {
381419
command.env("WASM_BENCH_USE_SMALL_WORKLOAD", "1");
382420
}

crates/cli/src/clean.rs

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,55 @@ pub struct CleanCommand {}
99

1010
impl CleanCommand {
1111
pub fn execute(&self) -> Result<()> {
12-
// Remove log files.
13-
let log_file_regex = Regex::new(r"^(stdout|stderr)\-\w+\-\d+-\d+.log$").unwrap();
14-
for entry in std::env::current_dir()?.read_dir()? {
15-
let entry = entry?;
12+
let log_file_regex = Regex::new(r"^(stdout|stderr)\-\w+\-\d+.log$").unwrap();
13+
let mut removed_count = 0;
1614

17-
// Only consider files, not dirs or symlinks.
18-
if !entry.file_type()?.is_file() {
15+
// Clean log files from both current directory (legacy) and temp directory (new)
16+
let dirs_to_clean = vec![
17+
std::env::current_dir()?,
18+
std::env::temp_dir().join("sightglass-logs"),
19+
];
20+
21+
for dir in dirs_to_clean {
22+
if !dir.exists() {
1923
continue;
2024
}
2125

22-
let path = entry.path();
23-
24-
// If it doesn't have a file name, it definitely isn't a log file.
25-
let name = match path.file_name().and_then(|n| n.to_str()) {
26-
None => continue,
27-
Some(n) => n,
26+
let read_dir = match dir.read_dir() {
27+
Ok(rd) => rd,
28+
Err(_) => continue,
2829
};
2930

30-
// If it doesn't match our log file regex, it isn't a log file.
31-
if !log_file_regex.is_match(name) {
32-
continue;
33-
}
31+
for entry in read_dir {
32+
let entry = entry?;
33+
34+
// Only consider files, not dirs or symlinks.
35+
if !entry.file_type()?.is_file() {
36+
continue;
37+
}
3438

35-
// Okay! It's one of our log files!
36-
log::info!("Removing log file: {}", path.display());
37-
std::fs::remove_file(&path)
38-
.with_context(|| format!("failed to remove {}", path.display()))?;
39+
let path = entry.path();
40+
41+
// If it doesn't have a file name, it definitely isn't a log file.
42+
let name = match path.file_name().and_then(|n| n.to_str()) {
43+
None => continue,
44+
Some(n) => n,
45+
};
46+
47+
// If it doesn't match our log file regex, it isn't a log file.
48+
if !log_file_regex.is_match(name) {
49+
continue;
50+
}
51+
52+
// Okay! It's one of our log files!
53+
log::info!("Removing log file: {}", path.display());
54+
std::fs::remove_file(&path)
55+
.with_context(|| format!("failed to remove {}", path.display()))?;
56+
removed_count += 1;
57+
}
3958
}
4059

60+
println!("Removed {} log file(s)", removed_count);
4161
Ok(())
4262
}
4363
}

0 commit comments

Comments
 (0)