tests.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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-2015 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 > MyDynamics;
  53. typedef paradevs::pdevs::Simulator < DoubleTime, MyDynamics > MySimulator;
  54. typedef std::vector < MySimulator* > MySimulators;
  55. const int simulator_number = 5000;
  56. const double simulation_duration = 1000;
  57. const int sample_number = 20;
  58. double unit_test(unsigned int n)
  59. {
  60. MySimulators simulators;
  61. HeapScheduler < DoubleTime > scheduler;
  62. for (unsigned int i = 0; i < simulator_number; ++i) {
  63. std::ostringstream ss;
  64. ss << "a" << (i + 1);
  65. simulators.push_back(new MySimulator(ss.str(), NoParameters()));
  66. }
  67. std::vector < std::vector < unsigned int > > lists;
  68. for (unsigned int t = 1; t < simulation_duration; ++t) {
  69. std::vector < unsigned int > list;
  70. std::vector < unsigned int > list2;
  71. for (unsigned int i = 0; i < n; ++i) {
  72. list2.push_back(i);
  73. }
  74. for (unsigned int i = 0; i < n; ++i) {
  75. unsigned int j = rand() % list2.size();
  76. unsigned int index = list2[j];
  77. std::vector < unsigned int >::iterator it =
  78. std::find(list2.begin(), list2.end(), index);
  79. list.push_back(index);
  80. list2.erase(it);
  81. }
  82. lists.push_back(list);
  83. }
  84. steady_clock::time_point t1 = steady_clock::now();
  85. for (unsigned int i = 0; i < n; ++i) {
  86. scheduler.init(0., simulators[i]);
  87. }
  88. for (unsigned int t = 1; t < simulation_duration; ++t) {
  89. for (unsigned int i = 0; i < n; ++i) {
  90. scheduler.put((double)t, simulators[lists[t - 1][i]]);
  91. }
  92. }
  93. steady_clock::time_point t2 = steady_clock::now();
  94. duration < double > time_span = duration_cast <
  95. duration < double > >(t2 - t1);
  96. for (MySimulators::const_iterator it = simulators.begin();
  97. it != simulators.end(); ++it) {
  98. delete *it;
  99. }
  100. return time_span.count();
  101. }
  102. void test(unsigned int n, unsigned int r)
  103. {
  104. std::vector < double > t;
  105. for (unsigned int i = 0; i < sample_number; ++i) {
  106. t.push_back(unit_test(n));
  107. }
  108. double a = 0;
  109. for (unsigned int i = 0; i < sample_number; ++i) {
  110. a += t[i];
  111. }
  112. a /= sample_number;
  113. double sd = 0;
  114. for (unsigned int i = 0; i < sample_number; ++i) {
  115. sd += (t[i] - a)*(t[i] - a);
  116. }
  117. sd /= sample_number;
  118. sd = sqrt(sd);
  119. std::cout << n << "\t" << a << "\t" << (a * r) << "\t"
  120. << sd << "\t" << (sd * r) << std::endl;
  121. }
  122. TEST_CASE("common/scheduler/heap", "run")
  123. {
  124. for (unsigned int i = 1; i <= 200; i+= 5) {
  125. test(simulator_number / i, i);
  126. }
  127. }