tests.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /**
  2. * @file tests/common/scheduler/tests.cpp
  3. * @author The PARADEVS Development Team
  4. * See the AUTHORS or Authors.txt file
  5. */
  6. /*
  7. * PARADEVS - the multimodeling and simulation environment
  8. * This file is a part of the PARADEVS environment
  9. *
  10. * Copyright (C) 2013 ULCO http://www.univ-litoral.fr
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation, either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. #include <paradevs/common/scheduler/HeapScheduler.hpp>
  26. #include <paradevs/common/time/DoubleTime.hpp>
  27. #include <paradevs/kernel/pdevs/Dynamics.hpp>
  28. #include <paradevs/kernel/pdevs/Simulator.hpp>
  29. #include <chrono>
  30. #define CATCH_CONFIG_MAIN
  31. #include <tests/catch.hpp>
  32. using namespace paradevs::common;
  33. using namespace paradevs::common::scheduler;
  34. using namespace paradevs::pdevs;
  35. using namespace std::chrono;
  36. struct SchedulerHandle;
  37. typedef typename HeapScheduler < DoubleTime,
  38. SchedulerHandle >::type SchedulerType;
  39. struct SchedulerHandle
  40. {
  41. SchedulerHandle()
  42. { }
  43. SchedulerHandle(const SchedulerType::handle_type& handle)
  44. : _handle(handle)
  45. { }
  46. const SchedulerHandle& handle() const
  47. { return *this; }
  48. void handle(const SchedulerHandle& handle)
  49. { _handle = handle._handle; }
  50. SchedulerType::handle_type _handle;
  51. };
  52. typedef Dynamics < DoubleTime, SchedulerHandle > MyDynamics;
  53. typedef paradevs::pdevs::Simulator < DoubleTime, MyDynamics,
  54. SchedulerHandle > MySimulator;
  55. typedef std::vector < MySimulator* > MySimulators;
  56. const int simulator_number = 5000;
  57. const double simulation_duration = 1000;
  58. const int sample_number = 20;
  59. double unit_test(unsigned int n)
  60. {
  61. MySimulators simulators;
  62. HeapScheduler < DoubleTime, SchedulerHandle > scheduler;
  63. for (unsigned int i = 0; i < simulator_number; ++i) {
  64. std::ostringstream ss;
  65. ss << "a" << (i + 1);
  66. simulators.push_back(new MySimulator(ss.str(), NoParameters()));
  67. }
  68. std::vector < std::vector < unsigned int > > lists;
  69. for (unsigned int t = 1; t < simulation_duration; ++t) {
  70. std::vector < unsigned int > list;
  71. std::vector < unsigned int > list2;
  72. for (unsigned int i = 0; i < n; ++i) {
  73. list2.push_back(i);
  74. }
  75. for (unsigned int i = 0; i < n; ++i) {
  76. unsigned int j = rand() % list2.size();
  77. unsigned int index = list2[j];
  78. std::vector < unsigned int >::iterator it =
  79. std::find(list2.begin(), list2.end(), index);
  80. list.push_back(index);
  81. list2.erase(it);
  82. }
  83. lists.push_back(list);
  84. }
  85. steady_clock::time_point t1 = steady_clock::now();
  86. for (unsigned int i = 0; i < n; ++i) {
  87. scheduler.init(0., simulators[i]);
  88. }
  89. for (unsigned int t = 1; t < simulation_duration; ++t) {
  90. for (unsigned int i = 0; i < n; ++i) {
  91. scheduler.put((double)t, simulators[lists[t - 1][i]]);
  92. }
  93. }
  94. steady_clock::time_point t2 = steady_clock::now();
  95. duration < double > time_span = duration_cast <
  96. duration < double > >(t2 - t1);
  97. for (MySimulators::const_iterator it = simulators.begin();
  98. it != simulators.end(); ++it) {
  99. delete *it;
  100. }
  101. return time_span.count();
  102. }
  103. void test(unsigned int n, unsigned int r)
  104. {
  105. std::vector < double > t;
  106. for (unsigned int i = 0; i < sample_number; ++i) {
  107. t.push_back(unit_test(n));
  108. }
  109. double a = 0;
  110. for (unsigned int i = 0; i < sample_number; ++i) {
  111. a += t[i];
  112. }
  113. a /= sample_number;
  114. double sd = 0;
  115. for (unsigned int i = 0; i < sample_number; ++i) {
  116. sd += (t[i] - a)*(t[i] - a);
  117. }
  118. sd /= sample_number;
  119. sd = sqrt(sd);
  120. std::cout << n << "\t" << a << "\t" << (a * r) << "\t"
  121. << sd << "\t" << (sd * r) << std::endl;
  122. }
  123. TEST_CASE("common/scheduler/heap", "run")
  124. {
  125. for (unsigned int i = 1; i <= 200; i+= 5) {
  126. test(simulator_number / i, i);
  127. }
  128. }