Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
s:k:profile: turn two functions into static methods
[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
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 = fes.add_trace(trace, &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       resource->apply_event(it, value);
57       res.push_back(simgrid::kernel::profile::DatedValue(thedate, value));
58     } else {
59       XBT_DEBUG("%.1f: ignore an event (idx: %u)\n", thedate, it->idx);
60     }
61   }
62   tmgr_finalize();
63   return res;
64 }
65
66 /* Fails in a way that is difficult to test: xbt_assert should become throw
67 BOOST_AUTO_TEST_CASE(no_evt_noloop) {
68 }*/
69 TEST_CASE("kernel::profile: Resource profiles, defining the external load", "kernel::profile")
70 {
71
72   SECTION("No event, no loop")
73   {
74     std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("");
75     std::vector<simgrid::kernel::profile::DatedValue> want;
76     REQUIRE(want == got);
77   }
78
79   SECTION("One event no loop")
80   {
81     std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("9.0 3.0\n");
82
83     std::vector<simgrid::kernel::profile::DatedValue> want;
84     want.push_back(simgrid::kernel::profile::DatedValue(9, 3));
85     REQUIRE(want == got);
86   }
87
88   SECTION("Two events, no loop")
89   {
90     std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("3.0 1.0\n"
91                                                                          "9.0 3.0\n");
92
93     std::vector<simgrid::kernel::profile::DatedValue> want;
94     want.push_back(simgrid::kernel::profile::DatedValue(3, 1));
95     want.push_back(simgrid::kernel::profile::DatedValue(9, 3));
96
97     REQUIRE(want == got);
98   }
99
100   SECTION("Three events, no loop")
101   {
102
103     std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("3.0 1.0\n"
104                                                                          "5.0 2.0\n"
105                                                                          "9.0 3.0\n");
106
107     std::vector<simgrid::kernel::profile::DatedValue> want;
108     want.push_back(simgrid::kernel::profile::DatedValue(3, 1));
109     want.push_back(simgrid::kernel::profile::DatedValue(5, 2));
110     want.push_back(simgrid::kernel::profile::DatedValue(9, 3));
111
112     REQUIRE(want == got);
113   }
114
115   SECTION("Two events, looping")
116   {
117     std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("1.0 1.0\n"
118                                                                          "3.0 3.0\n"
119                                                                          "LOOPAFTER 2\n");
120
121     std::vector<simgrid::kernel::profile::DatedValue> want;
122     want.push_back(simgrid::kernel::profile::DatedValue(1, 1));
123     want.push_back(simgrid::kernel::profile::DatedValue(3, 3));
124     want.push_back(simgrid::kernel::profile::DatedValue(6, 1));
125     want.push_back(simgrid::kernel::profile::DatedValue(8, 3));
126     want.push_back(simgrid::kernel::profile::DatedValue(11, 1));
127     want.push_back(simgrid::kernel::profile::DatedValue(13, 3));
128     want.push_back(simgrid::kernel::profile::DatedValue(16, 1));
129     want.push_back(simgrid::kernel::profile::DatedValue(18, 3));
130
131     REQUIRE(want == got);
132   }
133
134   SECTION("Two events, looping, start at 0")
135   {
136     std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("0.0 1\n"
137                                                                          "5.0 2\n"
138                                                                          "LOOPAFTER 5\n");
139
140     std::vector<simgrid::kernel::profile::DatedValue> want;
141     want.push_back(simgrid::kernel::profile::DatedValue(0, 1));
142     want.push_back(simgrid::kernel::profile::DatedValue(5, 2));
143     want.push_back(simgrid::kernel::profile::DatedValue(10, 1));
144     want.push_back(simgrid::kernel::profile::DatedValue(15, 2));
145     want.push_back(simgrid::kernel::profile::DatedValue(20, 1));
146
147     REQUIRE(want == got);
148   }
149 }