Skip to content

Commit 832505f

Browse files
committed
chore(spec): little refacto on the spec to simplify further migration
1 parent 8358838 commit 832505f

12 files changed

Lines changed: 603 additions & 559 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//! Appends benchmark results to a CSV file.
2+
3+
use std::fs::{File, OpenOptions};
4+
use std::io::Write;
5+
use std::path::Path;
6+
7+
pub struct CsvResultWriter {
8+
file: File,
9+
}
10+
11+
impl CsvResultWriter {
12+
pub fn new(file_name: &str) -> Self {
13+
let file_path = Path::new(file_name);
14+
Self::from_path(file_path)
15+
}
16+
17+
pub fn from_path(path: &Path) -> Self {
18+
if !path.exists() {
19+
if let Some(parent) = path.parent() {
20+
std::fs::create_dir_all(parent).expect("cannot create parent dirs");
21+
}
22+
File::create(path).expect("cannot create result file");
23+
}
24+
let file = OpenOptions::new()
25+
.append(true)
26+
.open(path)
27+
.expect("cannot open result file");
28+
Self { file }
29+
}
30+
31+
pub fn write_result(&mut self, name: &str, value: usize) {
32+
let line = format!("{name},{value}\n");
33+
let error_message = format!("cannot write {name} result into file");
34+
self.file.write_all(line.as_bytes()).expect(&error_message);
35+
}
36+
}

0 commit comments

Comments
 (0)