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.

scheduler_tests.cpp 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright (c) 2012-2013 The Bitcoin Core developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #include "random.h"
  5. #include "scheduler.h"
  6. #include "test/test_bitcoin.h"
  7. #include <boost/bind.hpp>
  8. #include <boost/random/mersenne_twister.hpp>
  9. #include <boost/random/uniform_int_distribution.hpp>
  10. #include <boost/thread.hpp>
  11. #include <boost/test/unit_test.hpp>
  12. BOOST_AUTO_TEST_SUITE(scheduler_tests)
  13. static void microTask(CScheduler& s, boost::mutex& mutex, int& counter, int delta, boost::chrono::system_clock::time_point rescheduleTime)
  14. {
  15. {
  16. boost::unique_lock<boost::mutex> lock(mutex);
  17. counter += delta;
  18. }
  19. boost::chrono::system_clock::time_point noTime = boost::chrono::system_clock::time_point::min();
  20. if (rescheduleTime != noTime) {
  21. CScheduler::Function f = boost::bind(&microTask, boost::ref(s), boost::ref(mutex), boost::ref(counter), -delta + 1, noTime);
  22. s.schedule(f, rescheduleTime);
  23. }
  24. }
  25. static void MicroSleep(uint64_t n)
  26. {
  27. #if defined(HAVE_WORKING_BOOST_SLEEP_FOR)
  28. boost::this_thread::sleep_for(boost::chrono::microseconds(n));
  29. #elif defined(HAVE_WORKING_BOOST_SLEEP)
  30. boost::this_thread::sleep(boost::posix_time::microseconds(n));
  31. #else
  32. //should never get here
  33. #error missing boost sleep implementation
  34. #endif
  35. }
  36. BOOST_AUTO_TEST_CASE(manythreads)
  37. {
  38. // Stress test: hundreds of microsecond-scheduled tasks,
  39. // serviced by 10 threads.
  40. //
  41. // So... ten shared counters, which if all the tasks execute
  42. // properly will sum to the number of tasks done.
  43. // Each task adds or subtracts from one of the counters a
  44. // random amount, and then schedules another task 0-1000
  45. // microseconds in the future to subtract or add from
  46. // the counter -random_amount+1, so in the end the shared
  47. // counters should sum to the number of initial tasks performed.
  48. CScheduler microTasks;
  49. boost::thread_group microThreads;
  50. for (int i = 0; i < 5; i++)
  51. microThreads.create_thread(boost::bind(&CScheduler::serviceQueue, &microTasks));
  52. boost::mutex counterMutex[10];
  53. int counter[10] = { 0 };
  54. boost::random::mt19937 rng(insecure_rand());
  55. boost::random::uniform_int_distribution<> zeroToNine(0, 9);
  56. boost::random::uniform_int_distribution<> randomMsec(-11, 1000);
  57. boost::random::uniform_int_distribution<> randomDelta(-1000, 1000);
  58. boost::chrono::system_clock::time_point start = boost::chrono::system_clock::now();
  59. boost::chrono::system_clock::time_point now = start;
  60. for (int i = 0; i < 100; i++) {
  61. boost::chrono::system_clock::time_point t = now + boost::chrono::microseconds(randomMsec(rng));
  62. boost::chrono::system_clock::time_point tReschedule = now + boost::chrono::microseconds(500 + randomMsec(rng));
  63. int whichCounter = zeroToNine(rng);
  64. CScheduler::Function f = boost::bind(&microTask, boost::ref(microTasks),
  65. boost::ref(counterMutex[whichCounter]), boost::ref(counter[whichCounter]),
  66. randomDelta(rng), tReschedule);
  67. microTasks.schedule(f, t);
  68. }
  69. MicroSleep(600);
  70. now = boost::chrono::system_clock::now();
  71. // More threads and more tasks:
  72. for (int i = 0; i < 5; i++)
  73. microThreads.create_thread(boost::bind(&CScheduler::serviceQueue, &microTasks));
  74. for (int i = 0; i < 100; i++) {
  75. boost::chrono::system_clock::time_point t = now + boost::chrono::microseconds(randomMsec(rng));
  76. boost::chrono::system_clock::time_point tReschedule = now + boost::chrono::microseconds(500 + randomMsec(rng));
  77. int whichCounter = zeroToNine(rng);
  78. CScheduler::Function f = boost::bind(&microTask, boost::ref(microTasks),
  79. boost::ref(counterMutex[whichCounter]), boost::ref(counter[whichCounter]),
  80. randomDelta(rng), tReschedule);
  81. microTasks.schedule(f, t);
  82. }
  83. // All 2,000 tasks should be finished within 2 milliseconds. Sleep a bit longer.
  84. MicroSleep(2100);
  85. microThreads.interrupt_all();
  86. microThreads.join_all();
  87. int counterSum = 0;
  88. for (int i = 0; i < 10; i++) {
  89. BOOST_CHECK(counter[i] != 0);
  90. counterSum += counter[i];
  91. }
  92. BOOST_CHECK_EQUAL(counterSum, 200);
  93. }
  94. BOOST_AUTO_TEST_SUITE_END()