-
Notifications
You must be signed in to change notification settings - Fork 496
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
695: a few tweaks to the demos r=cuviper a=nikomatsakis A few changes that I am making on my branch that I would like applied to master to. This renames some benchmarks to make them uniquely identifiable and also adds a test to measure #642. Co-authored-by: Niko Matsakis <niko@alum.mit.edu>
- Loading branch information
Showing
9 changed files
with
101 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use time::{self, Duration}; | ||
|
||
#[cfg(windows)] | ||
mod win; | ||
#[cfg(windows)] | ||
pub use self::win::get_cpu_time; | ||
|
||
#[cfg(unix)] | ||
mod unix; | ||
#[cfg(unix)] | ||
pub use self::unix::get_cpu_time; | ||
|
||
#[cfg(not(any(unix, windows)))] | ||
pub fn get_cpu_time() -> Option<u64> { | ||
None | ||
} | ||
|
||
pub fn get_cpu_duration(start: Option<u64>, stop: Option<u64>) -> Option<Duration> { | ||
start.and_then(|start| stop.and_then(|stop| Some(Duration::nanoseconds((stop - start) as i64)))) | ||
} | ||
|
||
#[derive(Copy, Clone)] | ||
pub struct CpuMeasure { | ||
/// number of ns | ||
pub time_duration: u64, | ||
|
||
/// percentage (0-100) of that as cpu time | ||
pub cpu_usage_percent: Option<f64>, | ||
} | ||
|
||
pub fn measure_cpu(op: impl FnOnce()) -> CpuMeasure { | ||
let time_start = time::precise_time_ns(); | ||
let cpu_start = get_cpu_time(); | ||
|
||
op(); | ||
|
||
let cpu_stop = get_cpu_time(); | ||
let time_duration = time::precise_time_ns() - time_start; | ||
|
||
CpuMeasure { | ||
time_duration, | ||
cpu_usage_percent: get_cpu_duration(cpu_start, cpu_stop) | ||
.and_then(|cpu| cpu.num_nanoseconds()) | ||
.and_then(|cpu| Some(100.0 * cpu as f64 / time_duration as f64)), | ||
} | ||
} | ||
|
||
pub fn print_time(m: CpuMeasure) { | ||
println!(" wallclock: {} ns", m.time_duration); | ||
if let Some(cpu_usage) = m.cpu_usage_percent { | ||
println!(" cpu usage: {:3.1}%", cpu_usage); | ||
} else { | ||
println!(" cpu usage: N/A"); | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const USAGE: &str = " | ||
Usage: noop [--sleep N] [--iters N] | ||
Noop loop to measure CPU usage. See rayon-rs/rayon#642. | ||
Options: | ||
--sleep N How long to sleep (in millis) between doing a spawn. [default: 10] | ||
--iters N Total time to execution (in millis). [default: 100] | ||
"; | ||
|
||
use cpu_time; | ||
use docopt::Docopt; | ||
|
||
#[derive(Deserialize)] | ||
pub struct Args { | ||
flag_sleep: u64, | ||
flag_iters: u64, | ||
} | ||
|
||
pub fn main(args: &[String]) { | ||
let args: Args = Docopt::new(USAGE) | ||
.and_then(|d| d.argv(args).deserialize()) | ||
.unwrap_or_else(|e| e.exit()); | ||
|
||
let m = cpu_time::measure_cpu(|| { | ||
for _ in 1..args.flag_iters { | ||
std::thread::sleep(std::time::Duration::from_millis(args.flag_sleep)); | ||
rayon::spawn(move || { } ); | ||
} | ||
}); | ||
println!("noop --iters={} --sleep={}", args.flag_iters, args.flag_sleep); | ||
cpu_time::print_time(m); | ||
} |