我正在对 log4cxx 进行性能测试。当我直接调用 LOG4CXX_XXXXX 时,以下代码大约需要 10 秒才能完成记录所有内容。但是当我用 boost::lockguard (我认为这是一个关键部分)来结束调用时,大约需要 5 秒。有人可以解释为什么会这样,而不是相反。
#include "log4cxx/logger.h"
#include "log4cxx/xml/domconfigurator.h"
#include <boost/thread/thread.hpp>
#include <boost/timer/timer.hpp>
#include <boost/thread/recursive_mutex.hpp>
//#define ENHANCEDLOGGING
using namespace log4cxx;
using namespace log4cxx::helpers;
#define LOG_TMP_BUF_SIZE 3000
LoggerPtr logger(Logger::getRootLogger());
#ifdef ENHANCEDLOGGING
void LOG_DEBUG(const char *format)
{
LOG4CXX_DEBUG(logger, format);
}
#else
boost::recursive_mutex m_guard;
#define LOG_COMM 0
void LOG_DEBUG(const char *format)
{
boost::lock_guard<boost::recursive_mutex> lock(m_guard);
LOG4CXX_DEBUG(logger, format);
}
#endif
const int thread_count = 100;
void printer(void)
{
for (int i = 0; i < 4000; i++)
LOG_DEBUG("Logging performance check");
}
int main(int argc, char **argv)
{
boost::timer::auto_cpu_timer t;
xml::DOMConfigurator::configure("config.xml");
boost::thread_group threads;
for (int i = 0; i != thread_count; ++i)
threads.create_thread(printer);
threads.join_all();
std::cout << t.elapsed().user << std::endl;
std::cout << t.elapsed().wall << std::endl;
std::cout << t.elapsed().system << std::endl;
return 0;
}
我正在使用以下配置。使用/不使用 org.apache.log4j.AsyncAppender 运行没有区别
<appender name="appxNormalAppender" class="org.apache.log4j.FileAppender">
<param name="file" value="appxLogFile.log" />
<param name="append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p %C{2} (%F:%L) %t - %m%n" />
</layout>
</appender>
<appender name="async" class="org.apache.log4j.AsyncAppender">
<param name="BufferSize" value="1000"/>
<appender-ref ref="appxNormalAppender"/>
</appender>
<root>
<priority value="all" />
<appender-ref ref="appxNormalAppender"/>
</root>
</log4j:configuration>