artdaq  v3_09_01
tracemf.cc
1 // This file (just.cc) was created by Ron Rechenmacher <ron@fnal.gov> on
2 // Feb 19, 2014. "TERMS AND CONDITIONS" governing this file are in the README
3 // or COPYING file. If you do not have such a file, one can be obtained by
4 // contacting Ron or Fermi Lab in Batavia IL, 60510, phone: 630-840-3000.
5 // $RCSfile: just_user.cc,v $
6 
7 #include "artdaq/DAQdata/Globals.hh"
8 
9 #include <boost/program_options.hpp>
10 
11 #include <iomanip>
12 namespace bpo = boost::program_options;
13 
14 std::string formatTime(double time)
15 {
16  std::ostringstream o;
17  o << std::fixed << std::setprecision(3);
18  if (time > 60)
19  {
20  o << static_cast<int>(time / 60) << " m " << time - 60 * static_cast<int>(time / 60) << " s";
21  }
22  else if (time > 1)
23  {
24  o << time << " s";
25  }
26  else
27  {
28  time *= 1000;
29  if (time > 1)
30  {
31  o << time << " ms";
32  }
33  else
34  {
35  time *= 1000;
36  if (time > 1)
37  {
38  o << time << " us";
39  }
40  else
41  {
42  time *= 1000;
43  o << time << " ns";
44  }
45  }
46  }
47 
48  return o.str();
49 }
50 
51 int main(int argc, char *argv[])
52 try
53 {
54  std::ostringstream descstr;
55  descstr << *argv
56  << " <-l test loops> [csutdi]";
57  bpo::options_description desc(descstr.str());
58  desc.add_options()("loops,l", bpo::value<size_t>(), "Number of times to run each test")("C,c", "Run TRACEC test")("S,s", "Run TRACES test")("U,u", "Run TRACE_ test")("T,t", "Run TRACE_STREAMER test")("D,d", "Run TLOG_DEBUG test")("I,i", "Run TLOG_INFO test")("console,x", "Enable MessageFacility output to console")("help,h", "produce help message");
59  bpo::variables_map vm;
60  try
61  {
62  bpo::store(bpo::command_line_parser(argc, argv).options(desc).run(), vm);
63  bpo::notify(vm);
64  }
65  catch (bpo::error const &e)
66  {
67  std::cerr << "Exception from command line processing in " << *argv
68  << ": " << e.what() << "\n";
69  return -1;
70  }
71  if (vm.count("help") != 0u)
72  {
73  std::cout << desc << std::endl;
74  return 1;
75  }
76  if (vm.count("loops") == 0u)
77  {
78  std::cerr << "Exception from command line processing in " << *argv
79  << ": no loop count given.\n"
80  << "For usage and an options list, please do '"
81  << *argv << " --help"
82  << "'.\n";
83  return 2;
84  }
85  size_t loops = vm["loops"].as<size_t>();
86 
87  artdaq::configureMessageFacility("tracemf", vm.count("console") != 0u);
88 
89  if (vm.count("C") != 0u)
90  {
91  std::cout << "Starting TRACEC test" << std::endl;
92  auto start = std::chrono::steady_clock::now();
93  for (size_t l = 0; l < loops; ++l)
94  {
95  TRACE(TLVL_DEBUG, "Test TRACEC with an int %i and a float %.1f", 42, 5.56); // NOLINT
96  }
97  auto time = std::chrono::duration_cast<std::chrono::duration<double>>(std::chrono::steady_clock::now() - start).count();
98  std::cout << "TRACEC test took " << formatTime(time) << ", avg: " << formatTime(time / loops) << std::endl;
99  }
100 
101  if (vm.count("S") != 0u)
102  {
103  std::cout << "Starting TRACES test" << std::endl;
104  auto start = std::chrono::steady_clock::now();
105  for (size_t l = 0; l < loops; ++l)
106  {
107  std::string test = "Test TRACE with an int %d";
108  std::string test2 = " and a float %.1f";
109  TRACE(TLVL_DEBUG, test + test2, 42, 5.56); // NOLINT
110  }
111  auto time = std::chrono::duration_cast<std::chrono::duration<double>>(std::chrono::steady_clock::now() - start).count();
112  std::cout << "TRACES test took " << formatTime(time) << ", avg: " << formatTime(time / loops) << std::endl;
113  }
114 
115  if (vm.count("U") != 0u)
116  {
117  std::cout << "Starting TRACE_ test" << std::endl;
118  auto start = std::chrono::steady_clock::now();
119  for (size_t l = 0; l < loops; ++l)
120  {
121  TRACEN_("tracemf", TLVL_DEBUG, "Test TRACE_ with an int " << 42 << " and a float %.1f", 5.56); // NOLINT
122  }
123  auto time = std::chrono::duration_cast<std::chrono::duration<double>>(std::chrono::steady_clock::now() - start).count();
124  std::cout << "TRACE_ test took " << formatTime(time) << ", avg: " << formatTime(time / loops) << std::endl;
125  }
126 
127  if (vm.count("D") != 0u)
128  {
129  std::cout << "Starting TLOG_DEBUG test" << std::endl;
130  auto start = std::chrono::steady_clock::now();
131  for (size_t l = 0; l < loops; ++l)
132  {
133  TLOG_DEBUG("tracemf") << "Test TLOG_DEBUG with an int " << 42 << " and a float " << std::setprecision(1) << 5.56 << ", and another float " << 7.3 << TRACE_ENDL;
134  }
135  auto time = std::chrono::duration_cast<std::chrono::duration<double>>(std::chrono::steady_clock::now() - start).count();
136  std::cout << "TLOG_DEBUG test took " << formatTime(time) << ", avg: " << formatTime(time / loops) << std::endl;
137  }
138 
139  if (vm.count("I") != 0u)
140  {
141  std::cout << "Starting TLOG_INFO test" << std::endl;
142  auto start = std::chrono::steady_clock::now();
143  for (size_t l = 0; l < loops; ++l)
144  {
145  TLOG_INFO("tracemf") << "Test TLOG_INFO with an int " << 42 << " and a float " << std::setprecision(1) << 5.56 << ", and another float " << std::fixed << 7.3 << TRACE_ENDL;
146  }
147  auto time = std::chrono::duration_cast<std::chrono::duration<double>>(std::chrono::steady_clock::now() - start).count();
148  std::cout << "TLOG_INFO test took " << formatTime(time) << ", avg: " << formatTime(time / loops) << std::endl;
149  }
150 
151  return (0);
152 }
153 catch (...)
154 {
155  return -1;
156 }