Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use consistent naming scheme.
[simgrid.git] / src / kernel / resource / profile / Profile_test.cpp
1 /* Copyright (c) 2017-2020. 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 "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"
13
14 #include "xbt/log.h"
15 #include "xbt/misc.h"
16
17 #include <cmath>
18
19 XBT_LOG_NEW_DEFAULT_CATEGORY(unit, "Unit tests of the Trace Manager");
20
21 double thedate;
22 class MockedResource : public simgrid::kernel::resource::Resource {
23 public:
24   explicit MockedResource() : simgrid::kernel::resource::Resource(nullptr, "fake", nullptr) {}
25   void apply_event(simgrid::kernel::profile::Event* event, double value) override
26   {
27     XBT_VERB("t=%.1f: Change value to %lg (idx: %u)", thedate, value, event->idx);
28     tmgr_trace_event_unref(&event);
29   }
30   bool is_used() const override { return true; }
31 };
32
33 static std::vector<simgrid::kernel::profile::DatedValue> trace2vector(const char* str)
34 {
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_);
41
42   MockedResource daResource;
43   simgrid::kernel::profile::FutureEvtSet fes;
44   simgrid::kernel::profile::Event* insertedIt = trace->schedule(&fes, &daResource);
45
46   while (fes.next_date() <= 20.0 && fes.next_date() >= 0) {
47     thedate = fes.next_date();
48     double value;
49     simgrid::kernel::resource::Resource* resource;
50     simgrid::kernel::profile::Event* it = fes.pop_leq(thedate, &value, &resource);
51     if (it == nullptr)
52       continue;
53
54     REQUIRE(it == insertedIt); // Check that we find what we've put
55     if (value >= 0) {
56       res.emplace_back(thedate, value);
57     } else {
58       XBT_DEBUG("%.1f: ignore an event (idx: %u)\n", thedate, it->idx);
59     }
60     resource->apply_event(it, value);
61   }
62   tmgr_finalize();
63   return res;
64 }
65
66 TEST_CASE("kernel::profile: Resource profiles, defining the external load", "kernel::profile")
67 {
68   SECTION("No event, no loop")
69   {
70     std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("");
71     std::vector<simgrid::kernel::profile::DatedValue> want;
72     REQUIRE(want == got);
73   }
74
75   SECTION("One event no loop")
76   {
77     std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("9.0 3.0\n");
78
79     std::vector<simgrid::kernel::profile::DatedValue> want;
80     want.emplace_back(9, 3);
81     REQUIRE(want == got);
82   }
83
84   SECTION("Two events, no loop")
85   {
86     std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("3.0 1.0\n"
87                                                                          "9.0 3.0\n");
88
89     std::vector<simgrid::kernel::profile::DatedValue> want;
90     want.emplace_back(3, 1);
91     want.emplace_back(9, 3);
92
93     REQUIRE(want == got);
94   }
95
96   SECTION("Three events, no loop")
97   {
98     std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("3.0 1.0\n"
99                                                                          "5.0 2.0\n"
100                                                                          "9.0 3.0\n");
101
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);
106
107     REQUIRE(want == got);
108   }
109
110   SECTION("Two events, looping")
111   {
112     std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("1.0 1.0\n"
113                                                                          "3.0 3.0\n"
114                                                                          "LOOPAFTER 2\n");
115
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);
125
126     REQUIRE(want == got);
127   }
128
129   SECTION("Two events, looping, start at 0")
130   {
131     std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("0.0 1\n"
132                                                                          "5.0 2\n"
133                                                                          "LOOPAFTER 5\n");
134
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);
141
142     REQUIRE(want == got);
143   }
144 }