Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix build on stable debian
[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/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() 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.push_back(simgrid::kernel::profile::DatedValue(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
69   SECTION("No event, no loop")
70   {
71     std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("");
72     std::vector<simgrid::kernel::profile::DatedValue> want;
73     REQUIRE(want == got);
74   }
75
76   SECTION("One event no loop")
77   {
78     std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("9.0 3.0\n");
79
80     std::vector<simgrid::kernel::profile::DatedValue> want;
81     want.push_back(simgrid::kernel::profile::DatedValue(9, 3));
82     REQUIRE(want == got);
83   }
84
85   SECTION("Two events, no loop")
86   {
87     std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("3.0 1.0\n"
88                                                                          "9.0 3.0\n");
89
90     std::vector<simgrid::kernel::profile::DatedValue> want;
91     want.push_back(simgrid::kernel::profile::DatedValue(3, 1));
92     want.push_back(simgrid::kernel::profile::DatedValue(9, 3));
93
94     REQUIRE(want == got);
95   }
96
97   SECTION("Three events, no loop")
98   {
99
100     std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("3.0 1.0\n"
101                                                                          "5.0 2.0\n"
102                                                                          "9.0 3.0\n");
103
104     std::vector<simgrid::kernel::profile::DatedValue> want;
105     want.push_back(simgrid::kernel::profile::DatedValue(3, 1));
106     want.push_back(simgrid::kernel::profile::DatedValue(5, 2));
107     want.push_back(simgrid::kernel::profile::DatedValue(9, 3));
108
109     REQUIRE(want == got);
110   }
111
112   SECTION("Two events, looping")
113   {
114     std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("1.0 1.0\n"
115                                                                          "3.0 3.0\n"
116                                                                          "LOOPAFTER 2\n");
117
118     std::vector<simgrid::kernel::profile::DatedValue> want;
119     want.push_back(simgrid::kernel::profile::DatedValue(1, 1));
120     want.push_back(simgrid::kernel::profile::DatedValue(3, 3));
121     want.push_back(simgrid::kernel::profile::DatedValue(6, 1));
122     want.push_back(simgrid::kernel::profile::DatedValue(8, 3));
123     want.push_back(simgrid::kernel::profile::DatedValue(11, 1));
124     want.push_back(simgrid::kernel::profile::DatedValue(13, 3));
125     want.push_back(simgrid::kernel::profile::DatedValue(16, 1));
126     want.push_back(simgrid::kernel::profile::DatedValue(18, 3));
127
128     REQUIRE(want == got);
129   }
130
131   SECTION("Two events, looping, start at 0")
132   {
133     std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("0.0 1\n"
134                                                                          "5.0 2\n"
135                                                                          "LOOPAFTER 5\n");
136
137     std::vector<simgrid::kernel::profile::DatedValue> want;
138     want.push_back(simgrid::kernel::profile::DatedValue(0, 1));
139     want.push_back(simgrid::kernel::profile::DatedValue(5, 2));
140     want.push_back(simgrid::kernel::profile::DatedValue(10, 1));
141     want.push_back(simgrid::kernel::profile::DatedValue(15, 2));
142     want.push_back(simgrid::kernel::profile::DatedValue(20, 1));
143
144     REQUIRE(want == got);
145   }
146 }