Merge commit '8e97373d883c06e1c73791849561d1311c880bc0' as 'libs/loguru'

This commit is contained in:
Henry Winkel
2022-09-15 09:52:21 +02:00
30 changed files with 5801 additions and 0 deletions

View File

@@ -0,0 +1,71 @@
cmake_minimum_required(VERSION 2.8)
project(glog_bench)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release" CACHE STRING
"Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE)
endif(NOT CMAKE_BUILD_TYPE)
MESSAGE(STATUS "CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")
add_compile_options(-std=c++11 -Werror -Wall -Wextra)
file(GLOB source
"${CMAKE_CURRENT_SOURCE_DIR}/*.cpp"
)
# ------------------------------------------------------
# GFLAGS
find_library(GFLAGS_LIB NAMES "gflags" PATHS
${GFLAGS_ROOT}/lib
${GFLAGS_ROOT}/Lib)
if (MSVC)
find_path(GFLAGS_INCLUDE_DIR gflags/gflags.h PATHS ${GFLAGS_ROOT}/Include)
set(GFLAGS_LIB_DIR ${GFLAGS_ROOT}/Lib)
else()
find_path(GFLAGS_INCLUDE_DIR gflags/gflags.h PATHS
${GFLAGS_ROOT}/include)
set(GFLAGS_LIB_DIR ${GFLAGS_ROOT}/lib)
endif()
mark_as_advanced(
GFLAGS_INCLUDE_DIR
GFLAGS_LIB
)
# ------------------------------------------------------
# GLOG
find_library(GLOG_LIB NAMES "glog" libglog PATHS
${GLOG_ROOT})
if (MSVC)
find_path(GLOG_INCLUDE_DIR glog/logging.h PATHS ${GLOG_ROOT})
set(GLOG_LIB_DIR ${GLOG_ROOT})
add_definitions(-DUSE_OWN_CHECK)
else()
find_path(GLOG_INCLUDE_DIR glog/logging.h PATHS
${GLOG_ROOT}/include)
set(GLOG_LIB_DIR ${GLOG_ROOT}/lib)
endif()
add_definitions(-DGLOG_NO_ABBREVIATED_SEVERITIES=1)
mark_as_advanced(
GLOG_INCLUDE_DIR
GLOG_LIB
)
# ------------------------------------------------------
add_executable(glog_bench ${source})
include_directories(
${GFLAGS_INCLUDE_DIR}
${GLOG_INCLUDE_DIR}
)
target_link_libraries(glog_bench
${GFLAGS_LIB}
${GLOG_LIB}
)

View File

@@ -0,0 +1,12 @@
#!/bin/bash
set -e # Fail on error
ROOT_DIR=$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)
cd "$ROOT_DIR"
mkdir -p build
cd build
cmake ..
make
./glog_bench $@ 2>/dev/null

View File

@@ -0,0 +1,93 @@
#include <chrono>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <glog/logging.h>
#include <glog/raw_logging.h>
const size_t kNumIterations = 50 * 1000;
const size_t kNumRuns = 10;
const double kPi = 3.1415926535897932384626433;
static long long now_ns()
{
using namespace std::chrono;
return duration_cast<nanoseconds>(high_resolution_clock::now().time_since_epoch()).count();
}
template<typename Function>
double time_sec(const Function& function)
{
auto start_ns = now_ns();
function();
return (now_ns() - start_ns) * 1e-9;
}
template<typename Function>
void bench(const std::string& name, const Function& function)
{
function(); // Warm-up
printf("%-30s ", name.c_str());
fflush(stdout);
std::vector<double> times;
double sum = 0;
for (size_t i = 0; i < kNumRuns; ++i)
{
times.push_back(time_sec(function) / kNumIterations);
sum += times.back();
}
double mean = sum / kNumRuns;
double std_dev_sum = 0;
for (double time : times) {
std_dev_sum += (time - mean) * (time - mean);
}
double variance = std::sqrt(std_dev_sum / kNumRuns);
printf("%6.3f +- %.3f us per call\n", mean * 1e6, variance * 1e6);
fflush(stdout);
}
// ----------------------------------------------------------------------------
void stream_strings()
{
for (size_t i = 0; i < kNumIterations; ++i) {
LOG(WARNING) << "Some long, complex message.";
}
google::FlushLogFiles(google::GLOG_INFO);
}
void stream_float()
{
for (size_t i = 0; i < kNumIterations; ++i) {
LOG(WARNING) << std::setfill('0') << std::setw(6) << std::setprecision(3) << kPi;
}
google::FlushLogFiles(google::GLOG_INFO);
}
void raw_string_float()
{
for (size_t i = 0; i < kNumIterations; ++i) {
RAW_LOG(WARNING, "Some long, complex message.");
}
google::FlushLogFiles(google::GLOG_INFO);
}
int main(int argc, char* argv[])
{
FLAGS_alsologtostderr = true;
google::ParseCommandLineFlags(&argc, &argv, true);
google::InitGoogleLogging(argv[0]);
bench("LOG(WARNING) << string (buffered):", stream_strings);
bench("LOG(WARNING) << float (buffered):", stream_float);
FLAGS_logbufsecs = 0; // Flush all output in realtime
bench("LOG(WARNING) << string (unbuffered):", stream_strings);
bench("LOG(WARNING) << float (unbuffered):", stream_float);
}