1- use std:: cmp:: min;
21use std:: {
32 env,
43 fs:: { self , File } ,
5- io:: Write ,
64 time:: { Duration , Instant } ,
75} ;
86
9- use benchmark:: execute_query;
7+ use benchmark:: { download_file , execute_query, time_direct_io } ;
108use clap:: Parser ;
119use flate2:: read:: GzDecoder ;
1210use futures:: future:: join_all;
13- use futures_util:: StreamExt ;
1411use indicatif:: { ProgressBar , ProgressStyle } ;
1512use serde:: { Deserialize , Serialize } ;
1613use tar:: Archive ;
@@ -91,38 +88,6 @@ pub struct Cli {
9188 pub force_update : bool ,
9289}
9390
94- pub async fn download_file ( url : & str , path : & str ) -> Result < ( ) , String > {
95- let client = reqwest:: Client :: new ( ) ;
96- let res = client
97- . get ( url)
98- . send ( )
99- . await
100- . or ( Err ( format ! ( "Failed to GET from '{}'" , & url) ) ) ?;
101- let total_size = res
102- . content_length ( )
103- . ok_or ( format ! ( "Failed to get content length from '{}'" , & url) ) ?;
104-
105- let pb = ProgressBar :: new ( total_size) ;
106- pb. set_style ( ProgressStyle :: with_template ( "{msg}\n {spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {bytes}/{total_bytes} ({bytes_per_sec}, {eta})" ) . unwrap ( )
107- . progress_chars ( "#>-" ) ) ;
108- pb. set_message ( format ! ( "Downloading {url}" ) . to_owned ( ) ) ;
109-
110- let mut file = File :: create ( path) . or ( Err ( format ! ( "Failed to create file '{path}'" ) ) ) ?;
111- let mut downloaded: u64 = 0 ;
112- let mut stream = res. bytes_stream ( ) ;
113-
114- while let Some ( item) = stream. next ( ) . await {
115- let chunk = item. or ( Err ( "Error while downloading file" . to_owned ( ) ) ) ?;
116- file. write_all ( & chunk)
117- . or ( Err ( "Error while writing to file" . to_owned ( ) ) ) ?;
118- let new = min ( downloaded + ( chunk. len ( ) as u64 ) , total_size) ;
119- downloaded = new;
120- pb. set_position ( new) ;
121- }
122-
123- pb. finish_with_message ( format ! ( "Downloaded {url} to {path}" ) . to_owned ( ) ) ;
124- Ok ( ( ) )
125- }
12691
12792#[ tokio:: main]
12893async fn main ( ) {
@@ -150,7 +115,10 @@ async fn main() {
150115 let id = Uuid :: new_v4 ( ) ;
151116 dir. push ( id. to_string ( ) ) ;
152117
153- let _ = download_file ( & url, dir. to_str ( ) . unwrap ( ) ) . await ;
118+ let big_download = download_file ( & url, dir. to_str ( ) . unwrap ( ) ) . await ;
119+ let download_speed = big_download. unwrap ( ) ;
120+ println ! ( "Download speed: {:?}" , download_speed) ;
121+
154122 let tar_gz = File :: open ( dir) . unwrap ( ) ;
155123 let tar = GzDecoder :: new ( tar_gz) ;
156124 let mut archive = Archive :: new ( tar) ;
@@ -161,6 +129,19 @@ async fn main() {
161129 bench_set = serde_yaml:: from_reader ( reader) . unwrap ( ) ;
162130 }
163131
132+ {
133+ // let file_path = "/home/denis/worker_bench/data/testfile.bin";
134+ let file_path = "./data/testfile.bin" ;
135+ let mut io_time = Duration :: new ( 0 , 0 ) ;
136+ let iterations = 10 ;
137+ for _ in 0 ..iterations {
138+ io_time += time_direct_io ( file_path. to_string ( ) ) . unwrap ( ) ;
139+ }
140+ io_time = io_time / iterations;
141+ println ! ( "Direct IO time: {:?}" , io_time) ;
142+ }
143+ // exit(0);
144+
164145 let width = bench_set. len ( ) ;
165146 let max_concurrent = args. max_concurrency ;
166147 let total_samples = args. total_samples ;
0 commit comments