artdaq  v3_11_00
CapacityTest_policy_t.cc
1 #define BOOST_TEST_MODULE CapacityTest_policy_t
2 #include <boost/test/unit_test.hpp>
3 
4 #include "artdaq-utilities/Plugins/MakeParameterSet.hh"
5 #include "artdaq/RoutingPolicies/makeRoutingManagerPolicy.hh"
6 #include "fhiclcpp/ParameterSet.h"
7 
8 BOOST_AUTO_TEST_SUITE(CapacityTest_policy_t)
9 
10 BOOST_AUTO_TEST_CASE(Simple)
11 {
12  TLOG(TLVL_INFO) << "CapacityTest_policy_t Test Case Simple BEGIN";
13  fhicl::ParameterSet ps = artdaq::make_pset("tokens_used_per_table_percent: 50");
14 
15  auto ct = artdaq::makeRoutingManagerPolicy("CapacityTest", ps);
16 
17  ct->AddReceiverToken(1, 10);
18  ct->AddReceiverToken(2, 10);
19  ct->AddReceiverToken(3, 10);
20  ct->AddReceiverToken(4, 10);
21  BOOST_REQUIRE_EQUAL(ct->GetReceiverCount(), 4);
22 
23  auto firstTable = ct->GetCurrentTable();
24  BOOST_REQUIRE_EQUAL(firstTable.size(), 20);
25  BOOST_REQUIRE_EQUAL(firstTable[0].destination_rank, 1);
26  BOOST_REQUIRE_EQUAL(firstTable[0].sequence_id, 1);
27  BOOST_REQUIRE_EQUAL(firstTable[firstTable.size() - 1].destination_rank, 2);
28  BOOST_REQUIRE_EQUAL(firstTable[firstTable.size() - 1].sequence_id, 20);
29 
30  ct->ResetTokensUsedSinceLastUpdate();
31 
32  //ct->Reset();
33  ct->AddReceiverToken(1, 1);
34  ct->AddReceiverToken(3, 1);
35  ct->AddReceiverToken(2, 1);
36  ct->AddReceiverToken(4, 1);
37  ct->AddReceiverToken(2, 1);
38  auto secondTable = ct->GetCurrentTable();
39  BOOST_REQUIRE_EQUAL(secondTable.size(), 13);
40  BOOST_REQUIRE_EQUAL(secondTable[0].destination_rank, 1);
41  BOOST_REQUIRE_EQUAL(secondTable[0].sequence_id, 21);
42  BOOST_REQUIRE_EQUAL(secondTable[1].destination_rank, 2);
43  BOOST_REQUIRE_EQUAL(secondTable[1].sequence_id, 22);
44  BOOST_REQUIRE_EQUAL(secondTable[2].destination_rank, 2);
45  BOOST_REQUIRE_EQUAL(secondTable[2].sequence_id, 23);
46  BOOST_REQUIRE_EQUAL(secondTable[3].destination_rank, 3);
47  BOOST_REQUIRE_EQUAL(secondTable[3].sequence_id, 24);
48  BOOST_REQUIRE_EQUAL(secondTable[4].destination_rank, 3);
49  BOOST_REQUIRE_EQUAL(secondTable[4].sequence_id, 25);
50  BOOST_REQUIRE_EQUAL(secondTable[5].destination_rank, 3);
51  BOOST_REQUIRE_EQUAL(secondTable[5].sequence_id, 26);
52  BOOST_REQUIRE_EQUAL(secondTable[6].destination_rank, 3);
53  BOOST_REQUIRE_EQUAL(secondTable[6].sequence_id, 27);
54  BOOST_REQUIRE_EQUAL(secondTable[7].destination_rank, 3);
55  BOOST_REQUIRE_EQUAL(secondTable[7].sequence_id, 28);
56  BOOST_REQUIRE_EQUAL(secondTable[8].destination_rank, 3);
57  BOOST_REQUIRE_EQUAL(secondTable[8].sequence_id, 29);
58  BOOST_REQUIRE_EQUAL(secondTable[9].destination_rank, 3);
59  BOOST_REQUIRE_EQUAL(secondTable[9].sequence_id, 30);
60  BOOST_REQUIRE_EQUAL(secondTable[10].destination_rank, 3);
61  BOOST_REQUIRE_EQUAL(secondTable[10].sequence_id, 31);
62  BOOST_REQUIRE_EQUAL(secondTable[11].destination_rank, 3);
63  BOOST_REQUIRE_EQUAL(secondTable[11].sequence_id, 32);
64  BOOST_REQUIRE_EQUAL(secondTable[12].destination_rank, 3);
65  BOOST_REQUIRE_EQUAL(secondTable[12].sequence_id, 33);
66 
67  ct->ResetTokensUsedSinceLastUpdate();
68  //ct->Reset();
69  ct->AddReceiverToken(1, 0);
70  auto thirdTable = ct->GetCurrentTable();
71  BOOST_REQUIRE_EQUAL(thirdTable.size(), 6);
72  BOOST_REQUIRE_EQUAL(thirdTable[0].destination_rank, 3);
73  BOOST_REQUIRE_EQUAL(thirdTable[0].sequence_id, 34);
74  BOOST_REQUIRE_EQUAL(thirdTable[1].destination_rank, 4);
75  BOOST_REQUIRE_EQUAL(thirdTable[1].sequence_id, 35);
76  BOOST_REQUIRE_EQUAL(thirdTable[2].destination_rank, 4);
77  BOOST_REQUIRE_EQUAL(thirdTable[2].sequence_id, 36);
78  BOOST_REQUIRE_EQUAL(thirdTable[3].destination_rank, 4);
79  BOOST_REQUIRE_EQUAL(thirdTable[3].sequence_id, 37);
80  BOOST_REQUIRE_EQUAL(thirdTable[4].destination_rank, 4);
81  BOOST_REQUIRE_EQUAL(thirdTable[4].sequence_id, 38);
82  BOOST_REQUIRE_EQUAL(thirdTable[5].destination_rank, 4);
83  BOOST_REQUIRE_EQUAL(thirdTable[5].sequence_id, 39);
84 
85  ct->AddReceiverToken(1, 2);
86  ct->AddReceiverToken(2, 1);
87  ct->AddReceiverToken(3, 1);
88  ct->AddReceiverToken(4, 2);
89  ct->ResetTokensUsedSinceLastUpdate();
90  auto fourthTable = ct->GetCurrentTable();
91  BOOST_REQUIRE_EQUAL(fourthTable.size(), 6);
92  BOOST_REQUIRE_EQUAL(fourthTable[0].destination_rank, 1);
93  BOOST_REQUIRE_EQUAL(fourthTable[0].sequence_id, 40);
94  BOOST_REQUIRE_EQUAL(fourthTable[1].destination_rank, 1);
95  BOOST_REQUIRE_EQUAL(fourthTable[1].sequence_id, 41);
96  BOOST_REQUIRE_EQUAL(fourthTable[2].destination_rank, 2);
97  BOOST_REQUIRE_EQUAL(fourthTable[2].sequence_id, 42);
98  BOOST_REQUIRE_EQUAL(fourthTable[3].destination_rank, 3);
99  BOOST_REQUIRE_EQUAL(fourthTable[3].sequence_id, 43);
100  BOOST_REQUIRE_EQUAL(fourthTable[4].destination_rank, 4);
101  BOOST_REQUIRE_EQUAL(fourthTable[4].sequence_id, 44);
102  BOOST_REQUIRE_EQUAL(fourthTable[5].destination_rank, 4);
103  BOOST_REQUIRE_EQUAL(fourthTable[5].sequence_id, 45);
104 
105  ct->AddReceiverToken(3, 1);
106  ct->ResetTokensUsedSinceLastUpdate();
107  auto fifthTable = ct->GetCurrentTable();
108  BOOST_REQUIRE_EQUAL(fifthTable.size(), 4);
109  BOOST_REQUIRE_EQUAL(fifthTable[0].destination_rank, 3);
110  BOOST_REQUIRE_EQUAL(fifthTable[0].sequence_id, 46);
111  BOOST_REQUIRE_EQUAL(fifthTable[1].destination_rank, 4);
112  BOOST_REQUIRE_EQUAL(fifthTable[1].sequence_id, 47);
113  BOOST_REQUIRE_EQUAL(fifthTable[2].destination_rank, 4);
114  BOOST_REQUIRE_EQUAL(fifthTable[2].sequence_id, 48);
115  BOOST_REQUIRE_EQUAL(fifthTable[3].destination_rank, 4);
116  BOOST_REQUIRE_EQUAL(fifthTable[3].sequence_id, 49);
117 
118  ct->Reset();
119  ct->AddReceiverToken(1, 2);
120  ct->ResetTokensUsedSinceLastUpdate();
121  auto sixthTable = ct->GetCurrentTable();
122  BOOST_REQUIRE_EQUAL(sixthTable.size(), 1);
123  BOOST_REQUIRE_EQUAL(sixthTable[0].destination_rank, 1);
124  BOOST_REQUIRE_EQUAL(sixthTable[0].sequence_id, 1);
125  TLOG(TLVL_INFO) << "CapacityTest_policy_t Test Case Simple END";
126 }
127 
128 BOOST_AUTO_TEST_CASE(DataFlowMode)
129 {
130  TLOG(TLVL_INFO) << "CapacityTest_policy_t Test Case DataFlowMode BEGIN";
131  fhicl::ParameterSet ps = artdaq::make_pset("routing_manager_mode: DataFlow");
132 
133  auto ct = artdaq::makeRoutingManagerPolicy("CapacityTest", ps);
134 
135 
136  ct->Reset();
137  ct->AddReceiverToken(1, 3);
138  ct->AddReceiverToken(2, 3);
139  BOOST_REQUIRE_EQUAL(ct->GetReceiverCount(), 2);
140  auto route = ct->GetRouteForSequenceID(1, 4);
141  BOOST_REQUIRE_EQUAL(route.destination_rank, 1);
142  BOOST_REQUIRE_EQUAL(route.sequence_id, 1);
143 
144  // Multiple hits for the same sequence ID are allowed, and should receive different information
145  route = ct->GetRouteForSequenceID(1, 5);
146  BOOST_REQUIRE_EQUAL(route.destination_rank, 1);
147  BOOST_REQUIRE_EQUAL(route.sequence_id, 1);
148 
149  // Except, that the same sequence ID from the same host should always get the same info
150  route = ct->GetRouteForSequenceID(1, 5);
151  BOOST_REQUIRE_EQUAL(route.destination_rank, 1);
152  BOOST_REQUIRE_EQUAL(route.sequence_id, 1);
153 
154  route = ct->GetRouteForSequenceID(2, 4);
155  BOOST_REQUIRE_EQUAL(route.destination_rank, 1);
156  BOOST_REQUIRE_EQUAL(route.sequence_id, 2);
157 
158  route = ct->GetRouteForSequenceID(2, 5);
159  BOOST_REQUIRE_EQUAL(route.destination_rank, 2);
160 
161  ct->AddReceiverToken(1, 1);
162  route = ct->GetRouteForSequenceID(2, 5);
163  BOOST_REQUIRE_EQUAL(route.destination_rank, 2);
164  BOOST_REQUIRE_EQUAL(route.sequence_id, 2);
165 
166  // Out-of-order sequence IDs are allowed
167  route = ct->GetRouteForSequenceID(1, 6);
168  BOOST_REQUIRE_EQUAL(route.destination_rank, 1);
169  BOOST_REQUIRE_EQUAL(route.sequence_id, 1);
170 
171  // Arbitrary sequence IDs are allowed
172  route = ct->GetRouteForSequenceID(10343, 4);
173  BOOST_REQUIRE_EQUAL(route.destination_rank, 2);
174  BOOST_REQUIRE_EQUAL(route.sequence_id, 10343);
175 
176  TLOG(TLVL_INFO) << "CapacityTest_policy_t Test Case DataFlowMode END";
177 }
178 
179 BOOST_AUTO_TEST_CASE(RequestBasedEventBuilding)
180 {
181  TLOG(TLVL_INFO) << "CapacityTest_policy_t Test Case RequestBasedEventBuilding BEGIN";
182  fhicl::ParameterSet ps = artdaq::make_pset("routing_manager_mode: RequestBasedEventBuilding routing_cache_size: 2");
183 
184  auto ct = artdaq::makeRoutingManagerPolicy("CapacityTest", ps);
185 
186 
187  ct->Reset();
188  ct->AddReceiverToken(1, 3);
189  ct->AddReceiverToken(2, 3);
190  BOOST_REQUIRE_EQUAL(ct->GetReceiverCount(), 2);
191 
192  auto route = ct->GetRouteForSequenceID(1, 4);
193  BOOST_REQUIRE_EQUAL(route.destination_rank, 1);
194  BOOST_REQUIRE_EQUAL(route.sequence_id, 1);
195 
196  // Multiple hits for the same sequence ID should receive the same routing
197  route = ct->GetRouteForSequenceID(1, 5);
198  BOOST_REQUIRE_EQUAL(route.destination_rank, 1);
199  BOOST_REQUIRE_EQUAL(route.sequence_id, 1);
200 
201  // Only events which have started routing should be in the table
202  auto firstTable = ct->GetCurrentTable();
203  BOOST_REQUIRE_EQUAL(firstTable.size(), 1);
204  BOOST_REQUIRE_EQUAL(firstTable[0].destination_rank, 1);
205 
206  // Arbitrary Sequence IDs are allowed
207  route = ct->GetRouteForSequenceID(12343, 4);
208  BOOST_REQUIRE_EQUAL(route.destination_rank, 1);
209  BOOST_REQUIRE_EQUAL(route.sequence_id, 12343);
210 
211  // Out-of-order Sequence IDs are allowed
212  route = ct->GetRouteForSequenceID(4, 5);
213  BOOST_REQUIRE_EQUAL(route.destination_rank, 1);
214  BOOST_REQUIRE_EQUAL(route.sequence_id, 4);
215 
216  // Requests that arrive late still get the same info
217  route = ct->GetRouteForSequenceID(1, 6);
218  BOOST_REQUIRE_EQUAL(route.destination_rank, 1);
219  BOOST_REQUIRE_EQUAL(route.sequence_id, 1);
220 
221  // Check that things behave when tokens are exhausted...
222  route = ct->GetRouteForSequenceID(10, 4);
223  BOOST_REQUIRE_EQUAL(route.destination_rank, 2);
224  BOOST_REQUIRE_EQUAL(route.sequence_id, 10);
225  route = ct->GetRouteForSequenceID(11, 4);
226  BOOST_REQUIRE_EQUAL(route.destination_rank, 2);
227  BOOST_REQUIRE_EQUAL(route.sequence_id, 11);
228  route = ct->GetRouteForSequenceID(12, 4);
229  BOOST_REQUIRE_EQUAL(route.destination_rank, 2);
230  BOOST_REQUIRE_EQUAL(route.sequence_id, 12);
231 
232  route = ct->GetRouteForSequenceID(50, 4);
233  BOOST_REQUIRE_EQUAL(route.destination_rank, -1);
234 
235  ct->AddReceiverToken(1, 1);
236  route = ct->GetRouteForSequenceID(50, 4);
237  BOOST_REQUIRE_EQUAL(route.destination_rank, 1);
238  BOOST_REQUIRE_EQUAL(route.sequence_id, 50);
239 
240  route = ct->GetRouteForSequenceID(50, 5);
241  BOOST_REQUIRE_EQUAL(route.destination_rank, 1);
242  BOOST_REQUIRE_EQUAL(route.sequence_id, 50);
243 
244  // Routing cache is sorted by sequence ID
245  auto secondTable = ct->GetCurrentTable();
246  BOOST_REQUIRE_EQUAL(secondTable.size(), 6);
247  BOOST_REQUIRE_EQUAL(secondTable[0].destination_rank, 1);
248  BOOST_REQUIRE_EQUAL(secondTable[0].sequence_id, 4);
249  BOOST_REQUIRE_EQUAL(secondTable[1].destination_rank, 2);
250  BOOST_REQUIRE_EQUAL(secondTable[1].sequence_id, 10);
251  BOOST_REQUIRE_EQUAL(secondTable[2].destination_rank, 2);
252  BOOST_REQUIRE_EQUAL(secondTable[2].sequence_id, 11);
253  BOOST_REQUIRE_EQUAL(secondTable[3].destination_rank, 2);
254  BOOST_REQUIRE_EQUAL(secondTable[3].sequence_id, 12);
255  BOOST_REQUIRE_EQUAL(secondTable[4].destination_rank, 1);
256  BOOST_REQUIRE_EQUAL(secondTable[4].sequence_id, 50);
257  BOOST_REQUIRE_EQUAL(secondTable[5].destination_rank, 1);
258  BOOST_REQUIRE_EQUAL(secondTable[5].sequence_id, 12343);
259 
260  // Since the routing cache has been set to 2, only the highest two events routed are here, as the cache is checked when generating tables
261  BOOST_REQUIRE_EQUAL(ct->GetCacheSize(), 2);
262  auto thirdTable = ct->GetCurrentTable();
263  BOOST_REQUIRE_EQUAL(thirdTable.size(), 0);
264 
265  BOOST_REQUIRE(ct->CacheHasRoute(50));
266  BOOST_REQUIRE(!ct->CacheHasRoute(4));
267  route = ct->GetRouteForSequenceID(50, 6);
268  BOOST_REQUIRE_EQUAL(route.destination_rank, 1);
269  BOOST_REQUIRE_EQUAL(route.sequence_id, 50);
270 
271  TLOG(TLVL_INFO) << "CapacityTest_policy_t Test Case RequestBasedEventBuilding END";
272 }
273 
274 BOOST_AUTO_TEST_SUITE_END()
std::shared_ptr< RoutingManagerPolicy > makeRoutingManagerPolicy(std::string const &policy_plugin_spec, fhicl::ParameterSet const &ps)
Load a RoutingManagerPolicy plugin.