Commit 94b68f03 authored by Paal Kvamme's avatar Paal Kvamme
Browse files

Make SummaryPrintingTimerEx available in more than one file.

parent 22c0cecf
// Copyright 2017-2020, Schlumberger
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "fancy_timers.h"
#include "environment.h"
namespace InternalZGY {
#if 0
}
#endif
/**
* Create a new instance. The base class is constructed with the default
* csv=false but that makes no difference for us because print() is
* redefined here. So we decide for ourselves whether to output csv
* or not.
*
* In the simplest case, instances are statically constructed and
* print their result on program exit.
*/
SummaryPrintingTimerEx::SummaryPrintingTimerEx(const char *name)
: SummaryPrintingTimer(name)
, bytes_read_(0)
, bytes_written_(0)
{
}
SummaryPrintingTimerEx::~SummaryPrintingTimerEx()
{
print();
}
std::string
SummaryPrintingTimerEx::niceSize(const std::string& label, std::int64_t n)
{
if (n > 10*1024*1024)
return label + std::to_string(n/(1024*1024)) + " MB";
else if (n > 10*1024)
return label + std::to_string(n/1024) + " kB";
else if (n != 0)
return label + std::to_string(n) + " bytes";
else
return std::string();
}
void
SummaryPrintingTimerEx::print()
{
if (getCount() != 0) {
std::string msg(isCSVEnabled() ? getCSV() : getValue(true, true));
if (!msg.empty() && msg.back() == '\n')
msg = msg.substr(0, msg.size()-1);
if (isCSVEnabled())
std::cerr << msg
<< "," << bytes_read_.load()
<< "," << bytes_written_.load()
<< std::endl;
else
std::cerr << msg
<< niceSize(", R: ", bytes_read_.load())
<< niceSize(", W: ", bytes_written_.load())
<< std::endl;
}
reset(); // Prevent the base class from printing as well.
}
void
SummaryPrintingTimerEx::addBytesRead(std::int64_t nbytes)
{
bytes_read_.fetch_add(nbytes);
}
void SummaryPrintingTimerEx::addBytesWritten(std::int64_t nbytes) {
bytes_written_.fetch_add(nbytes);
}
bool
SummaryPrintingTimerEx::isCSVEnabled()
{
static int enable = Environment::getNumericEnv("OPENZGY_TIMERS", 0);
return enable > 1;
}
/**
* The only difference between a SimpleTimer and a SimpleTimerEx
* is that the latter is enabled or disabled using the environment
* variable OPENZGY_TIMERS. So it becomes specific to this library.
*/
SimpleTimerEx::SimpleTimerEx(SummaryTimer& owner)
: SimpleTimer(owner, isTimerEnabled())
{
};
bool
SimpleTimerEx::isTimerEnabled()
{
static int enable = Environment::getNumericEnv("OPENZGY_TIMERS", 0);
return enable > 0;
}
} // namespace
// Copyright 2017-2020, Schlumberger
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include "timer.h"
#include <atomic>
#include <cstdint>
#include <string>
#include <iostream>
namespace InternalZGY {
#if 0
}
#endif
/**
* \file fancy-timers.h
* \brief Easy to use performance measurement.
* \details Adds bells and whistles specifically for this project.
* The base class is deliberately kept simple and with few
* dependencies so it can be copy/pasted between projects.
*/
/**
* \brief SummaryTimer that prints its result when going out of scope.
* \details This is a private extension that also knows about total
* number of bytes transfered, so it can report the actuar throughput.
*/
class OPENZGY_TEST_API SummaryPrintingTimerEx : public SummaryPrintingTimer
{
std::atomic<std::int64_t> bytes_read_;
std::atomic<std::int64_t> bytes_written_;
public:
explicit SummaryPrintingTimerEx(const char *name);
virtual ~SummaryPrintingTimerEx();
static std::string niceSize(const std::string& label, std::int64_t n);
virtual void print();
void addBytesRead(std::int64_t nbytes);
void addBytesWritten(std::int64_t nbytes);
static bool isCSVEnabled();
};
/**
* The only difference between a SimpleTimer and a SimpleTimerEx
* is that the latter is enabled or disabled using the environment
* variable OPENZGY_TIMERS. So it becomes specific to this library.
*/
class OPENZGY_TEST_API SimpleTimerEx : public SimpleTimer
{
public:
explicit SimpleTimerEx(SummaryTimer& owner);
static bool isTimerEnabled();
};
} // namespace
......@@ -17,6 +17,7 @@
#include "file.h"
#include "../exception.h"
#include "timer.h"
#include "fancy_timers.h"
#include "environment.h"
#include "file_performance.h"
......@@ -58,60 +59,6 @@ namespace {
static int enable = Environment::getNumericEnv("OPENZGY_TIMERS", 0);
return enable > 0;
}
/**
* \brief SummaryTimer that prints its result when going out of scope.
* \details This is a private extension that also knows about total
* number of bytes transfered, so it can report the actuar throughput.
*/
class OPENZGY_TEST_API SummaryPrintingTimerEx : public SummaryPrintingTimer
{
std::atomic<std::int64_t> bytes_read_;
std::atomic<std::int64_t> bytes_written_;
public:
explicit SummaryPrintingTimerEx(const char *name, bool csv = false)
: SummaryPrintingTimer(name, csv)
, bytes_read_(0)
, bytes_written_(0)
{
}
virtual ~SummaryPrintingTimerEx() {
print();
}
static std::string niceNumber(const std::string& label, std::int64_t n) {
if (n > 10*1024*1024)
return label + std::to_string(n/(1024*1024)) + " MB";
else if (n > 10*1024)
return label + std::to_string(n/1024) + " kB";
else if (n != 0)
return label + std::to_string(n) + " bytes";
else
return std::string();
}
virtual void print() {
if (getCount() != 0) {
std::string msg(csv_ ? getCSV() : getValue(true, true));
if (!msg.empty() && msg.back() == '\n')
msg = msg.substr(0, msg.size()-1);
if (csv_)
std::cerr << msg
<< "," << bytes_read_.load()
<< "," << bytes_written_.load()
<< std::endl;
else
std::cerr << msg
<< niceNumber(", R: ", bytes_read_.load())
<< niceNumber(", W: ", bytes_written_.load())
<< std::endl;
}
reset(); // Prevent the base class from printing as well.
}
void addBytesRead(std::int64_t nbytes) {
bytes_read_.fetch_add(nbytes);
}
void addBytesWritten(std::int64_t nbytes) {
bytes_written_.fetch_add(nbytes);
}
};
}
/**
......
......@@ -194,7 +194,7 @@ public:
*/
class OPENZGY_TEST_API SummaryPrintingTimer : public SummaryTimer
{
protected:
private:
bool csv_;
public:
explicit SummaryPrintingTimer(const char *name, bool csv = false);
......
......@@ -94,6 +94,7 @@
<ClCompile Include="..\src\impl\cornerpoints.cpp" />
<ClCompile Include="..\src\impl\databuffer.cpp" />
<ClCompile Include="..\src\impl\environment.cpp" />
<ClCompile Include="..\src\impl\fancy_timers.cpp" />
<ClCompile Include="..\src\impl\file.cpp" />
<ClCompile Include="..\src\impl\file_consolidate.cpp" />
<ClCompile Include="..\src\impl\file_local.cpp" />
......@@ -132,6 +133,7 @@
<ClInclude Include="..\src\impl\databuffer.h" />
<ClInclude Include="..\src\impl\enum.h" />
<ClInclude Include="..\src\impl\environment.h" />
<ClInclude Include="..\src\impl\fancy_timers.h" />
<ClInclude Include="..\src\impl\file.h" />
<ClInclude Include="..\src\impl\file_consolidate.h" />
<ClInclude Include="..\src\impl\file_performance.h" />
......
......@@ -41,6 +41,9 @@
<ClCompile Include="..\src\impl\environment.cpp">
<Filter>src\impl</Filter>
</ClCompile>
<ClCompile Include="..\src\impl\fancy_timers.cpp">
<Filter>src\impl</Filter>
</ClCompile>
<ClCompile Include="..\src\impl\file.cpp">
<Filter>src\impl</Filter>
</ClCompile>
......@@ -142,6 +145,9 @@
<ClInclude Include="..\src\impl\environment.h">
<Filter>src\impl</Filter>
</ClInclude>
<ClInclude Include="..\src\impl\fancy_timers.h">
<Filter>src\impl</Filter>
</ClInclude>
<ClInclude Include="..\src\impl\file.h">
<Filter>src\impl</Filter>
</ClInclude>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment