1 /* Copyright (c) 2017-2019. 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. */
6 #define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
7 #include "src/include/catch.hpp"
9 #include "simgrid/kernel/resource/Resource.hpp"
10 #include "src/kernel/resource/profile/trace_mgr.hpp"
11 #include "src/surf/surf_interface.hpp"
18 XBT_LOG_NEW_DEFAULT_CATEGORY(unit, "Unit tests of the Trace Manager");
21 class MockedResource : public simgrid::kernel::resource::Resource {
23 explicit MockedResource() : simgrid::kernel::resource::Resource(nullptr, "fake", nullptr) {}
24 void apply_event(simgrid::kernel::profile::Event* event, double value) override
26 XBT_VERB("t=%.1f: Change value to %lg (idx: %u)", thedate, value, event->idx);
27 tmgr_trace_event_unref(&event);
29 bool is_used() override { return true; }
32 static std::vector<simgrid::kernel::profile::DatedValue> trace2vector(const char* str)
34 std::vector<simgrid::kernel::profile::DatedValue> res;
35 simgrid::kernel::profile::Profile* trace = simgrid::kernel::profile::Profile::from_string("TheName", str, 0);
36 XBT_VERB("---------------------------------------------------------");
37 XBT_VERB("data>>\n%s<<data\n", str);
38 for (auto const& evt : trace->event_list)
39 XBT_VERB("event: d:%lg v:%lg", evt.date_, evt.value_);
41 MockedResource daResource;
42 simgrid::kernel::profile::FutureEvtSet fes;
43 simgrid::kernel::profile::Event* insertedIt = trace->schedule(&fes, &daResource);
45 while (fes.next_date() <= 20.0 && fes.next_date() >= 0) {
46 thedate = fes.next_date();
48 simgrid::kernel::resource::Resource* resource;
49 simgrid::kernel::profile::Event* it = fes.pop_leq(thedate, &value, &resource);
53 REQUIRE(it == insertedIt); // Check that we find what we've put
55 resource->apply_event(it, value);
56 res.push_back(simgrid::kernel::profile::DatedValue(thedate, value));
58 XBT_DEBUG("%.1f: ignore an event (idx: %u)\n", thedate, it->idx);
65 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.push_back(simgrid::kernel::profile::DatedValue(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.push_back(simgrid::kernel::profile::DatedValue(3, 1));
91 want.push_back(simgrid::kernel::profile::DatedValue(9, 3));
96 SECTION("Three events, no loop")
99 std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("3.0 1.0\n"
103 std::vector<simgrid::kernel::profile::DatedValue> want;
104 want.push_back(simgrid::kernel::profile::DatedValue(3, 1));
105 want.push_back(simgrid::kernel::profile::DatedValue(5, 2));
106 want.push_back(simgrid::kernel::profile::DatedValue(9, 3));
108 REQUIRE(want == got);
111 SECTION("Two events, looping")
113 std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("1.0 1.0\n"
117 std::vector<simgrid::kernel::profile::DatedValue> want;
118 want.push_back(simgrid::kernel::profile::DatedValue(1, 1));
119 want.push_back(simgrid::kernel::profile::DatedValue(3, 3));
120 want.push_back(simgrid::kernel::profile::DatedValue(6, 1));
121 want.push_back(simgrid::kernel::profile::DatedValue(8, 3));
122 want.push_back(simgrid::kernel::profile::DatedValue(11, 1));
123 want.push_back(simgrid::kernel::profile::DatedValue(13, 3));
124 want.push_back(simgrid::kernel::profile::DatedValue(16, 1));
125 want.push_back(simgrid::kernel::profile::DatedValue(18, 3));
127 REQUIRE(want == got);
130 SECTION("Two events, looping, start at 0")
132 std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("0.0 1\n"
136 std::vector<simgrid::kernel::profile::DatedValue> want;
137 want.push_back(simgrid::kernel::profile::DatedValue(0, 1));
138 want.push_back(simgrid::kernel::profile::DatedValue(5, 2));
139 want.push_back(simgrid::kernel::profile::DatedValue(10, 1));
140 want.push_back(simgrid::kernel::profile::DatedValue(15, 2));
141 want.push_back(simgrid::kernel::profile::DatedValue(20, 1));
143 REQUIRE(want == got);