Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cosmetics
[simgrid.git] / src / kernel / resource / profile / Profile_test.cpp
1 /* Copyright (c) 2017-2022. The SimGrid Team. All rights reserved.               */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "catch.hpp"
7
8 #include "src/kernel/resource/Resource.hpp"
9 #include "src/kernel/resource/profile/Event.hpp"
10 #include "simgrid/kernel/ProfileBuilder.hpp"
11 #include "src/kernel/resource/profile/StochasticDatedValue.hpp"
12 #include "src/surf/surf_interface.hpp"
13
14 #include "xbt/log.h"
15 #include "xbt/misc.h"
16 #include "xbt/random.hpp"
17
18 #include <cmath>
19
20 XBT_LOG_NEW_DEFAULT_CATEGORY(unit, "Unit tests of the Trace Manager");
21
22 class MockedResource : public simgrid::kernel::resource::Resource {
23 public:
24   static double the_date;
25
26   explicit MockedResource() : Resource("fake"){}
27   void apply_event(simgrid::kernel::profile::Event* event, double value) override
28   {
29     XBT_VERB("t=%.1f: Change value to %lg (idx: %u)", the_date, value, event->idx);
30     tmgr_trace_event_unref(&event);
31   }
32   bool is_used() const override { return true; }
33 };
34
35 double MockedResource::the_date;
36
37 static std::vector<simgrid::kernel::profile::DatedValue> trace2vector(const char* str)
38 {
39   std::vector<simgrid::kernel::profile::DatedValue> res;
40   simgrid::kernel::profile::Profile* trace = simgrid::kernel::profile::ProfileBuilder::from_string("TheName", str, 0);
41   XBT_VERB("---------------------------------------------------------");
42   XBT_VERB("data>>\n%s<<data\n", str);
43   for (auto const& evt : trace->get_event_list())
44     XBT_VERB("event: d:%lg v:%lg", evt.date_, evt.value_);
45
46   MockedResource daResource;
47   simgrid::kernel::profile::FutureEvtSet fes;
48   simgrid::kernel::profile::Event* insertedIt = trace->schedule(&fes, &daResource);
49
50   while (fes.next_date() <= 20.0 && fes.next_date() >= 0) {
51     MockedResource::the_date = fes.next_date();
52     double value;
53     simgrid::kernel::resource::Resource* resource;
54     simgrid::kernel::profile::Event* it = fes.pop_leq(MockedResource::the_date, &value, &resource);
55     if (it == nullptr)
56       continue;
57
58     REQUIRE(it == insertedIt); // Check that we find what we've put
59     if (value >= 0) {
60       res.emplace_back(MockedResource::the_date, value);
61     } else {
62       XBT_DEBUG("%.1f: ignore an event (idx: %u)\n", MockedResource::the_date, it->idx);
63     }
64     resource->apply_event(it, value);
65   }
66   tmgr_finalize();
67   return res;
68 }
69
70 TEST_CASE("kernel::profile: Resource profiles, defining the external load", "kernel::profile")
71 {
72   SECTION("No event, no loop")
73   {
74     std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("");
75     std::vector<simgrid::kernel::profile::DatedValue> want;
76     REQUIRE(want == got);
77   }
78
79   SECTION("One event no loop")
80   {
81     std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("9.0 3.0\n");
82
83     std::vector<simgrid::kernel::profile::DatedValue> want;
84     want.emplace_back(9, 3);
85     REQUIRE(want == got);
86   }
87
88   SECTION("Two events, no loop")
89   {
90     std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("3.0 1.0\n"
91                                                                          "9.0 3.0\n");
92
93     std::vector<simgrid::kernel::profile::DatedValue> want;
94     want.emplace_back(3, 1);
95     want.emplace_back(9, 3);
96
97     REQUIRE(want == got);
98   }
99
100   SECTION("Three events, no loop")
101   {
102     std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("3.0 1.0\n"
103                                                                          "5.0 2.0\n"
104                                                                          "9.0 3.0\n");
105
106     std::vector<simgrid::kernel::profile::DatedValue> want;
107     want.emplace_back(3, 1);
108     want.emplace_back(5, 2);
109     want.emplace_back(9, 3);
110
111     REQUIRE(want == got);
112   }
113
114   SECTION("Two events, looping")
115   {
116     std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("1.0 1.0\n"
117                                                                          "3.0 3.0\n"
118                                                                          "LOOPAFTER 2\n");
119
120     std::vector<simgrid::kernel::profile::DatedValue> want;
121     want.emplace_back(1, 1);
122     want.emplace_back(3, 3);
123     want.emplace_back(6, 1);
124     want.emplace_back(8, 3);
125     want.emplace_back(11, 1);
126     want.emplace_back(13, 3);
127     want.emplace_back(16, 1);
128     want.emplace_back(18, 3);
129
130     REQUIRE(want == got);
131   }
132
133   SECTION("Two events, looping, start at 0")
134   {
135     std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("0.0 1\n"
136                                                                          "5.0 2\n"
137                                                                          "LOOPAFTER 5\n");
138
139     std::vector<simgrid::kernel::profile::DatedValue> want;
140     want.emplace_back(0, 1);
141     want.emplace_back(5, 2);
142     want.emplace_back(10, 1);
143     want.emplace_back(15, 2);
144     want.emplace_back(20, 1);
145
146     REQUIRE(want == got);
147   }
148
149   SECTION("One stochastic event (parsing)")
150   {
151     using simgrid::kernel::profile::Distribution;
152     std::vector<simgrid::kernel::profile::StochasticDatedValue> got = trace2selist("STOCHASTIC\n"
153                                                                                    "DET 0 UNIF 10 20");
154
155     std::vector<simgrid::kernel::profile::StochasticDatedValue> want;
156     want.emplace_back(Distribution::DET, std::vector<double>{0}, Distribution::UNIF, std::vector<double>{10, 20});
157
158     REQUIRE(want == got);
159   }
160
161   SECTION("Several stochastic events (all possible parsing forms)")
162   {
163     using simgrid::kernel::profile::Distribution;
164     std::vector<simgrid::kernel::profile::StochasticDatedValue> got = trace2selist("STOCHASTIC\n"
165                                                                                    "DET 0 DET 4\n"
166                                                                                    "NORMAL 25 10 DET 3\n"
167                                                                                    "UNIF 10 20 NORMAL 25 10\n"
168                                                                                    "DET 5 UNIF 5 25");
169
170     std::vector<simgrid::kernel::profile::StochasticDatedValue> want;
171     want.emplace_back(Distribution::DET, std::vector<double>{0}, Distribution::DET, std::vector<double>{4});
172     want.emplace_back(Distribution::NORM, std::vector<double>{25, 10}, Distribution::DET, std::vector<double>{3});
173     want.emplace_back(Distribution::UNIF, std::vector<double>{10, 20}, Distribution::NORM, std::vector<double>{25, 10});
174     want.emplace_back(Distribution::DET, std::vector<double>{5}, Distribution::UNIF, std::vector<double>{5, 25});
175
176     REQUIRE(want == got);
177   }
178
179   SECTION("Two stochastic events (drawing each distribution)")
180   {
181     simgrid::xbt::random::set_implem_xbt();
182     simgrid::xbt::random::set_mersenne_seed(12345);
183     std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("STOCHASTIC\n"
184                                                                          "DET 0 UNIF 10 20\n"
185                                                                          "EXP 0.05 NORMAL 15 5");
186
187     std::vector<simgrid::kernel::profile::DatedValue> want;
188     // The following values were drawn using the XBT_RNG_xbt method /outside/ the testcase.
189     want.emplace_back(0, 19.29616086867082813683);
190     want.emplace_back(2.32719992449416279712, 20.16807234800742065772);
191
192     REQUIRE(want == got);
193   }
194
195   SECTION("Two stochastic events, with a loop")
196   {
197     simgrid::xbt::random::set_implem_xbt();
198     simgrid::xbt::random::set_mersenne_seed(12345);
199     std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("STOCHASTIC LOOP\n"
200                                                                          "DET 0 UNIF 10 20\n"
201                                                                          "EXP 0.05 NORMAL 15 5\n"
202                                                                          "UNIF 1 2 DET 0");
203
204     // In this case, the main use of the last stochastic event is to set when the first event takes place.
205
206     std::vector<simgrid::kernel::profile::DatedValue> want;
207     want.emplace_back(0, 19.29616086867082813683);
208     want.emplace_back(2.32719992449416279712, 20.16807234800742065772);
209     want.emplace_back(3.51111873684917075167, 0);
210     want.emplace_back(3.51111873684917075167, 10.39759496468994726115);
211
212     REQUIRE(want == got);
213   }
214 }