Line data Source code
1 : //
2 : // DUNE DAQ modification notice:
3 : // This file has been modified from the original ATLAS config source for the DUNE DAQ project.
4 : // Fork baseline commit: 67a24e731 (2022-10-27).
5 : // Renamed since fork: yes (from test/config_time_test.cpp to test/apps/config_time_test.cxx).
6 : //
7 :
8 : #include <stdlib.h>
9 : #include <sys/resource.h>
10 : #include <time.h>
11 :
12 : #include <chrono>
13 : #include <fstream>
14 : #include <iostream>
15 : #include <string>
16 :
17 : #include "conffwk/ConfigObject.hpp"
18 : #include "conffwk/Configuration.hpp"
19 : #include "conffwk/Schema.hpp"
20 :
21 : using namespace dunedaq::conffwk;
22 :
23 0 : ERS_DECLARE_ISSUE(conffwk_time_test, BadCommandLine, "bad command line: " << reason, ((const char*)reason))
24 :
25 0 : ERS_DECLARE_ISSUE(conffwk_time_test, ConfigException, "caught dunedaq::conffwk::Exception exception", )
26 :
27 : static void
28 0 : no_param(const char* s)
29 : {
30 0 : std::ostringstream text;
31 0 : text << "no parameter for " << s << " provided";
32 0 : ers::fatal(conffwk_time_test::BadCommandLine(ERS_HERE, text.str().c_str()));
33 0 : exit(EXIT_FAILURE);
34 0 : }
35 :
36 : template<class T>
37 : void
38 0 : stop_and_report(T& tp, const char* fname)
39 : {
40 0 : std::cout << "TEST \"" << fname << "\" => "
41 0 : << std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - tp).count() /
42 : 1000.
43 0 : << " ms\n";
44 0 : }
45 :
46 : int
47 0 : main(int argc, char* argv[])
48 : {
49 0 : const char* db_name = 0;
50 0 : bool verbose = false;
51 :
52 0 : for (int i = 1; i < argc; i++) {
53 0 : const char* cp = argv[i];
54 :
55 0 : if (!strcmp(cp, "-h") || !strcmp(cp, "--help")) {
56 0 : std::cout << "Usage: conffwk_time_test -d dbspec [-c | -C [class_name]] [-o | -O [object_id]] [-n]\n"
57 : "\n"
58 : "Options/Arguments:\n"
59 : " -d | --database dbspec database specification in format plugin-name:parameters\n"
60 : " -v | --verbose print details\n"
61 : "\n"
62 : "Description:\n"
63 : " The utility reports results of time tests.\n"
64 0 : " When no -c or -o options are provided, utility lists all classes.\n\n";
65 0 : } else if (!strcmp(cp, "-d") || !strcmp(cp, "--database")) {
66 0 : if (++i == argc) {
67 0 : no_param(cp);
68 : } else {
69 0 : db_name = argv[i];
70 : }
71 0 : } else if (!strcmp(cp, "-v") || !strcmp(cp, "--verbose")) {
72 0 : verbose = true;
73 : }
74 : }
75 :
76 0 : if (!db_name) {
77 0 : ers::fatal(conffwk_time_test::BadCommandLine(ERS_HERE, "no database name given"));
78 0 : return (EXIT_FAILURE);
79 : }
80 :
81 0 : try {
82 :
83 : ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
84 :
85 0 : auto tp = std::chrono::steady_clock::now();
86 :
87 0 : Configuration conf(db_name);
88 :
89 0 : if (verbose) {
90 0 : std::cout << "load database \"" << conf.get_impl_spec() << '\"' << std::endl;
91 : }
92 :
93 0 : stop_and_report(tp, "loading database");
94 :
95 : ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
96 :
97 0 : tp = std::chrono::steady_clock::now();
98 :
99 0 : std::set<std::string> classes;
100 :
101 0 : for (fmap<fset>::const_iterator i = conf.superclasses().begin(); i != conf.superclasses().end(); ++i) {
102 0 : classes.insert(*i->first);
103 : }
104 :
105 0 : if (verbose) {
106 0 : std::cout << "The database schema has " << classes.size() << " class(es):\n";
107 : }
108 :
109 0 : for (std::set<std::string>::const_iterator i = classes.begin(); i != classes.end(); ++i) {
110 0 : const dunedaq::conffwk::class_t& d(conf.get_class_info(*i));
111 0 : if (verbose) {
112 0 : d.print(std::cout, " ");
113 : }
114 : }
115 :
116 0 : stop_and_report(tp, "reading schema meta-information");
117 :
118 : ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
119 :
120 0 : tp = std::chrono::steady_clock::now();
121 :
122 0 : std::vector<ConfigObject> all_objects;
123 :
124 0 : for (std::set<std::string>::const_iterator i = classes.begin(); i != classes.end(); ++i) {
125 0 : std::vector<ConfigObject> objects;
126 0 : conf.get(*i, objects);
127 :
128 0 : unsigned int count = 0;
129 :
130 0 : for (std::vector<ConfigObject>::const_iterator j = objects.begin(); j != objects.end(); ++j) {
131 0 : if ((*j).class_name() == *i) {
132 0 : count++;
133 0 : all_objects.push_back(*j);
134 : }
135 : }
136 :
137 0 : if (verbose) {
138 0 : std::cout << "Class " << *i << " has " << count << " objects (" << objects.size() << " with derived classes)\n";
139 : }
140 0 : }
141 :
142 0 : if (verbose) {
143 0 : std::cout << "Total number of objects: " << all_objects.size() << std::endl;
144 : }
145 :
146 0 : stop_and_report(tp, "reading names of objects");
147 :
148 : ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
149 :
150 0 : tp = std::chrono::steady_clock::now();
151 :
152 0 : std::set<std::string> files;
153 :
154 0 : for (std::vector<ConfigObject>::const_iterator i = all_objects.begin(); i != all_objects.end(); ++i) {
155 0 : files.insert((*i).contained_in());
156 : }
157 :
158 0 : if (verbose) {
159 0 : std::cout << "There are " << files.size() << " data files:\n";
160 :
161 0 : for (std::set<std::string>::const_iterator i = files.begin(); i != files.end(); ++i) {
162 0 : std::cout << " - \"" << *i << "\"\n";
163 : }
164 : }
165 :
166 0 : stop_and_report(tp, "reading names of files");
167 :
168 : ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
169 :
170 0 : tp = std::chrono::steady_clock::now();
171 :
172 0 : if (verbose) {
173 0 : std::cout << "Details of objects:\n";
174 :
175 0 : for (std::vector<ConfigObject>::const_iterator i = all_objects.begin(); i != all_objects.end(); ++i) {
176 0 : (*i).print_ref(std::cout, conf, " ");
177 : }
178 : } else {
179 0 : std::ofstream null("/dev/null", std::ios::out);
180 0 : for (std::vector<ConfigObject>::const_iterator i = all_objects.begin(); i != all_objects.end(); ++i) {
181 0 : (*i).print_ref(null, conf);
182 : }
183 0 : }
184 :
185 0 : stop_and_report(tp, "reading all attributes and relationships");
186 :
187 : ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
188 :
189 0 : return 0;
190 0 : } catch (dunedaq::conffwk::Exception& ex) {
191 0 : ers::fatal(conffwk_time_test::ConfigException(ERS_HERE, ex));
192 0 : }
193 :
194 0 : return (EXIT_FAILURE);
195 : }
|