mirror of
https://github.com/KjellKod/g3log.git
synced 2024-12-14 02:57:46 +01:00
87a095e384
--HG-- rename : g2log/Build.cmake => Build.cmake rename : g2log/CMakeLists.txt => CMakeLists.txt rename : g2log/CPackLists.txt => CPackLists.txt rename : g2log/Dynamic.cmake => Dynamic.cmake rename : g2log/example/Example.cmake => example/Example.cmake rename : g2log/example/main_contract.cpp => example/main_contract.cpp rename : g2log/example/main_sigsegv.cpp => example/main_sigsegv.cpp rename : g2log/src/active.hpp => src/active.hpp rename : g2log/src/crashhandler.hpp => src/crashhandler.hpp rename : g2log/src/crashhandler_unix.cpp => src/crashhandler_unix.cpp rename : g2log/src/crashhandler_win.cpp => src/crashhandler_win.cpp rename : g2log/src/g2filesink.cpp => src/g2filesink.cpp rename : g2log/src/g2filesink.hpp => src/g2filesink.hpp rename : g2log/src/g2filesinkhelper.ipp => src/g2filesinkhelper.ipp rename : g2log/src/g2future.hpp => src/g2future.hpp rename : g2log/src/g2log.cpp => src/g2log.cpp rename : g2log/src/g2log.hpp => src/g2log.hpp rename : g2log/src/g2loglevels.cpp => src/g2loglevels.cpp rename : g2log/src/g2loglevels.hpp => src/g2loglevels.hpp rename : g2log/src/g2logmessage.cpp => src/g2logmessage.cpp rename : g2log/src/g2logmessage.hpp => src/g2logmessage.hpp rename : g2log/src/g2logmessagecapture.cpp => src/g2logmessagecapture.cpp rename : g2log/src/g2logmessagecapture.hpp => src/g2logmessagecapture.hpp rename : g2log/src/g2logworker.cpp => src/g2logworker.cpp rename : g2log/src/g2logworker.hpp => src/g2logworker.hpp rename : g2log/src/g2moveoncopy.hpp => src/g2moveoncopy.hpp rename : g2log/src/g2sink.hpp => src/g2sink.hpp rename : g2log/src/g2sinkhandle.hpp => src/g2sinkhandle.hpp rename : g2log/src/g2sinkwrapper.hpp => src/g2sinkwrapper.hpp rename : g2log/src/g2time.cpp => src/g2time.cpp rename : g2log/src/g2time.hpp => src/g2time.hpp rename : g2log/src/shared_queue.hpp => src/shared_queue.hpp rename : g2log/src/std2_make_unique.hpp => src/std2_make_unique.hpp rename : g2log/src/stlpatch_future.hpp => src/stlpatch_future.hpp rename : g2log/test_performance/Performance.cmake => test_performance/Performance.cmake rename : g2log/test_performance/main_threaded_mean.cpp => test_performance/main_threaded_mean.cpp rename : g2log/test_performance/main_threaded_worst.cpp => test_performance/main_threaded_worst.cpp rename : g2log/test_performance/performance.h => test_performance/performance.h rename : g2log/test_unit/Test.cmake => test_unit/Test.cmake rename : g2log/test_unit/test_concept_sink.cpp => test_unit/test_concept_sink.cpp rename : g2log/test_unit/test_configuration.cpp => test_unit/test_configuration.cpp rename : g2log/test_unit/test_filechange.cpp => test_unit/test_filechange.cpp rename : g2log/test_unit/test_io.cpp => test_unit/test_io.cpp rename : g2log/test_unit/test_linux_dynamic_loaded_sharedlib.cpp => test_unit/test_linux_dynamic_loaded_sharedlib.cpp rename : g2log/test_unit/test_sink.cpp => test_unit/test_sink.cpp rename : g2log/test_unit/tester_sharedlib.cpp => test_unit/tester_sharedlib.cpp rename : g2log/test_unit/tester_sharedlib.h => test_unit/tester_sharedlib.h rename : g2log/test_unit/testing_helpers.cpp => test_unit/testing_helpers.cpp rename : g2log/test_unit/testing_helpers.h => test_unit/testing_helpers.h
132 lines
4.0 KiB
C++
132 lines
4.0 KiB
C++
/** ==========================================================================
|
|
* 2011 by KjellKod.cc. This is PUBLIC DOMAIN to use at your own risk and comes
|
|
* with no warranties. This code is yours to share, use and modify with no
|
|
* strings attached and no restrictions or obligations.
|
|
*
|
|
* For more information see g3log/LICENSE or refer refer to http://unlicense.org
|
|
* ============================================================================*/
|
|
#ifndef PERFORMANCE_G2_TEST_H_
|
|
#define PERFORMANCE_G2_TEST_H_
|
|
|
|
#include <iostream>
|
|
#include <sstream>
|
|
#include <fstream>
|
|
#include <ctime>
|
|
#include <cstdio>
|
|
#include <iomanip>
|
|
#include <vector>
|
|
#include <algorithm>
|
|
#include <numeric>
|
|
#include <chrono>
|
|
#include <cassert>
|
|
|
|
#if defined(G2LOG_PERFORMANCE)
|
|
#include "g2log.hpp"
|
|
#include "g2logworker.hpp"
|
|
using namespace g2::internal;
|
|
|
|
#elif defined(GOOGLE_GLOG_PERFORMANCE)
|
|
#include <glog/logging.h>
|
|
#else
|
|
#error G2LOG_PERFORMANCE or GOOGLE_GLOG_PERFORMANCE was not defined
|
|
#endif
|
|
|
|
typedef std::chrono::high_resolution_clock::time_point time_point;
|
|
typedef std::chrono::duration<uint64_t,std::ratio<1, 1000> > millisecond;
|
|
typedef std::chrono::duration<uint64_t,std::ratio<1, 1000000> > microsecond;
|
|
|
|
namespace g2_test
|
|
{
|
|
enum WriteMode
|
|
{
|
|
kAppend = 0,
|
|
kTruncate = 1
|
|
};
|
|
|
|
const uint64_t g_loop{1};
|
|
const uint64_t g_iterations{1000000};
|
|
const char* charptrmsg = "\tmessage by char*";
|
|
const std::string strmsg{"\tmessage by string"};
|
|
float pi_f{3.1415926535897932384626433832795f};
|
|
|
|
|
|
bool writeTextToFile(const std::string& filename, const std::string& msg, const WriteMode write_mode, bool push_out = true)
|
|
{
|
|
if(push_out)
|
|
{
|
|
std::cout << msg << std::flush;
|
|
}
|
|
|
|
std::ofstream out;
|
|
std::ios_base::openmode mode = std::ios_base::out; // for clarity: it's really overkill since it's an ofstream
|
|
(kTruncate == write_mode) ? mode |= std::ios_base::trunc : mode |= std::ios_base::app;
|
|
out.open(filename.c_str(), mode);
|
|
if (!out.is_open())
|
|
{
|
|
std::ostringstream ss_error;
|
|
ss_error << "Fatal error could not open log file:[" << filename << "]";
|
|
ss_error << "\n\t\t std::ios_base state = " << out.rdstate();
|
|
std::cerr << ss_error.str().c_str() << std::endl << std::flush;
|
|
return false;
|
|
}
|
|
|
|
out << msg;
|
|
return true;
|
|
}
|
|
|
|
uint64_t mean(const std::vector<uint64_t> &v)
|
|
{
|
|
uint64_t total = std::accumulate(v.begin(), v.end(), uint64_t(0) ); // '0' is the initial value
|
|
return total/v.size();
|
|
}
|
|
|
|
|
|
|
|
|
|
void measurePeakDuringLogWrites(const std::string& title, std::vector<uint64_t>& result);
|
|
inline void measurePeakDuringLogWrites(const std::string& title, std::vector<uint64_t>& result)
|
|
{
|
|
|
|
|
|
#if defined(G2LOG_PERFORMANCE)
|
|
std::cout << "G2LOG (" << title << ") WORST_PEAK PERFORMANCE TEST" << std::endl;
|
|
#elif defined(GOOGLE_GLOG_PERFORMANCE)
|
|
std::cout << "GOOGLE_GLOG (" << title << ") WORST_PEAK PERFORMANCE TEST" << std::endl;
|
|
#else
|
|
std::cout << "ERROR no performance type chosen" << std::endl;
|
|
assert(false);
|
|
#endif
|
|
for(uint64_t count = 0; count < g_iterations; ++count)
|
|
{
|
|
auto start_time = std::chrono::high_resolution_clock::now();
|
|
LOG(INFO) << title << " iteration #" << count << " " << charptrmsg << strmsg << " and a float: " << std::setprecision(6) << pi_f;
|
|
auto stop_time = std::chrono::high_resolution_clock::now();
|
|
uint64_t time_us = std::chrono::duration_cast<microsecond>(stop_time - start_time).count();
|
|
result.push_back(time_us);
|
|
}
|
|
}
|
|
|
|
|
|
void doLogWrites(const std::string& title);
|
|
inline void doLogWrites(const std::string& title)
|
|
{
|
|
#if defined(G2LOG_PERFORMANCE)
|
|
std::cout << "G2LOG (" << title << ") PERFORMANCE TEST" << std::endl;
|
|
#elif defined(GOOGLE_GLOG_PERFORMANCE)
|
|
std::cout << "GOOGLE_GLOG (" << title << ") PERFORMANCE TEST" << std::endl;
|
|
#else
|
|
std::cout << "ERROR no performance type chosen" << std::endl;
|
|
assert(false);
|
|
#endif
|
|
for(uint64_t count = 0; count < g_iterations; ++count)
|
|
{
|
|
LOG(INFO) << title << " iteration #" << count << " " << charptrmsg << strmsg << " and a float: " << std::setprecision(6) << pi_f;
|
|
}
|
|
}
|
|
|
|
|
|
} // end namespace
|
|
|
|
|
|
#endif // fPERFORMANCE_G2_TEST_H_
|