You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
3.2KB

  1. #![feature(duration_from_micros)]
  2. #![allow(unused)]
  3. extern crate logging;
  4. use std::time::{Instant, Duration};
  5. use std::thread;
  6. use logging::hist::{Entry, HistLog, nanos};
  7. const N_SECS: u64 = 30;
  8. //const N_THREADS: usize = 48;
  9. macro_rules! n_threads {
  10. ($n:expr) => {
  11. const N_THREADS: usize = $n;
  12. const NAME: &'static str = concat!("sleeptest_", stringify!($n));
  13. }
  14. }
  15. n_threads!(96);
  16. fn main() {
  17. ::std::process::exit(test_n());
  18. }
  19. fn test_n() -> i32 {
  20. let start = Instant::now();
  21. let hist = HistLog::new(NAME, "master", Duration::from_millis(100));
  22. for _ in 0..N_THREADS {
  23. let mut a = hist.clone_with_tag("sleep_1ns");
  24. thread::spawn(move || {
  25. let mut prev = Instant::now();
  26. let mut loop_time = Instant::now();
  27. loop {
  28. prev = loop_time;
  29. loop_time = Instant::now();
  30. a.record(nanos(loop_time - prev));
  31. a.check_send(loop_time);
  32. if loop_time - start > Duration::from_secs(N_SECS) { break }
  33. thread::sleep(Duration::new(0, 1));
  34. //thread::yield_now();
  35. }
  36. });
  37. }
  38. thread::sleep(Duration::from_secs(N_SECS));
  39. 0
  40. }
  41. fn test_3() -> i32 {
  42. let start = Instant::now();
  43. let hist = HistLog::new("sleeptest", "master", Duration::from_millis(100));
  44. let mut a = hist.clone_with_tag("yield");
  45. let mut b = hist.clone_with_tag("sleep_0");
  46. let mut c = hist.clone_with_tag("sleep_100ns");
  47. thread::spawn(move || {
  48. let mut prev = Instant::now();
  49. let mut loop_time = Instant::now();
  50. let mut i = 0;
  51. loop {
  52. prev = loop_time;
  53. loop_time = Instant::now();
  54. a.record(nanos(loop_time - prev));
  55. a.check_send(loop_time);
  56. if loop_time - start > Duration::from_secs(N_SECS) { break }
  57. i += 1;
  58. //if i % 100 == 0 { thread::sleep(Duration::new(0, 0)); }
  59. //thread::sleep(Duration::new(0, 0));
  60. thread::yield_now();
  61. }
  62. });
  63. thread::spawn(move || {
  64. let mut prev = Instant::now();
  65. let mut loop_time = Instant::now();
  66. let mut i = 0;
  67. loop {
  68. prev = loop_time;
  69. loop_time = Instant::now();
  70. b.record(nanos(loop_time - prev));
  71. b.check_send(loop_time);
  72. if loop_time - start > Duration::from_secs(N_SECS) { break }
  73. i += 1;
  74. //if i % 1_000 == 0 { thread::sleep(Duration::new(0, 0)); }
  75. //if i % 100 == 0 { thread::sleep(Duration::new(0, 0)); }
  76. thread::sleep(Duration::new(0, 0));
  77. }
  78. });
  79. let mut prev = Instant::now();
  80. let mut loop_time = Instant::now();
  81. let mut i = 0;
  82. loop {
  83. prev = loop_time;
  84. loop_time = Instant::now();
  85. c.record(nanos(loop_time - prev));
  86. c.check_send(loop_time);
  87. if loop_time - start > Duration::from_secs(N_SECS) { break }
  88. i += 1;
  89. //if i % 100_000 == 0 { thread::sleep(Duration::from_millis(10)); }
  90. //if i % 100 == 0 { thread::sleep(Duration::new(0, 0)); }
  91. thread::sleep(Duration::new(0, 100));
  92. }
  93. 0
  94. }