Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Provide our own main() for unit-tests.
[simgrid.git] / src / kernel / resource / profile / trace_mgr_test.cpp
1 /* Copyright (c) 2017-2019. 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/trace_mgr.hpp"
10 #include "src/surf/surf_interface.hpp"
11
12 #include "xbt/log.h"
13 #include "xbt/misc.h"
14
15 #include <cmath>
16
17 XBT_LOG_NEW_DEFAULT_CATEGORY(unit, "Unit tests of the Trace Manager");
18
19 double thedate;
20 class MockedResource : public simgrid::kernel::resource::Resource {
21 public:
22   explicit MockedResource() : simgrid::kernel::resource::Resource(nullptr, "fake", nullptr) {}
23   void apply_event(simgrid::kernel::profile::Event* event, double value) override
24   {
25     XBT_VERB("t=%.1f: Change value to %lg (idx: %u)", thedate, value, event->idx);
26     tmgr_trace_event_unref(&event);
27   }
28   bool is_used() override { return true; }
29 };
30
31 static std::vector<simgrid::kernel::profile::DatedValue> trace2vector(const char* str)
32 {
33   std::vector<simgrid::kernel::profile::DatedValue> res;
34   simgrid::kernel::profile::Profile* trace = simgrid::kernel::profile::Profile::from_string("TheName", str, 0);
35   XBT_VERB("---------------------------------------------------------");
36   XBT_VERB("data>>\n%s<<data\n", str);
37   for (auto const& evt : trace->event_list)
38     XBT_VERB("event: d:%lg v:%lg", evt.date_, evt.value_);
39
40   MockedResource daResource;
41   simgrid::kernel::profile::FutureEvtSet fes;
42   simgrid::kernel::profile::Event* insertedIt = trace->schedule(&fes, &daResource);
43
44   while (fes.next_date() <= 20.0 && fes.next_date() >= 0) {
45     thedate = fes.next_date();
46     double value;
47     simgrid::kernel::resource::Resource* resource;
48     simgrid::kernel::profile::Event* it = fes.pop_leq(thedate, &value, &resource);
49     if (it == nullptr)
50       continue;
51
52     REQUIRE(it == insertedIt); // Check that we find what we've put
53     if (value >= 0) {
54       resource->apply_event(it, value);
55       res.push_back(simgrid::kernel::profile::DatedValue(thedate, value));
56     } else {
57       XBT_DEBUG("%.1f: ignore an event (idx: %u)\n", thedate, it->idx);
58     }
59   }
60   tmgr_finalize();
61   return res;
62 }
63
64 TEST_CASE("kernel::profile: Resource profiles, defining the external load", "kernel::profile")
65 {
66
67   SECTION("No event, no loop")
68   {
69     std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("");
70     std::vector<simgrid::kernel::profile::DatedValue> want;
71     REQUIRE(want == got);
72   }
73
74   SECTION("One event no loop")
75   {
76     std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("9.0 3.0\n");
77
78     std::vector<simgrid::kernel::profile::DatedValue> want;
79     want.push_back(simgrid::kernel::profile::DatedValue(9, 3));
80     REQUIRE(want == got);
81   }
82
83   SECTION("Two events, no loop")
84   {
85     std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("3.0 1.0\n"
86                                                                          "9.0 3.0\n");
87
88     std::vector<simgrid::kernel::profile::DatedValue> want;
89     want.push_back(simgrid::kernel::profile::DatedValue(3, 1));
90     want.push_back(simgrid::kernel::profile::DatedValue(9, 3));
91
92     REQUIRE(want == got);
93   }
94
95   SECTION("Three events, no loop")
96   {
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.push_back(simgrid::kernel::profile::DatedValue(3, 1));
104     want.push_back(simgrid::kernel::profile::DatedValue(5, 2));
105     want.push_back(simgrid::kernel::profile::DatedValue(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.push_back(simgrid::kernel::profile::DatedValue(1, 1));
118     want.push_back(simgrid::kernel::profile::DatedValue(3, 3));
119     want.push_back(simgrid::kernel::profile::DatedValue(6, 1));
120     want.push_back(simgrid::kernel::profile::DatedValue(8, 3));
121     want.push_back(simgrid::kernel::profile::DatedValue(11, 1));
122     want.push_back(simgrid::kernel::profile::DatedValue(13, 3));
123     want.push_back(simgrid::kernel::profile::DatedValue(16, 1));
124     want.push_back(simgrid::kernel::profile::DatedValue(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.push_back(simgrid::kernel::profile::DatedValue(0, 1));
137     want.push_back(simgrid::kernel::profile::DatedValue(5, 2));
138     want.push_back(simgrid::kernel::profile::DatedValue(10, 1));
139     want.push_back(simgrid::kernel::profile::DatedValue(15, 2));
140     want.push_back(simgrid::kernel::profile::DatedValue(20, 1));
141
142     REQUIRE(want == got);
143   }
144 }