1 /* Copyright (c) 2017-2020. The SimGrid Team. All rights reserved. */
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. */
8 #include "simgrid/kernel/resource/Resource.hpp"
9 #include "src/kernel/resource/profile/DatedValue.hpp"
10 #include "src/kernel/resource/profile/Event.hpp"
11 #include "src/kernel/resource/profile/Profile.hpp"
12 #include "src/kernel/resource/profile/StochasticDatedValue.hpp"
13 #include "src/surf/surf_interface.hpp"
17 #include "xbt/random.hpp"
21 XBT_LOG_NEW_DEFAULT_CATEGORY(unit, "Unit tests of the Trace Manager");
24 class MockedResource : public simgrid::kernel::resource::Resource {
26 explicit MockedResource() : simgrid::kernel::resource::Resource(nullptr, "fake", nullptr) {}
27 void apply_event(simgrid::kernel::profile::Event* event, double value) override
29 XBT_VERB("t=%.1f: Change value to %lg (idx: %u)", thedate, value, event->idx);
30 tmgr_trace_event_unref(&event);
32 bool is_used() const override { return true; }
35 static std::vector<simgrid::kernel::profile::DatedValue> trace2vector(const char* str)
37 std::vector<simgrid::kernel::profile::DatedValue> res;
38 simgrid::kernel::profile::Profile* trace = simgrid::kernel::profile::Profile::from_string("TheName", str, 0);
39 XBT_VERB("---------------------------------------------------------");
40 XBT_VERB("data>>\n%s<<data\n", str);
41 for (auto const& evt : trace->event_list)
42 XBT_VERB("event: d:%lg v:%lg", evt.date_, evt.value_);
44 MockedResource daResource;
45 simgrid::kernel::profile::FutureEvtSet fes;
46 simgrid::kernel::profile::Event* insertedIt = trace->schedule(&fes, &daResource);
48 while (fes.next_date() <= 20.0 && fes.next_date() >= 0) {
49 thedate = fes.next_date();
51 simgrid::kernel::resource::Resource* resource;
52 simgrid::kernel::profile::Event* it = fes.pop_leq(thedate, &value, &resource);
56 REQUIRE(it == insertedIt); // Check that we find what we've put
58 res.emplace_back(thedate, value);
60 XBT_DEBUG("%.1f: ignore an event (idx: %u)\n", thedate, it->idx);
62 resource->apply_event(it, value);
68 static std::vector<simgrid::kernel::profile::StochasticDatedValue> trace2selist(const char* str)
70 simgrid::kernel::profile::Profile* trace = simgrid::kernel::profile::Profile::from_string("TheName", str, 0);
71 std::vector<simgrid::kernel::profile::StochasticDatedValue> stocevlist = trace->stochastic_event_list;
76 TEST_CASE("kernel::profile: Resource profiles, defining the external load", "kernel::profile")
78 SECTION("No event, no loop")
80 std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("");
81 std::vector<simgrid::kernel::profile::DatedValue> want;
85 SECTION("One event no loop")
87 std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("9.0 3.0\n");
89 std::vector<simgrid::kernel::profile::DatedValue> want;
90 want.emplace_back(9, 3);
94 SECTION("Two events, no loop")
96 std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("3.0 1.0\n"
99 std::vector<simgrid::kernel::profile::DatedValue> want;
100 want.emplace_back(3, 1);
101 want.emplace_back(9, 3);
103 REQUIRE(want == got);
106 SECTION("Three events, no loop")
108 std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("3.0 1.0\n"
112 std::vector<simgrid::kernel::profile::DatedValue> want;
113 want.emplace_back(3, 1);
114 want.emplace_back(5, 2);
115 want.emplace_back(9, 3);
117 REQUIRE(want == got);
120 SECTION("Two events, looping")
122 std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("1.0 1.0\n"
126 std::vector<simgrid::kernel::profile::DatedValue> want;
127 want.emplace_back(1, 1);
128 want.emplace_back(3, 3);
129 want.emplace_back(6, 1);
130 want.emplace_back(8, 3);
131 want.emplace_back(11, 1);
132 want.emplace_back(13, 3);
133 want.emplace_back(16, 1);
134 want.emplace_back(18, 3);
136 REQUIRE(want == got);
139 SECTION("Two events, looping, start at 0")
141 std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("0.0 1\n"
145 std::vector<simgrid::kernel::profile::DatedValue> want;
146 want.emplace_back(0, 1);
147 want.emplace_back(5, 2);
148 want.emplace_back(10, 1);
149 want.emplace_back(15, 2);
150 want.emplace_back(20, 1);
152 REQUIRE(want == got);
155 SECTION("One stochastic event (parsing)")
157 std::vector<simgrid::kernel::profile::StochasticDatedValue> got = trace2selist("STOCHASTIC\n"
160 std::vector<simgrid::kernel::profile::StochasticDatedValue> want;
161 want.push_back(simgrid::kernel::profile::StochasticDatedValue(0, -1)); // The initial fake event
162 want.push_back(simgrid::kernel::profile::StochasticDatedValue(simgrid::kernel::profile::Dist_Det, {0},
163 simgrid::kernel::profile::Dist_Unif, {10, 20}));
165 REQUIRE(want == got);
168 SECTION("Several stochastic events (all possible parsing forms)")
170 std::vector<simgrid::kernel::profile::StochasticDatedValue> got = trace2selist("STOCHASTIC\n"
172 "NORMAL 25 10 DET 3\n"
173 "UNIF 10 20 NORMAL 25 10\n"
176 std::vector<simgrid::kernel::profile::StochasticDatedValue> want;
177 want.push_back(simgrid::kernel::profile::StochasticDatedValue(0, -1));
178 want.push_back(simgrid::kernel::profile::StochasticDatedValue(simgrid::kernel::profile::Dist_Det, {0},
179 simgrid::kernel::profile::Dist_Det, {4}));
180 want.push_back(simgrid::kernel::profile::StochasticDatedValue(simgrid::kernel::profile::Dist_Norm, {25, 10},
181 simgrid::kernel::profile::Dist_Det, {3}));
182 want.push_back(simgrid::kernel::profile::StochasticDatedValue(simgrid::kernel::profile::Dist_Unif, {10, 20},
183 simgrid::kernel::profile::Dist_Norm, {25, 10}));
184 want.push_back(simgrid::kernel::profile::StochasticDatedValue(simgrid::kernel::profile::Dist_Det, {5},
185 simgrid::kernel::profile::Dist_Unif, {5, 25}));
187 REQUIRE(want == got);
190 SECTION("Two stochastic events (drawing each distribution)")
192 simgrid::xbt::random::set_implem_xbt();
193 simgrid::xbt::random::set_mersenne_seed(12345);
194 std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("STOCHASTIC\n"
196 "EXP 0.05 NORMAL 15 5");
198 std::vector<simgrid::kernel::profile::DatedValue> want;
199 // The following values were drawn using the XBT_RNG_xbt method /outside/ the testcase.
200 want.push_back(simgrid::kernel::profile::DatedValue(0, 19.29616086867082813683));
201 want.push_back(simgrid::kernel::profile::DatedValue(2.32719992449416279712, 20.16807234800742065772));
203 REQUIRE(want == got);
206 SECTION("Two stochastic events, with a loop")
208 simgrid::xbt::random::set_implem_xbt();
209 simgrid::xbt::random::set_mersenne_seed(12345);
210 std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("STOCHASTIC LOOP\n"
212 "EXP 0.05 NORMAL 15 5\n"
215 // In this case, the main use of the last stochastic event is to set when the first event takes place.
217 std::vector<simgrid::kernel::profile::DatedValue> want;
218 want.push_back(simgrid::kernel::profile::DatedValue(0, 19.29616086867082813683));
219 want.push_back(simgrid::kernel::profile::DatedValue(2.32719992449416279712, 20.16807234800742065772));
220 want.push_back(simgrid::kernel::profile::DatedValue(3.51111873684917075167, 0));
221 want.push_back(simgrid::kernel::profile::DatedValue(3.51111873684917075167, 10.39759496468994726115));
223 REQUIRE(want == got);