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/surf/surf_interface.hpp"
19 XBT_LOG_NEW_DEFAULT_CATEGORY(unit, "Unit tests of the Trace Manager");
22 class MockedResource : public simgrid::kernel::resource::Resource {
24 explicit MockedResource() : simgrid::kernel::resource::Resource(nullptr, "fake", nullptr) {}
25 void apply_event(simgrid::kernel::profile::Event* event, double value) override
27 XBT_VERB("t=%.1f: Change value to %lg (idx: %u)", thedate, value, event->idx);
28 tmgr_trace_event_unref(&event);
30 bool is_used() const override { return true; }
33 static std::vector<simgrid::kernel::profile::DatedValue> trace2vector(const char* str)
35 std::vector<simgrid::kernel::profile::DatedValue> res;
36 simgrid::kernel::profile::Profile* trace = simgrid::kernel::profile::Profile::from_string("TheName", str, 0);
37 XBT_VERB("---------------------------------------------------------");
38 XBT_VERB("data>>\n%s<<data\n", str);
39 for (auto const& evt : trace->event_list)
40 XBT_VERB("event: d:%lg v:%lg", evt.date_, evt.value_);
42 MockedResource daResource;
43 simgrid::kernel::profile::FutureEvtSet fes;
44 simgrid::kernel::profile::Event* insertedIt = trace->schedule(&fes, &daResource);
46 while (fes.next_date() <= 20.0 && fes.next_date() >= 0) {
47 thedate = fes.next_date();
49 simgrid::kernel::resource::Resource* resource;
50 simgrid::kernel::profile::Event* it = fes.pop_leq(thedate, &value, &resource);
54 REQUIRE(it == insertedIt); // Check that we find what we've put
56 res.emplace_back(thedate, value);
58 XBT_DEBUG("%.1f: ignore an event (idx: %u)\n", thedate, it->idx);
60 resource->apply_event(it, value);
66 TEST_CASE("kernel::profile: Resource profiles, defining the external load", "kernel::profile")
68 SECTION("No event, no loop")
70 std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("");
71 std::vector<simgrid::kernel::profile::DatedValue> want;
75 SECTION("One event no loop")
77 std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("9.0 3.0\n");
79 std::vector<simgrid::kernel::profile::DatedValue> want;
80 want.emplace_back(9, 3);
84 SECTION("Two events, no loop")
86 std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("3.0 1.0\n"
89 std::vector<simgrid::kernel::profile::DatedValue> want;
90 want.emplace_back(3, 1);
91 want.emplace_back(9, 3);
96 SECTION("Three events, no loop")
98 std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("3.0 1.0\n"
102 std::vector<simgrid::kernel::profile::DatedValue> want;
103 want.emplace_back(3, 1);
104 want.emplace_back(5, 2);
105 want.emplace_back(9, 3);
107 REQUIRE(want == got);
110 SECTION("Two events, looping")
112 std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("1.0 1.0\n"
116 std::vector<simgrid::kernel::profile::DatedValue> want;
117 want.emplace_back(1, 1);
118 want.emplace_back(3, 3);
119 want.emplace_back(6, 1);
120 want.emplace_back(8, 3);
121 want.emplace_back(11, 1);
122 want.emplace_back(13, 3);
123 want.emplace_back(16, 1);
124 want.emplace_back(18, 3);
126 REQUIRE(want == got);
129 SECTION("Two events, looping, start at 0")
131 std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("0.0 1\n"
135 std::vector<simgrid::kernel::profile::DatedValue> want;
136 want.emplace_back(0, 1);
137 want.emplace_back(5, 2);
138 want.emplace_back(10, 1);
139 want.emplace_back(15, 2);
140 want.emplace_back(20, 1);
142 REQUIRE(want == got);