Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://framagit.org/simgrid/simgrid
[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 #define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
7 #include "src/include/catch.hpp"
8
9 #include "simgrid/kernel/resource/Resource.hpp"
10 #include "src/kernel/resource/profile/trace_mgr.hpp"
11 #include "src/surf/surf_interface.hpp"
12
13 #include "xbt/log.h"
14 #include "xbt/misc.h"
15
16 #include <cmath>
17
18 XBT_LOG_NEW_DEFAULT_CATEGORY(unit, "Unit tests of the Trace Manager");
19
20 double thedate;
21 class MockedResource : public simgrid::kernel::resource::Resource {
22 public:
23   explicit MockedResource() : simgrid::kernel::resource::Resource(nullptr, "fake", nullptr) {}
24   void apply_event(simgrid::kernel::profile::Event* event, double value) override
25   {
26     XBT_VERB("t=%.1f: Change value to %lg (idx: %u)", thedate, value, event->idx);
27     tmgr_trace_event_unref(&event);
28   }
29   bool is_used() override { return true; }
30 };
31
32 static std::vector<simgrid::kernel::profile::DatedValue> trace2vector(const char* str)
33 {
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_);
40
41   MockedResource daResource;
42   simgrid::kernel::profile::FutureEvtSet fes;
43   simgrid::kernel::profile::Event* insertedIt = trace->schedule(&fes, &daResource);
44
45   while (fes.next_date() <= 20.0 && fes.next_date() >= 0) {
46     thedate = fes.next_date();
47     double value;
48     simgrid::kernel::resource::Resource* resource;
49     simgrid::kernel::profile::Event* it = fes.pop_leq(thedate, &value, &resource);
50     if (it == nullptr)
51       continue;
52
53     REQUIRE(it == insertedIt); // Check that we find what we've put
54     if (value >= 0) {
55       resource->apply_event(it, value);
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   }
61   tmgr_finalize();
62   return res;
63 }
64
65 TEST_CASE("kernel::profile: Resource profiles, defining the external load", "kernel::profile")
66 {
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.push_back(simgrid::kernel::profile::DatedValue(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.push_back(simgrid::kernel::profile::DatedValue(3, 1));
91     want.push_back(simgrid::kernel::profile::DatedValue(9, 3));
92
93     REQUIRE(want == got);
94   }
95
96   SECTION("Three events, no loop")
97   {
98
99     std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("3.0 1.0\n"
100                                                                          "5.0 2.0\n"
101                                                                          "9.0 3.0\n");
102
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));
107
108     REQUIRE(want == got);
109   }
110
111   SECTION("Two events, looping")
112   {
113     std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("1.0 1.0\n"
114                                                                          "3.0 3.0\n"
115                                                                          "LOOPAFTER 2\n");
116
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));
126
127     REQUIRE(want == got);
128   }
129
130   SECTION("Two events, looping, start at 0")
131   {
132     std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("0.0 1\n"
133                                                                          "5.0 2\n"
134                                                                          "LOOPAFTER 5\n");
135
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));
142
143     REQUIRE(want == got);
144   }
145 }