artdaq  v3_12_02
CapacityTest_policy_t.cc
1 #define BOOST_TEST_MODULE CapacityTest_policy_t
2 #include <boost/test/unit_test.hpp>
3 
4 #include "TRACE/tracemf.h"
5 
6 #include "artdaq/RoutingPolicies/RoutingManagerPolicy.hh"
7 #include "artdaq/RoutingPolicies/makeRoutingManagerPolicy.hh"
8 #include "fhiclcpp/ParameterSet.h"
9 
10 BOOST_AUTO_TEST_SUITE(CapacityTest_policy_t)
11 
12 BOOST_AUTO_TEST_CASE(Simple)
13 {
14  TLOG(TLVL_INFO) << "CapacityTest_policy_t Test Case Simple BEGIN";
15  fhicl::ParameterSet ps = fhicl::ParameterSet::make("tokens_used_per_table_percent: 50");
16 
17  auto ct = artdaq::makeRoutingManagerPolicy("CapacityTest", ps);
18 
19  ct->AddReceiverToken(1, 10);
20  ct->AddReceiverToken(2, 10);
21  ct->AddReceiverToken(3, 10);
22  ct->AddReceiverToken(4, 10);
23  BOOST_REQUIRE_EQUAL(ct->GetReceiverCount(), 4);
24 
25  auto firstTable = ct->GetCurrentTable();
26  BOOST_REQUIRE_EQUAL(firstTable.size(), 20);
27  BOOST_REQUIRE_EQUAL(firstTable[0].destination_rank, 1);
28  BOOST_REQUIRE_EQUAL(firstTable[0].sequence_id, 1);
29  BOOST_REQUIRE_EQUAL(firstTable[firstTable.size() - 1].destination_rank, 2);
30  BOOST_REQUIRE_EQUAL(firstTable[firstTable.size() - 1].sequence_id, 20);
31 
32  ct->ResetTokensUsedSinceLastUpdate();
33 
34  // ct->Reset();
35  ct->AddReceiverToken(1, 1);
36  ct->AddReceiverToken(3, 1);
37  ct->AddReceiverToken(2, 1);
38  ct->AddReceiverToken(4, 1);
39  ct->AddReceiverToken(2, 1);
40  auto secondTable = ct->GetCurrentTable();
41  BOOST_REQUIRE_EQUAL(secondTable.size(), 13);
42  BOOST_REQUIRE_EQUAL(secondTable[0].destination_rank, 1);
43  BOOST_REQUIRE_EQUAL(secondTable[0].sequence_id, 21);
44  BOOST_REQUIRE_EQUAL(secondTable[1].destination_rank, 2);
45  BOOST_REQUIRE_EQUAL(secondTable[1].sequence_id, 22);
46  BOOST_REQUIRE_EQUAL(secondTable[2].destination_rank, 2);
47  BOOST_REQUIRE_EQUAL(secondTable[2].sequence_id, 23);
48  BOOST_REQUIRE_EQUAL(secondTable[3].destination_rank, 3);
49  BOOST_REQUIRE_EQUAL(secondTable[3].sequence_id, 24);
50  BOOST_REQUIRE_EQUAL(secondTable[4].destination_rank, 3);
51  BOOST_REQUIRE_EQUAL(secondTable[4].sequence_id, 25);
52  BOOST_REQUIRE_EQUAL(secondTable[5].destination_rank, 3);
53  BOOST_REQUIRE_EQUAL(secondTable[5].sequence_id, 26);
54  BOOST_REQUIRE_EQUAL(secondTable[6].destination_rank, 3);
55  BOOST_REQUIRE_EQUAL(secondTable[6].sequence_id, 27);
56  BOOST_REQUIRE_EQUAL(secondTable[7].destination_rank, 3);
57  BOOST_REQUIRE_EQUAL(secondTable[7].sequence_id, 28);
58  BOOST_REQUIRE_EQUAL(secondTable[8].destination_rank, 3);
59  BOOST_REQUIRE_EQUAL(secondTable[8].sequence_id, 29);
60  BOOST_REQUIRE_EQUAL(secondTable[9].destination_rank, 3);
61  BOOST_REQUIRE_EQUAL(secondTable[9].sequence_id, 30);
62  BOOST_REQUIRE_EQUAL(secondTable[10].destination_rank, 3);
63  BOOST_REQUIRE_EQUAL(secondTable[10].sequence_id, 31);
64  BOOST_REQUIRE_EQUAL(secondTable[11].destination_rank, 3);
65  BOOST_REQUIRE_EQUAL(secondTable[11].sequence_id, 32);
66  BOOST_REQUIRE_EQUAL(secondTable[12].destination_rank, 3);
67  BOOST_REQUIRE_EQUAL(secondTable[12].sequence_id, 33);
68 
69  ct->ResetTokensUsedSinceLastUpdate();
70  // ct->Reset();
71  ct->AddReceiverToken(1, 0);
72  auto thirdTable = ct->GetCurrentTable();
73  BOOST_REQUIRE_EQUAL(thirdTable.size(), 6);
74  BOOST_REQUIRE_EQUAL(thirdTable[0].destination_rank, 3);
75  BOOST_REQUIRE_EQUAL(thirdTable[0].sequence_id, 34);
76  BOOST_REQUIRE_EQUAL(thirdTable[1].destination_rank, 4);
77  BOOST_REQUIRE_EQUAL(thirdTable[1].sequence_id, 35);
78  BOOST_REQUIRE_EQUAL(thirdTable[2].destination_rank, 4);
79  BOOST_REQUIRE_EQUAL(thirdTable[2].sequence_id, 36);
80  BOOST_REQUIRE_EQUAL(thirdTable[3].destination_rank, 4);
81  BOOST_REQUIRE_EQUAL(thirdTable[3].sequence_id, 37);
82  BOOST_REQUIRE_EQUAL(thirdTable[4].destination_rank, 4);
83  BOOST_REQUIRE_EQUAL(thirdTable[4].sequence_id, 38);
84  BOOST_REQUIRE_EQUAL(thirdTable[5].destination_rank, 4);
85  BOOST_REQUIRE_EQUAL(thirdTable[5].sequence_id, 39);
86 
87  ct->AddReceiverToken(1, 2);
88  ct->AddReceiverToken(2, 1);
89  ct->AddReceiverToken(3, 1);
90  ct->AddReceiverToken(4, 2);
91  ct->ResetTokensUsedSinceLastUpdate();
92  auto fourthTable = ct->GetCurrentTable();
93  BOOST_REQUIRE_EQUAL(fourthTable.size(), 6);
94  BOOST_REQUIRE_EQUAL(fourthTable[0].destination_rank, 1);
95  BOOST_REQUIRE_EQUAL(fourthTable[0].sequence_id, 40);
96  BOOST_REQUIRE_EQUAL(fourthTable[1].destination_rank, 1);
97  BOOST_REQUIRE_EQUAL(fourthTable[1].sequence_id, 41);
98  BOOST_REQUIRE_EQUAL(fourthTable[2].destination_rank, 2);
99  BOOST_REQUIRE_EQUAL(fourthTable[2].sequence_id, 42);
100  BOOST_REQUIRE_EQUAL(fourthTable[3].destination_rank, 3);
101  BOOST_REQUIRE_EQUAL(fourthTable[3].sequence_id, 43);
102  BOOST_REQUIRE_EQUAL(fourthTable[4].destination_rank, 4);
103  BOOST_REQUIRE_EQUAL(fourthTable[4].sequence_id, 44);
104  BOOST_REQUIRE_EQUAL(fourthTable[5].destination_rank, 4);
105  BOOST_REQUIRE_EQUAL(fourthTable[5].sequence_id, 45);
106 
107  ct->AddReceiverToken(3, 1);
108  ct->ResetTokensUsedSinceLastUpdate();
109  auto fifthTable = ct->GetCurrentTable();
110  BOOST_REQUIRE_EQUAL(fifthTable.size(), 4);
111  BOOST_REQUIRE_EQUAL(fifthTable[0].destination_rank, 3);
112  BOOST_REQUIRE_EQUAL(fifthTable[0].sequence_id, 46);
113  BOOST_REQUIRE_EQUAL(fifthTable[1].destination_rank, 4);
114  BOOST_REQUIRE_EQUAL(fifthTable[1].sequence_id, 47);
115  BOOST_REQUIRE_EQUAL(fifthTable[2].destination_rank, 4);
116  BOOST_REQUIRE_EQUAL(fifthTable[2].sequence_id, 48);
117  BOOST_REQUIRE_EQUAL(fifthTable[3].destination_rank, 4);
118  BOOST_REQUIRE_EQUAL(fifthTable[3].sequence_id, 49);
119 
120  ct->Reset();
121  ct->AddReceiverToken(1, 2);
122  ct->ResetTokensUsedSinceLastUpdate();
123  auto sixthTable = ct->GetCurrentTable();
124  BOOST_REQUIRE_EQUAL(sixthTable.size(), 1);
125  BOOST_REQUIRE_EQUAL(sixthTable[0].destination_rank, 1);
126  BOOST_REQUIRE_EQUAL(sixthTable[0].sequence_id, 1);
127  TLOG(TLVL_INFO) << "CapacityTest_policy_t Test Case Simple END";
128 }
129 
130 BOOST_AUTO_TEST_CASE(DataFlowMode)
131 {
132  TLOG(TLVL_INFO) << "CapacityTest_policy_t Test Case DataFlowMode BEGIN";
133  fhicl::ParameterSet ps = fhicl::ParameterSet::make("routing_manager_mode: DataFlow");
134 
135  auto ct = artdaq::makeRoutingManagerPolicy("CapacityTest", ps);
136 
137  ct->Reset();
138  ct->AddReceiverToken(1, 3);
139  ct->AddReceiverToken(2, 3);
140  BOOST_REQUIRE_EQUAL(ct->GetReceiverCount(), 2);
141  auto route = ct->GetRouteForSequenceID(1, 4);
142  BOOST_REQUIRE_EQUAL(route.destination_rank, 1);
143  BOOST_REQUIRE_EQUAL(route.sequence_id, 1);
144 
145  // Multiple hits for the same sequence ID are allowed, and should receive different information
146  route = ct->GetRouteForSequenceID(1, 5);
147  BOOST_REQUIRE_EQUAL(route.destination_rank, 1);
148  BOOST_REQUIRE_EQUAL(route.sequence_id, 1);
149 
150  // Except, that the same sequence ID from the same host should always get the same info
151  route = ct->GetRouteForSequenceID(1, 5);
152  BOOST_REQUIRE_EQUAL(route.destination_rank, 1);
153  BOOST_REQUIRE_EQUAL(route.sequence_id, 1);
154 
155  route = ct->GetRouteForSequenceID(2, 4);
156  BOOST_REQUIRE_EQUAL(route.destination_rank, 1);
157  BOOST_REQUIRE_EQUAL(route.sequence_id, 2);
158 
159  route = ct->GetRouteForSequenceID(2, 5);
160  BOOST_REQUIRE_EQUAL(route.destination_rank, 2);
161 
162  ct->AddReceiverToken(1, 1);
163  route = ct->GetRouteForSequenceID(2, 5);
164  BOOST_REQUIRE_EQUAL(route.destination_rank, 2);
165  BOOST_REQUIRE_EQUAL(route.sequence_id, 2);
166 
167  // Out-of-order sequence IDs are allowed
168  route = ct->GetRouteForSequenceID(1, 6);
169  BOOST_REQUIRE_EQUAL(route.destination_rank, 1);
170  BOOST_REQUIRE_EQUAL(route.sequence_id, 1);
171 
172  // Arbitrary sequence IDs are allowed
173  route = ct->GetRouteForSequenceID(10343, 4);
174  BOOST_REQUIRE_EQUAL(route.destination_rank, 2);
175  BOOST_REQUIRE_EQUAL(route.sequence_id, 10343);
176 
177  TLOG(TLVL_INFO) << "CapacityTest_policy_t Test Case DataFlowMode END";
178 }
179 
180 BOOST_AUTO_TEST_CASE(RequestBasedEventBuilding)
181 {
182  TLOG(TLVL_INFO) << "CapacityTest_policy_t Test Case RequestBasedEventBuilding BEGIN";
183  fhicl::ParameterSet ps = fhicl::ParameterSet::make("routing_manager_mode: RequestBasedEventBuilding routing_cache_size: 2");
184 
185  auto ct = artdaq::makeRoutingManagerPolicy("CapacityTest", ps);
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.