当前位置: 首页 > 工具软件 > G3log > 使用案例 >

log4cplus与g3log性能测试

司徒胤
2023-12-01

https://kjellkod.wordpress.com/2015/06/30/the-worlds-fastest-logger-vs-g3log/  g3log作者对spdlog的对比和嘲讽

http://moodycamel.com/blog/2014/a-fast-general-purpose-lock-free-queue-for-c++ 一个lockfree的queue,被g3log的作者推荐

简单地对log4cplus和g3log的性能做了测试,打印1百万条日志信息所需时间如下:

  LinuxWindows
log4cplus同步4s6s
异步1.9s64s
g3log异步2.3s待做

对比说明:

1. Linux用的虚拟机,操作系统为Red Hat Enterprise Linux Server release 6.7 (Santiago), 8核cpu,内存32G,Intel(R) Xeon(R) CPU E5-2690 v4 @ 2.60GHz。

2. Windows用的实体机,操作系统为Win7, Intel(R) Core(TM) i5-6500, 3.2GHz,  内存16G。

3. 由于Windows和Linux所用配置完全不一样,可能对于两个操作系统之间的对比没有什么可比性。

4. log4cplus在Windows下异步模式非常的慢(经过多次跑都差不多的慢,把配置文件里的Queuelimit增加到1百万也没用),不知道瓶颈是在哪里,也可能是我的程序或配置有问题。

 

log4cplus的测试程序如下:

//main.cc

#include <string>
#include <iostream>
#include <boost/property_tree/ini_parser.hpp>
#include <log4cplus/logger.h>
#include <log4cplus/configurator.h>
#include <log4cplus/loggingmacros.h>

int ReadFile(const char* file_path, std::vector<char>* o_data) {
  std::ifstream in (file_path, std::ios::binary);
  if (!in.good ()) {
    return -1;
  }

  in.seekg(0, std::ifstream::end);
  size_t size = (size_t) in.tellg ();
  in.seekg(0, std::ifstream::beg);
  o_data->resize (size);
  in.read(&(*o_data)[0], size);
  if (in.gcount() != size) {
    return -1;
  }

  return 0;
}


int main(int argc, char* argv[])
{
    if (argc < 2) {
      std::cout << "Please specify path of the confuguration file." << std::endl;
      return 0;
    }

    std::string config_path(argv[1]);
    std::string log_config;
    std::vector<char> data;
    int res = ReadFile(config_path.c_str(), &data);
    if (0 != res) {
      std::cout << "Read ini file failed!" << std::endl;
      return -1;
    }

    std::string config_str(&data[0], data.size());
    std::stringstream css(config_str);
    boost::property_tree::ptree pt;
    boost::property_tree::read_ini(css, pt);

    std::stringstream ss;
    boost::property_tree::write_ini(ss, pt.get_child("log4cplus"));

    log4cplus::initialize();
    log4cplus::PropertyConfigurator pc(ss);
    pc.configure();

    log4cplus::Logger logger = log4cplus::Logger::getInstance("global");

    for(int i=0; i<1000000; ++i)
    {
      LOG4CPLUS_INFO(logger, "logging test " << i);
    }

    log4cplus::Logger::shutdown();
    return 0;
}

配置文件如下:

## synchronous log properties.
[log4cplus_sync]
log4cplus.logger.global = INFO, SA
log4cplus.appender.SA=log4cplus::DailyRollingFileAppender
log4cplus.appender.SA.Schedule=HOURLY
log4cplus.appender.SA.DatePattern=%Y-%m-%d:%H
log4cplus.appender.SA.File=./main.log
log4cplus.appender.SA.MaxBackupIndex=100
log4cplus.appender.SA.BufferSize=131072
log4cplus.appender.SA.Append=true
log4cplus.appender.SA.layout=log4cplus::PatternLayout
log4cplus.appender.SA.layout.ConversionPattern=%D{%Y-%m-%d %H:%M:%S,%Q} [%t] %-5p %m%n

## asynchronous log properties.
[log4cplus]
log4cplus.logger.global = INFO, AA
log4cplus.appender.AA=log4cplus::AsyncAppender
log4cplus.appender.AA.QueueLimit=10000
log4cplus.appender.AA.Appender=log4cplus::DailyRollingFileAppender
log4cplus.appender.AA.Appender.Schedule=HOURLY
log4cplus.appender.AA.Appender.Threshold = INFO
log4cplus.appender.AA.Appender.DatePattern=%Y-%m-%d-%H
log4cplus.appender.AA.Appender.File=./logger_test.log
log4cplus.appender.AA.Appender.ImmediateFlush=false
log4cplus.appender.AA.Appender.MaxFileSize=1000MB
log4cplus.appender.AA.Appender.MaxBackupIndex=100
log4cplus.appender.AA.Appender.Append=true
log4cplus.appender.AA.Appender.layout=log4cplus::PatternLayout
log4cplus.appender.AA.Appender.layout.ConversionPattern=%D{%Y-%m-%d %H:%M:%S,%Q} [%t] %-5p %m%n

Linux下的Makefile内容如下:

main:
  g++ -std=c++11 -Wall -O3 -L/home/wqf/usr/lib/boost_1_67_0/lib/ -lboost_system -lboost_filesystem -lboost_locale -lboost_date_time -llog4cplus main.cc -o main

 

 

g3log的测试程序如下:

#include <string>
#include <iostream>
#include <memory>
#include <g3log/g3log.hpp>
#include <g3log/logworker.hpp>

std::string path_to_log_file = "./";
std::string log_file = "g3logfile";

std::unique_ptr<g3::LogWorker> worker;

void log_init()
{
  worker = g3::LogWorker::createLogWorker();
  auto handle = worker->addDefaultLogger(log_file, path_to_log_file);
  g3::initializeLogging(worker.get());
}

void log_shutdown()
{
  g3::internal::shutDownLogging();
}

int main(int argc, char* argv[])
{
    log_init();

    for (int i = 0; i < 1000000; i++)
    {
      // use LOGF which like printf
      LOGF(INFO, "logging test %d", i);
    }

    log_shutdown();

    return 0;
}

Linux下的Makefile内容如下:

main:
  g++ -std=c++14 -Wall -O3 -I/home/wqf/usr/lib/g3log/include -L/home/wqf/usr/lib/g3log/lib -lg3logger main.cc -o main

 

 类似资料: