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.

128 lines
3.1KB

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