testbench.cpp
Go to the documentation of this file.
00001 #include <cstdlib>
00002 #include <cstring>
00003 #include <cstdio>
00004 #include <iostream>
00005 
00006 #include <log4cpp/Category.hh>
00007 #include <log4cpp/OstreamAppender.hh>
00008 #include <log4cpp/FileAppender.hh>
00009 #include <log4cpp/BasicLayout.hh>
00010 #include <log4cpp/PatternLayout.hh>
00011 #include <log4cpp/TimeStamp.hh>
00012 
00013 #include "Clock.hh"
00014 
00015 
00016 // -----------------------------------------------------------------------------
00017 int main(int argc, char* argv[])
00018 {
00019     int    count  = argc > 1 ? std::atoi(argv[1]) : 100;
00020     size_t size   = argc > 2 ? std::atoi(argv[2]) : 128;    
00021 
00022     std::cout << "  count: " << count << std::endl
00023               << "   size: " << size  << " bytes" << std::endl
00024               << std::endl;
00025 
00026     log4cpp::Category&  root     = log4cpp::Category::getRoot();
00027     root.setPriority(log4cpp::Priority::ERROR);    
00028 
00029     log4cpp::OstreamAppender ostreamAppender("cerr", &std::cerr);
00030     log4cpp::FileAppender fileAppender("stderr", fileno(stderr));
00031     ostreamAppender.setLayout(new log4cpp::BasicLayout());
00032     fileAppender.setLayout(new log4cpp::BasicLayout());
00033 
00034     root.removeAllAppenders();
00035     root.addAppender(ostreamAppender);
00036 
00037     log4cpp::Category& log = log4cpp::Category::getInstance("someCategory");
00038 
00039     Clock  clock;
00040     char*  buffer = new char[size + 1];
00041     
00042     std::memset(buffer, 'X', size + 1);
00043     buffer[size] = '\0';
00044 
00045     std::cout << "BasicLayout:" << std::endl;
00046     {   
00047         clock.start();
00048         for (int i = 0; i < count; i++) log.error("%s", buffer);    
00049         clock.stop();
00050         std::cout << "  charbuf printf  ostream:  " << ((float)clock.elapsed()) / count << " us" << std::endl;
00051     }
00052     
00053     {   
00054         const char* buffer2 = buffer;
00055         clock.start();
00056         for (int i = 0; i < count; i++) log.error(std::string(buffer2));    
00057         clock.stop();
00058         std::cout << "  charbuf string  ostream:  " << ((float)clock.elapsed()) / count << " us" << std::endl;
00059     }
00060 
00061     {
00062         std::string str(size, 'X');
00063 
00064         clock.start();
00065         for (int i = 0; i < count; i++) 
00066             log << log4cpp::Priority::ERROR << str;
00067         clock.stop();
00068         std::cout << "  string  stream  ostream:  " << ((float)clock.elapsed()) / count << " us" << std::endl;
00069     }
00070 
00071     {
00072         clock.start();
00073         for (int i = 0; i < count; i++) 
00074             log << log4cpp::Priority::ERROR << buffer;
00075         clock.stop();
00076         std::cout << "  charbuf stream  ostream:  " << ((float)clock.elapsed()) / count << " us" << std::endl;
00077     }
00078     
00079     {
00080         std::string str(size, 'X');
00081 
00082         clock.start();
00083         log4cpp::CategoryStream s(log << log4cpp::Priority::ERROR);
00084         for (int i = 0; i < count; i++) 
00085             s << str << log4cpp::eol;
00086         clock.stop();
00087         std::cout << "  string  stream2 ostream:  " << ((float)clock.elapsed()) / count << " us" << std::endl;
00088     }
00089 
00090     {
00091         clock.start();
00092         log4cpp::CategoryStream s(log << log4cpp::Priority::ERROR);
00093         for (int i = 0; i < count; i++) 
00094             s << buffer << log4cpp::eol;
00095         clock.stop();
00096         std::cout << "  charbuf stream2 ostream:  " << ((float)clock.elapsed()) / count << " us" << std::endl;
00097     }
00098     
00099     {
00100         std::string str(size, 'X');
00101         
00102         clock.start();
00103         for (int i = 0; i < count; i++) log.error(str);
00104         clock.stop();
00105         std::cout << "  direct  string  ostream:  " << ((float)clock.elapsed()) / count << " us" << std::endl;
00106     }
00107 
00108     {
00109         std::string str(size, 'X');
00110         root.removeAllAppenders();
00111         root.addAppender(fileAppender);
00112 
00113         clock.start();
00114         for (int i = 0; i < count; i++) log.error(str);
00115         clock.stop();
00116         std::cout << "  direct  string  file:     " << ((float)clock.elapsed()) / count << " us" << std::endl;
00117     }
00118 
00119     std::cout << "PatternLayout:" << std::endl;
00120     {
00121         log4cpp::PatternLayout* patternLayout = new log4cpp::PatternLayout();
00122         patternLayout->setConversionPattern("%R %p %c %x: %m\n");
00123         ostreamAppender.setLayout(patternLayout);
00124     }
00125     {
00126         log4cpp::PatternLayout* patternLayout = new log4cpp::PatternLayout();
00127         patternLayout->setConversionPattern("%R %p %c %x: %m\n");
00128         fileAppender.setLayout(patternLayout);
00129     }
00130 
00131     root.removeAllAppenders();
00132     root.addAppender(ostreamAppender);
00133 
00134     {   
00135         clock.start();
00136         for (int i = 0; i < count; i++) log.error("%s", buffer);    
00137         clock.stop();
00138         std::cout << "  charbuf printf ostream:  " << ((float)clock.elapsed()) / count << " us" << std::endl;
00139     }
00140     
00141     {
00142         std::string str(size, 'X');
00143         
00144         clock.start();
00145         for (int i = 0; i < count; i++) log.error(str);
00146         clock.stop();
00147         std::cout << "  direct  string ostream:  " << ((float)clock.elapsed()) / count << " us" << std::endl;
00148     }
00149 
00150     {
00151         std::string str(size, 'X');
00152         root.removeAllAppenders();
00153         root.addAppender(fileAppender);
00154 
00155         clock.start();
00156         for (int i = 0; i < count; i++) log.error(str);
00157         clock.stop();
00158         std::cout << "  string  file:     " << ((float)clock.elapsed()) / count << " us" << std::endl;
00159     }
00160 
00161     {
00162         clock.start();
00163         for (int i = 0; i < count; i++) fprintf(stderr, "%d ERROR someCategory : %s\n", log4cpp::TimeStamp().getSeconds(), buffer);
00164         clock.stop();
00165         std::cout << std::endl << "  fprintf:        " << ((float)clock.elapsed()) / count << " us" << std::endl;
00166     }
00167 
00168     delete[] buffer;
00169     log4cpp::Category::shutdown();
00170 
00171     return 0;
00172 }


log4cpp
Author(s): Stephen Roderick
autogenerated on Mon Oct 6 2014 03:13:14