source: benchmark/bench.rs@ 195f43d

Last change on this file since 195f43d was 821c534, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

Implemented transfer in rust and fixed minor issues with rust benchmarks

  • Property mode set to 100644
File size: 2.4 KB
Line 
1use std::io::{self, Write};
2use std::option;
3use std::sync::atomic::{AtomicU64, AtomicBool, Ordering};
4use std::time::{Instant,Duration};
5use std::u128;
6
7use clap::{Arg, ArgMatches};
8use isatty::stdout_isatty;
9
10use tokio::time;
11
12
13// ==================================================
14pub fn args<'a, 'b>() -> [Arg<'a, 'b>; 4] {[
15 Arg::with_name("duration") .short("d").long("duration") .takes_value(true).default_value("5").help("Duration of the experiments in seconds"),
16 Arg::with_name("iterations").short("i").long("iterations").takes_value(true).conflicts_with("duration").help("Number of iterations of the experiments"),
17 Arg::with_name("nthreads") .short("t").long("nthreads") .takes_value(true).default_value("1").help("Number of threads to use"),
18 Arg::with_name("nprocs") .short("p").long("nprocs") .takes_value(true).default_value("1").help("Number of processors to use")
19]}
20
21pub struct BenchData {
22 pub clock_mode: bool,
23 pub stop: AtomicBool,
24 pub stop_count: u64,
25 pub duration: f64,
26 pub threads_left: AtomicU64,
27 is_tty: bool,
28}
29
30impl BenchData {
31 pub fn new(options: ArgMatches, nthreads: usize, default_it: option::Option<u64>) -> BenchData {
32 let (clock_mode, stop_count, duration) = if options.is_present("iterations") {
33 (false,
34 options.value_of("iterations").unwrap().parse::<u64>().unwrap(),
35 -1.0)
36 } else if !default_it.is_none() {
37 (false,
38 default_it.unwrap(),
39 -1.0)
40 } else {
41 (true,
42 std::u64::MAX,
43 options.value_of("duration").unwrap().parse::<f64>().unwrap())
44 };
45
46 BenchData{
47 clock_mode: clock_mode,
48 stop: AtomicBool::new(false),
49 stop_count: stop_count,
50 duration: duration,
51 threads_left: AtomicU64::new(nthreads as u64),
52 is_tty: stdout_isatty(),
53 }
54 }
55
56 #[allow(dead_code)]
57 pub async fn wait(&self, start: &Instant) -> Duration{
58 loop {
59 time::sleep(Duration::from_micros(100000)).await;
60 let delta = start.elapsed();
61 if self.is_tty {
62 print!(" {:.1}\r", delta.as_secs_f32());
63 io::stdout().flush().unwrap();
64 }
65 if self.clock_mode && delta >= Duration::from_secs_f64(self.duration) {
66 break;
67 }
68 else if !self.clock_mode && self.threads_left.load(Ordering::Relaxed) == 0 {
69 break;
70 }
71 }
72
73 self.stop.store(true, Ordering::SeqCst);
74 return start.elapsed();
75 }
76}
77
78// ==================================================
79pub fn _lehmer64( state: &mut u128 ) -> u64 {
80 *state = state.wrapping_mul(0xda942042e4dd58b5);
81 return (*state >> 64) as u64;
82}
Note: See TracBrowser for help on using the repository browser.