11use std:: fmt;
2+ use std:: path:: PathBuf ;
23use 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 ===
37124Target : {}
38125Scan duration : {:.2}s
@@ -49,19 +136,18 @@ Output file size : {:.2} Mo
49136Output 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}
0 commit comments