Project
Loading...
Searching...
No Matches
test_DataSamplingPolicy.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11#define BOOST_TEST_MODULE Test Framework DataSamplingPolicy
12#define BOOST_TEST_MAIN
13#define BOOST_TEST_DYN_LINK
14
15#include <boost/test/unit_test.hpp>
16#include <boost/property_tree/ptree.hpp>
17
20#include "Framework/DataRef.h"
22
23using namespace o2::framework;
24using namespace o2::utilities;
25using namespace o2::header;
26
27// an example of DataSamplingPolicy JSON object
28// {
29// "id" : "policy_example1",
30// "active" : "false",
31// "machines" : [
32// "aidlalala1",
33// "aidlalala2"
34// ]
35// "query" : "c:TST/CHLEB/33;m:TST/MLEKO/33",
36// "outputs" : "cc:TST/CHLEB_S/33;mm:TST/MLEKO_S/33", "": "this is optional, if not specified, default format is used",
37// "samplingConditions" : [
38// {
39// "condition" : "random",
40// "fraction" : "0.1",
41// "seed" : "2137"
42// }
43// ],
44// "blocking" : "false"
45// }
46
47BOOST_AUTO_TEST_CASE(DataSamplingPolicyFromConfiguration)
48{
49 using boost::property_tree::ptree;
50
51 ptree config;
52 config.put("id", "my_policy");
53 config.put("active", "true");
54 config.put("query", "c:TST/CHLEB/33;m:TST/MLEKO/33");
55 ptree samplingConditions;
56 ptree conditionRandom;
57 conditionRandom.put("condition", "random");
58 conditionRandom.put("fraction", "0.1");
59 conditionRandom.put("seed", "2137");
60 samplingConditions.push_back(std::make_pair("", conditionRandom));
61 config.add_child("samplingConditions", samplingConditions);
62 config.put("blocking", "false");
63
64 {
65 auto policy = std::move(DataSamplingPolicy::fromConfiguration(config));
66
67 BOOST_CHECK_EQUAL(policy.getName(), "my_policy");
68 BOOST_CHECK((policy.prepareOutput(ConcreteDataMatcher{"TST", "CHLEB", 33})) == (Output{"DS", "my_policy0", 33}));
69 BOOST_CHECK((policy.prepareOutput(ConcreteDataMatcher{"TST", "MLEKO", 33})) == (Output{"DS", "my_policy1", 33}));
70 const auto& map = policy.getPathMap();
71 BOOST_CHECK_EQUAL((*map.find(ConcreteDataMatcher{"TST", "CHLEB", 33})).second, (OutputSpec{"DS", "my_policy0", 33, Lifetime::QA}));
72 BOOST_CHECK_EQUAL((*map.find(ConcreteDataMatcher{"TST", "MLEKO", 33})).second, (OutputSpec{"DS", "my_policy1", 33, Lifetime::QA}));
73 BOOST_CHECK_EQUAL(map.size(), 2);
74
75 BOOST_CHECK(policy.match(ConcreteDataMatcher{"TST", "CHLEB", 33}));
76 BOOST_CHECK(!policy.match(ConcreteDataMatcher{"TST", "SZYNKA", 33}));
77
78 DataProcessingHeader dph{555, 0};
79 o2::header::Stack headerStack{dph};
80 DataRef dr{nullptr, reinterpret_cast<const char*>(headerStack.data()), nullptr};
81 policy.decide(dr); // just make sure it does not crash
82 }
83
84 config.put("id", "too-long-policy-name");
85 {
86 auto policy = std::move(DataSamplingPolicy::fromConfiguration(config));
87 BOOST_CHECK_EQUAL(policy.getName(), "too-long-policy-name");
88 BOOST_CHECK((policy.prepareOutput(ConcreteDataMatcher{"TST", "CHLEB", 33})) == (Output{"DS", "too-long-polic0", 33}));
89 BOOST_CHECK((policy.prepareOutput(ConcreteDataMatcher{"TST", "MLEKO", 33})) == (Output{"DS", "too-long-polic1", 33}));
90 }
91
92 // with custom outputs
93 config.put("outputs", "cc:TST/CHLEB_S/33;mm:TST/MLEKO_S/33");
94 {
95 auto policy = std::move(DataSamplingPolicy::fromConfiguration(config));
96
97 BOOST_CHECK((policy.prepareOutput(ConcreteDataMatcher{"TST", "CHLEB", 33})) == (Output{"TST", "CHLEB_S", 33}));
98 BOOST_CHECK((policy.prepareOutput(ConcreteDataMatcher{"TST", "MLEKO", 33})) == (Output{"TST", "MLEKO_S", 33}));
99 const auto& map = policy.getPathMap();
100 BOOST_CHECK_EQUAL((*map.find(ConcreteDataMatcher{"TST", "CHLEB", 33})).second, (OutputSpec{"TST", "CHLEB_S", 33, Lifetime::QA}));
101 BOOST_CHECK_EQUAL((*map.find(ConcreteDataMatcher{"TST", "MLEKO", 33})).second, (OutputSpec{"TST", "MLEKO_S", 33, Lifetime::QA}));
102 BOOST_CHECK_EQUAL(map.size(), 2);
103 }
104 // with custom outputs which are wildcards
105 config.put("outputs", "cc:TST/CHLEB_S;mm:TST/MLEKO_S");
106 {
107 auto policy = std::move(DataSamplingPolicy::fromConfiguration(config));
108
109 BOOST_CHECK((policy.prepareOutput(ConcreteDataMatcher{"TST", "CHLEB", 33})) == (Output{"TST", "CHLEB_S", 33}));
110 BOOST_CHECK((policy.prepareOutput(ConcreteDataMatcher{"TST", "MLEKO", 33})) == (Output{"TST", "MLEKO_S", 33}));
111 const auto& map = policy.getPathMap();
112 BOOST_CHECK_EQUAL((*map.find(ConcreteDataMatcher{"TST", "CHLEB", 33})).second, (OutputSpec{{"TST", "CHLEB_S"}, Lifetime::QA}));
113 BOOST_CHECK_EQUAL((*map.find(ConcreteDataMatcher{"TST", "MLEKO", 33})).second, (OutputSpec{{"TST", "MLEKO_S"}, Lifetime::QA}));
114 BOOST_CHECK_EQUAL(map.size(), 2);
115 }
116}
117
118BOOST_AUTO_TEST_CASE(DataSamplingPolicyFromMethods)
119{
120 DataSamplingPolicy policy("my_policy");
121 auto conditionNConsecutive = DataSamplingConditionFactory::create("nConsecutive");
122 BOOST_REQUIRE(conditionNConsecutive);
123
124 boost::property_tree::ptree config;
125 config.put("samplesNumber", 3);
126 config.put("cycleSize", 10);
127 conditionNConsecutive->configure(config);
128
129 policy.registerCondition(std::move(conditionNConsecutive));
130
131 policy.registerPath({"tststs", {"TST", "CHLEB"}}, {{"asdf"}, "AA", "BBBB"});
132 BOOST_CHECK((policy.prepareOutput(ConcreteDataMatcher{"TST", "CHLEB", 33})) == (Output{"AA", "BBBB", 33}));
133}
134
135BOOST_AUTO_TEST_CASE(DataSamplingPolicyStaticMethods)
136{
139 BOOST_CHECK(DataSamplingPolicy::createPolicyDataDescription("asdfasdfasdfasdf", 0) == DataDescription("asdfasdfasdfas0"));
140 BOOST_CHECK(DataSamplingPolicy::createPolicyDataDescription("asdfasdfasdfasdf", 10) == DataDescription("asdfasdfasdfas10"));
141}
A definition of DataSamplingConditionFactory.
A declaration of O2 Data Sampling Policy.
static std::unique_ptr< DataSamplingCondition > create(std::string name)
Creates instance of DataSamplingCondition child, given the name.
static DataSamplingPolicy fromConfiguration(const boost::property_tree::ptree &)
Configures a policy using structured configuration entry.
static header::DataDescription createPolicyDataDescription(std::string policyName, size_t id)
void registerPath(const framework::InputSpec &, const framework::OutputSpec &)
Adds a new association between inputs and outputs.
void registerCondition(std::unique_ptr< DataSamplingCondition > &&)
Adds a new association between inputs and outputs.
static header::DataOrigin createPolicyDataOrigin()
framework::Output prepareOutput(const framework::ConcreteDataMatcher &input, framework::Lifetime lifetime=framework::Lifetime::Timeframe) const
Returns Output for given InputSpec to pass data forward.
Defining PrimaryVertex explicitly as messageable.
Definition TFIDInfo.h:20
O2 data header classes and API, v0.1.
Definition DetID.h:49
Descriptor< gSizeDataDescriptionString > DataDescription
Definition DataHeader.h:551
BOOST_AUTO_TEST_CASE(Descriptor_test)
Descriptor< gSizeDataOriginString > DataOrigin
Definition DataHeader.h:550
A header which contains some meta-data generated by Data Sampling.
a move-only header stack with serialized headers This is the flat buffer where all the headers in a m...
Definition Stack.h:36
BOOST_CHECK(tree)
BOOST_CHECK_EQUAL(triggersD.size(), triggers.size())