2011-11-22 00:04:02 +01:00
|
|
|
/** ==========================================================================
|
|
|
|
* 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.
|
2014-07-03 23:42:19 +02:00
|
|
|
*
|
|
|
|
* For more information see g3log/LICENSE or refer refer to http://unlicense.org
|
2011-11-22 00:04:02 +01:00
|
|
|
* ============================================================================*/
|
2015-07-16 09:55:23 +02:00
|
|
|
#pragma once
|
2011-11-05 17:36:07 +01:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <ctime>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <vector>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <numeric>
|
|
|
|
#include <chrono>
|
|
|
|
#include <cassert>
|
|
|
|
|
2015-07-20 07:10:56 +02:00
|
|
|
#if defined(G3LOG_PERFORMANCE)
|
|
|
|
#include <g3log/g3log.hpp>
|
|
|
|
#include <g3log/logworker.hpp>
|
|
|
|
using namespace g3::internal;
|
2012-06-02 22:04:28 +02:00
|
|
|
|
2011-11-05 17:36:07 +01:00
|
|
|
#elif defined(GOOGLE_GLOG_PERFORMANCE)
|
|
|
|
#include <glog/logging.h>
|
2011-11-07 12:04:32 +01:00
|
|
|
#else
|
2015-07-20 07:10:56 +02:00
|
|
|
#error G3LOG_PERFORMANCE or GOOGLE_GLOG_PERFORMANCE was not defined
|
2011-11-05 17:36:07 +01:00
|
|
|
#endif
|
|
|
|
|
2013-12-28 22:58:22 +01:00
|
|
|
typedef std::chrono::high_resolution_clock::time_point time_point;
|
2014-02-26 07:00:08 +01:00
|
|
|
typedef std::chrono::duration<uint64_t,std::ratio<1, 1000> > millisecond;
|
|
|
|
typedef std::chrono::duration<uint64_t,std::ratio<1, 1000000> > microsecond;
|
2011-11-05 17:36:07 +01:00
|
|
|
|
2015-07-20 07:10:56 +02:00
|
|
|
namespace g3_test
|
2011-11-05 17:36:07 +01:00
|
|
|
{
|
|
|
|
enum WriteMode
|
|
|
|
{
|
|
|
|
kAppend = 0,
|
|
|
|
kTruncate = 1
|
|
|
|
};
|
|
|
|
|
2014-02-26 07:00:08 +01:00
|
|
|
const uint64_t g_loop{1};
|
|
|
|
const uint64_t g_iterations{1000000};
|
2011-11-05 17:36:07 +01:00
|
|
|
const char* charptrmsg = "\tmessage by char*";
|
2014-02-26 07:00:08 +01:00
|
|
|
const std::string strmsg{"\tmessage by string"};
|
|
|
|
float pi_f{3.1415926535897932384626433832795f};
|
2011-11-05 17:36:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2011-11-10 22:23:33 +01:00
|
|
|
|
2011-11-05 17:36:07 +01:00
|
|
|
out << msg;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-02-26 07:00:08 +01:00
|
|
|
uint64_t mean(const std::vector<uint64_t> &v)
|
2011-11-05 17:36:07 +01:00
|
|
|
{
|
2014-02-26 07:00:08 +01:00
|
|
|
uint64_t total = std::accumulate(v.begin(), v.end(), uint64_t(0) ); // '0' is the initial value
|
2011-11-05 17:36:07 +01:00
|
|
|
return total/v.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-02-26 07:00:08 +01:00
|
|
|
void measurePeakDuringLogWrites(const std::string& title, std::vector<uint64_t>& result);
|
|
|
|
inline void measurePeakDuringLogWrites(const std::string& title, std::vector<uint64_t>& result)
|
2011-11-05 17:36:07 +01:00
|
|
|
{
|
2012-06-02 22:04:28 +02:00
|
|
|
|
|
|
|
|
2015-07-20 07:10:56 +02:00
|
|
|
#if defined(G3LOG_PERFORMANCE)
|
|
|
|
std::cout << "G3LOG (" << title << ") WORST_PEAK PERFORMANCE TEST" << std::endl;
|
2011-11-05 17:36:07 +01:00
|
|
|
#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
|
2014-02-26 07:00:08 +01:00
|
|
|
for(uint64_t count = 0; count < g_iterations; ++count)
|
2011-11-05 17:36:07 +01:00
|
|
|
{
|
2013-12-28 22:58:22 +01:00
|
|
|
auto start_time = std::chrono::high_resolution_clock::now();
|
2011-11-05 17:36:07 +01:00
|
|
|
LOG(INFO) << title << " iteration #" << count << " " << charptrmsg << strmsg << " and a float: " << std::setprecision(6) << pi_f;
|
2013-12-28 22:58:22 +01:00
|
|
|
auto stop_time = std::chrono::high_resolution_clock::now();
|
2014-02-26 07:00:08 +01:00
|
|
|
uint64_t time_us = std::chrono::duration_cast<microsecond>(stop_time - start_time).count();
|
2011-11-05 17:36:07 +01:00
|
|
|
result.push_back(time_us);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void doLogWrites(const std::string& title);
|
|
|
|
inline void doLogWrites(const std::string& title)
|
|
|
|
{
|
2015-07-20 07:10:56 +02:00
|
|
|
#if defined(G3LOG_PERFORMANCE)
|
|
|
|
std::cout << "G3LOG (" << title << ") PERFORMANCE TEST" << std::endl;
|
2011-11-05 17:36:07 +01:00
|
|
|
#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
|
2014-02-26 07:00:08 +01:00
|
|
|
for(uint64_t count = 0; count < g_iterations; ++count)
|
2011-11-05 17:36:07 +01:00
|
|
|
{
|
|
|
|
LOG(INFO) << title << " iteration #" << count << " " << charptrmsg << strmsg << " and a float: " << std::setprecision(6) << pi_f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // end namespace
|