testbench.cpp
Go to the documentation of this file.
1 #include <cstdlib>
2 #include <cstring>
3 #include <cstdio>
4 #include <iostream>
5 
6 #include <log4cpp/Category.hh>
9 #include <log4cpp/BasicLayout.hh>
10 #include <log4cpp/PatternLayout.hh>
11 #include <log4cpp/TimeStamp.hh>
12 
13 #include "Clock.hh"
14 
15 
16 // -----------------------------------------------------------------------------
17 int main(int argc, char* argv[])
18 {
19  int count = argc > 1 ? std::atoi(argv[1]) : 100;
20  size_t size = argc > 2 ? std::atoi(argv[2]) : 128;
21 
22  std::cout << " count: " << count << std::endl
23  << " size: " << size << " bytes" << std::endl
24  << std::endl;
25 
28 
29  log4cpp::OstreamAppender ostreamAppender("cerr", &std::cerr);
30  log4cpp::FileAppender fileAppender("stderr", fileno(stderr));
31  ostreamAppender.setLayout(new log4cpp::BasicLayout());
32  fileAppender.setLayout(new log4cpp::BasicLayout());
33 
34  root.removeAllAppenders();
35  root.addAppender(ostreamAppender);
36 
38 
39  Clock clock;
40  char* buffer = new char[size + 1];
41 
42  std::memset(buffer, 'X', size + 1);
43  buffer[size] = '\0';
44 
45  std::cout << "BasicLayout:" << std::endl;
46  {
47  clock.start();
48  for (int i = 0; i < count; i++) log.error("%s", buffer);
49  clock.stop();
50  std::cout << " charbuf printf ostream: " << ((float)clock.elapsed()) / count << " us" << std::endl;
51  }
52 
53  {
54  const char* buffer2 = buffer;
55  clock.start();
56  for (int i = 0; i < count; i++) log.error(std::string(buffer2));
57  clock.stop();
58  std::cout << " charbuf string ostream: " << ((float)clock.elapsed()) / count << " us" << std::endl;
59  }
60 
61  {
62  std::string str(size, 'X');
63 
64  clock.start();
65  for (int i = 0; i < count; i++)
66  log << log4cpp::Priority::ERROR << str;
67  clock.stop();
68  std::cout << " string stream ostream: " << ((float)clock.elapsed()) / count << " us" << std::endl;
69  }
70 
71  {
72  clock.start();
73  for (int i = 0; i < count; i++)
74  log << log4cpp::Priority::ERROR << buffer;
75  clock.stop();
76  std::cout << " charbuf stream ostream: " << ((float)clock.elapsed()) / count << " us" << std::endl;
77  }
78 
79  {
80  std::string str(size, 'X');
81 
82  clock.start();
84  for (int i = 0; i < count; i++)
85  s << str << log4cpp::eol;
86  clock.stop();
87  std::cout << " string stream2 ostream: " << ((float)clock.elapsed()) / count << " us" << std::endl;
88  }
89 
90  {
91  clock.start();
93  for (int i = 0; i < count; i++)
94  s << buffer << log4cpp::eol;
95  clock.stop();
96  std::cout << " charbuf stream2 ostream: " << ((float)clock.elapsed()) / count << " us" << std::endl;
97  }
98 
99  {
100  std::string str(size, 'X');
101 
102  clock.start();
103  for (int i = 0; i < count; i++) log.error(str);
104  clock.stop();
105  std::cout << " direct string ostream: " << ((float)clock.elapsed()) / count << " us" << std::endl;
106  }
107 
108  {
109  std::string str(size, 'X');
110  root.removeAllAppenders();
111  root.addAppender(fileAppender);
112 
113  clock.start();
114  for (int i = 0; i < count; i++) log.error(str);
115  clock.stop();
116  std::cout << " direct string file: " << ((float)clock.elapsed()) / count << " us" << std::endl;
117  }
118 
119  std::cout << "PatternLayout:" << std::endl;
120  {
121  log4cpp::PatternLayout* patternLayout = new log4cpp::PatternLayout();
122  patternLayout->setConversionPattern("%R %p %c %x: %m\n");
123  ostreamAppender.setLayout(patternLayout);
124  }
125  {
126  log4cpp::PatternLayout* patternLayout = new log4cpp::PatternLayout();
127  patternLayout->setConversionPattern("%R %p %c %x: %m\n");
128  fileAppender.setLayout(patternLayout);
129  }
130 
131  root.removeAllAppenders();
132  root.addAppender(ostreamAppender);
133 
134  {
135  clock.start();
136  for (int i = 0; i < count; i++) log.error("%s", buffer);
137  clock.stop();
138  std::cout << " charbuf printf ostream: " << ((float)clock.elapsed()) / count << " us" << std::endl;
139  }
140 
141  {
142  std::string str(size, 'X');
143 
144  clock.start();
145  for (int i = 0; i < count; i++) log.error(str);
146  clock.stop();
147  std::cout << " direct string ostream: " << ((float)clock.elapsed()) / count << " us" << std::endl;
148  }
149 
150  {
151  std::string str(size, 'X');
152  root.removeAllAppenders();
153  root.addAppender(fileAppender);
154 
155  clock.start();
156  for (int i = 0; i < count; i++) log.error(str);
157  clock.stop();
158  std::cout << " string file: " << ((float)clock.elapsed()) / count << " us" << std::endl;
159  }
160 
161  {
162  clock.start();
163  for (int i = 0; i < count; i++) fprintf(stderr, "%d ERROR someCategory : %s\n", log4cpp::TimeStamp().getSeconds(), buffer);
164  clock.stop();
165  std::cout << std::endl << " fprintf: " << ((float)clock.elapsed()) / count << " us" << std::endl;
166  }
167 
168  delete[] buffer;
170 
171  return 0;
172 }
Definition: Clock.hh:21
LOG4CPP_EXPORT CategoryStream & eol(CategoryStream &os)
int main(int argc, char *argv[])
Definition: testbench.cpp:17
static Category & getInstance(const std::string &name)
Definition: Category.cpp:35
static void shutdown()
Definition: Category.cpp:48
usec_t stop(void)
Definition: Clock.cpp:95
virtual void setConversionPattern(const std::string &conversionPattern)
virtual void removeAllAppenders()
Definition: Category.cpp:151
static Category & getRoot()
Definition: Category.cpp:23
usec_t start(void)
Definition: Clock.cpp:87
usec_t elapsed(void) const
Definition: Clock.cpp:77
virtual void setPriority(Priority::Value priority)
Definition: Category.cpp:71
virtual void setLayout(Layout *layout=NULL)
virtual void addAppender(Appender *appender)
Definition: Category.cpp:94
void error(const char *stringFormat,...)
Definition: Category.cpp:347


log4cpp
Author(s): Stephen Roderick, Bastiaan Bakker, Cedric Le Goater, Steve Ostlind, Marcel Harkema, Walter Stroebel, Glenn Scott and Tony Cheung
autogenerated on Sun Jun 23 2019 19:10:00