Skip to content

Commit 0286639

Browse files
committed
Connected Report struct to the serialize function
1 parent e4a964f commit 0286639

File tree

4 files changed

+151
-44
lines changed

4 files changed

+151
-44
lines changed

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ use std::path::Path;
22
mod yadm;
33

44
fn main() {
5-
yadm::serialize(Path::new("."))
5+
yadm::serialize(Path::new("../"))
66
}

src/yadm.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
pub mod report;
2-
pub use report::report::Report;
3-
42
pub mod serialize;
53
pub use serialize::serialize;

src/yadm/report.rs

Lines changed: 125 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,125 @@
11
use std::fmt;
2+
use std::path::PathBuf;
23
use std::time::{Duration, Instant};
34

4-
pub mod report {
5-
use super::*;
5+
pub struct Report {
6+
pub target_path: PathBuf,
7+
pub scan_start_at: Instant,
8+
pub scan_duration: Duration,
9+
pub scan_started: bool,
10+
pub elements_found: usize,
611

7-
pub struct Report {
8-
pub target: String,
9-
pub scan_start_at: Instant,
10-
pub scan_duration: Duration,
11-
pub elements_found: u64,
12+
pub hashmap_parsing_start_at: Instant,
13+
pub hashmap_parsing_duration: Duration,
14+
pub hashmap_parsing_started: bool,
1215

13-
pub hashmap_parsing_start_at: Instant,
14-
pub hashmap_parsing_duration: Duration,
16+
pub msgpack_parsing_start_at: Instant,
17+
pub msgpack_parsing_duration: Duration,
18+
pub msgpack_parsing: bool,
1519

16-
pub msgpack_parsing_start_at: Instant,
17-
pub msgpack_parsing_duration: Duration,
20+
pub zstd_compression_start_at: Instant,
21+
pub zstd_compression_duration: Duration,
22+
pub zstd_compression: bool,
1823

19-
pub output_file_size: u64,
20-
pub full_duration: Duration,
21-
pub average_duration_by_file: u64,
22-
pub average_size_by_file: u64,
24+
pub output_file_size: usize,
25+
pub full_duration: Duration,
26+
pub average_duration_by_file: usize,
27+
pub average_size_by_file: usize,
28+
}
29+
30+
impl Report {
31+
pub fn time_scan(&mut self) {
32+
if self.scan_started {
33+
self.scan_duration = Instant::now() - self.scan_start_at;
34+
} else {
35+
self.scan_started = true;
36+
}
37+
}
38+
pub fn time_hashmap_parsing(&mut self) {
39+
if self.hashmap_parsing_started {
40+
self.hashmap_parsing_duration = Instant::now() - self.hashmap_parsing_start_at;
41+
} else {
42+
self.hashmap_parsing_started = true;
43+
}
44+
}
45+
pub fn time_msgpack_parsing(&mut self) {
46+
if self.msgpack_parsing {
47+
self.msgpack_parsing_duration = Instant::now() - self.msgpack_parsing_start_at;
48+
} else {
49+
self.msgpack_parsing = true;
50+
}
51+
}
52+
pub fn time_zstd_compression(&mut self) {
53+
if self.zstd_compression {
54+
self.zstd_compression_duration = Instant::now() - self.zstd_compression_start_at;
55+
} else {
56+
self.zstd_compression = true;
57+
}
58+
}
59+
60+
pub fn new(target_path: PathBuf) -> Self {
61+
// We use `Instant::now()` as a neutral initialization value for all start timestamps.
62+
// Although these fields will be overwritten later when their respective steps actually begin,
63+
// `Instant` does not implement `Default`, and there is no way to create an "empty" Instant.
64+
// Using a common `now` reference ensures the struct is fully initialized with valid values
65+
// and avoids the need for Option<Instant>, which would add complexity and require unwrapping.
66+
let now = Instant::now();
67+
Report {
68+
target_path: target_path.canonicalize().unwrap_or(target_path),
69+
scan_start_at: now,
70+
scan_duration: Duration::ZERO,
71+
scan_started: false,
72+
elements_found: usize::default(),
73+
74+
hashmap_parsing_start_at: now,
75+
hashmap_parsing_duration: Duration::ZERO,
76+
hashmap_parsing_started: false,
77+
78+
msgpack_parsing_start_at: now,
79+
msgpack_parsing_duration: Duration::ZERO,
80+
msgpack_parsing: false,
81+
82+
zstd_compression_start_at: now,
83+
zstd_compression_duration: Duration::ZERO,
84+
zstd_compression: false,
85+
86+
output_file_size: usize::default(),
87+
full_duration: Duration::ZERO,
88+
average_duration_by_file: usize::default(),
89+
average_size_by_file: usize::default(),
90+
}
2391
}
2492

25-
impl fmt::Display for Report {
26-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
27-
write!(
28-
f,
29-
"Scan of {} found {} elements in {:?}",
30-
self.target, self.elements_found, self.full_duration
31-
)?;
93+
pub fn close_report(&mut self) {
94+
self.full_duration = self.scan_duration
95+
+ self.hashmap_parsing_duration
96+
+ self.msgpack_parsing_duration
97+
+ self.zstd_compression_duration;
3298

33-
write!(
34-
f,
35-
"
99+
if self.elements_found > 0 {
100+
let nanos = self.full_duration.as_nanos() / self.elements_found as u128;
101+
self.average_duration_by_file = (nanos / 1_000_000) as usize; // en millisecondes
102+
self.average_size_by_file = self.output_file_size / self.elements_found;
103+
} else {
104+
self.average_duration_by_file = 0;
105+
self.average_size_by_file = 0;
106+
}
107+
}
108+
}
109+
110+
impl fmt::Display for Report {
111+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
112+
write!(
113+
f,
114+
"Scan of {} found {} elements in {:?}",
115+
self.target_path.to_str().unwrap(),
116+
self.elements_found,
117+
self.full_duration
118+
)?;
119+
120+
write!(
121+
f,
122+
"
36123
=== YADM report ===
37124
Target : {}
38125
Scan duration : {:.2}s
@@ -49,19 +136,18 @@ Output file size : {:.2} Mo
49136
Output file size : {:.2} Go
50137
===================
51138
",
52-
self.target,
53-
self.scan_duration.as_secs_f64(),
54-
self.elements_found,
55-
self.hashmap_parsing_duration.as_secs_f64(),
56-
self.msgpack_parsing_duration.as_secs_f64(),
57-
self.full_duration.as_secs_f64(),
58-
self.average_duration_by_file,
59-
self.average_size_by_file,
60-
self.output_file_size,
61-
self.output_file_size as f64 / 1024.0,
62-
self.output_file_size as f64 / (1024.0 * 1024.0),
63-
self.output_file_size as f64 / (1024.0 * 1024.0 * 1024.0),
64-
)
65-
}
139+
self.target_path.to_str().unwrap(),
140+
self.scan_duration.as_secs_f64(),
141+
self.elements_found,
142+
self.hashmap_parsing_duration.as_secs_f64(),
143+
self.msgpack_parsing_duration.as_secs_f64(),
144+
self.full_duration.as_secs_f64(),
145+
self.average_duration_by_file,
146+
self.average_size_by_file,
147+
self.output_file_size,
148+
self.output_file_size as f64 / 1024.0,
149+
self.output_file_size as f64 / (1024.0 * 1024.0),
150+
self.output_file_size as f64 / (1024.0 * 1024.0 * 1024.0),
151+
)
66152
}
67153
}

src/yadm/serialize.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use std::path::{Path, PathBuf};
55
use walkdir::WalkDir;
66
use zstd;
77

8+
use super::report::Report;
9+
810
/// TODO: Faire la doc
911
pub fn serialize(path: &Path) {
1012
// Step 1 : Getting all entities into the Path
@@ -57,15 +59,31 @@ pub fn serialize(path: &Path) {
5759
return file;
5860
}
5961

62+
// Report give some informations to know how good or bad this version of YADM was.
63+
let mut report: Report = Report::new(path.to_path_buf());
64+
6065
// We check if the path go to only one File to skip the WalkDir call if not needed.
6166
let hashmap: Vec<HashMap<String, String>> = if path.is_file() {
67+
// Because we didn't scan a directory, we intialized
68+
report.elements_found = 1;
69+
report.time_hashmap_parsing();
6270
parsing_to_hashmap(&[path.to_path_buf()])
6371
} else {
64-
parsing_to_hashmap(&scan_dir(path))
72+
report.time_scan();
73+
let entries: Vec<PathBuf> = scan_dir(path);
74+
report.time_scan();
75+
report.elements_found = entries.len();
76+
77+
report.time_hashmap_parsing();
78+
parsing_to_hashmap(&entries)
6579
};
6680

67-
// Sérialisation MsgPack dans fichier temporaire
81+
report.time_hashmap_parsing(); // Here we stop the timer
82+
83+
// Serialization into msgpack format
84+
report.time_msgpack_parsing();
6885
serialize_to_msgpack(&hashmap);
86+
report.time_msgpack_parsing();
6987

7088
// Ouvrir le fichier sérialisé en lecture
7189
let mut uncompressed_file =
@@ -75,11 +93,16 @@ pub fn serialize(path: &Path) {
7593
let output_file =
7694
File::create("output.msgpack.zst").expect("Failed to create output compressed file");
7795

96+
report.time_zstd_compression();
7897
// Compresser du fichier sérialisé vers le fichier compressé avec un niveau de compression à 22 (max)
7998
zstd::stream::copy_encode(&mut uncompressed_file, output_file, 22).expect("Compression failed");
99+
report.time_zstd_compression();
80100

81101
match std::fs::remove_file(Path::new("output.msgpack.temp")) {
82102
Ok(_) => println!("Fichier supprimé avec succès !"),
83103
Err(e) => eprintln!("Erreur lors de la suppression du fichier : {}", e),
84104
}
105+
106+
report.close_report();
107+
println!("{}", report);
85108
}

0 commit comments

Comments
 (0)