Commit f3ba09c5 authored by Paal Kvamme's avatar Paal Kvamme
Browse files

Consolidate 4 methods to get a human readable size.

parent ac17811a
......@@ -41,17 +41,30 @@ SummaryPrintingTimerEx::~SummaryPrintingTimerEx()
print();
}
/**
* \brief Convert a long integer to a human readable string.
* \details
* Use the largest suffix (TB, MB, etc.) that still allows the number
* to be displayed without any decimal point. So, 2*(1024^3) will be
* disokated as "2 GB" while that number plus 1024 will display
* returns the size in kilobytes. I.e. "2097153 KB".
*/
std::string
SummaryPrintingTimerEx::niceSize(const std::string& label, std::int64_t n)
SummaryPrintingTimerEx::niceSize(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();
static struct {std::int64_t factor; const char *unit;} lookup[]{
{1024LL*1024LL*1024LL*1024LL, " TB"},
{1024*1024*1024, " GB"},
{1024*1024, " MB"},
{1024, " KB"},
{1, " bytes"},
};
std::string neg(n<0?"-":"");
n = std::abs(n);
for (const auto& it : lookup)
if (n >= it.factor && (n % it.factor) == 0)
return neg + std::to_string(n / it.factor) + std::string(it.unit);
return neg + std::to_string(n) + " bytes";
}
void
......@@ -68,8 +81,8 @@ SummaryPrintingTimerEx::print()
<< std::endl;
else
std::cerr << msg
<< niceSize(", R: ", bytes_read_.load())
<< niceSize(", W: ", bytes_written_.load())
<< ", R: " << niceSize(bytes_read_.load())
<< ", W: " << niceSize(bytes_written_.load())
<< std::endl;
}
reset(); // Prevent the base class from printing as well.
......
......@@ -46,7 +46,7 @@ class OPENZGY_TEST_API SummaryPrintingTimerEx : public SummaryPrintingTimer
public:
explicit SummaryPrintingTimerEx(const char *name);
virtual ~SummaryPrintingTimerEx();
static std::string niceSize(const std::string& label, std::int64_t n);
static std::string niceSize(std::int64_t n);
virtual void print();
void addBytesRead(std::int64_t nbytes);
void addBytesWritten(std::int64_t nbytes);
......
......@@ -49,14 +49,7 @@ FileADT::xx_close()
std::string
FileADT::_nice(std::int64_t n)
{
if (n >= 1024*1024 && (n % (1024*1024)) == 0)
return std::to_string(n/(1024*1024)) + " MB"; // whole number of MB
else if (n >= 256*1024 && (n % (256*1024)) == 0)
return std::to_string(n/(1024*1024.0)) + " MB"; // e.g. 42.75 MB
else if (n >= 1024 && (n % 1024) == 0)
return std::to_string(n/1024) + " kB";
else
return std::to_string(n) + " bytes";
return SummaryPrintingTimerEx::niceSize(n);
}
void
......
......@@ -13,6 +13,7 @@
// limitations under the License.
#include "file_consolidate.h"
#include "fancy_timers.h"
#include <algorithm>
#include <iostream>
......@@ -279,19 +280,6 @@ ConsolidateRequests::_join_requests(
return new_requests;
}
static std::string nicenumber(std::int64_t n)
{
std::string neg(n<0?"-":"");
n = std::abs(n);
if (n >= (1024*1024*1024) && (n % (1024*1024*1024)) == 0)
return neg + std::to_string(n / (1024*1024*1024)) + " GB";
if (n >= (1024*1024) && (n % (1024*1024)) == 0)
return neg + std::to_string(n / (1024*1024)) + " MB";
if (n >= (1024) && (n % (1024)) == 0)
return neg + std::to_string(n / (1024)) + " KB";
return neg + std::to_string(n) + " bytes";
}
/**
* For debugging only, print a list of list of requests.
*/
......@@ -304,9 +292,9 @@ ConsolidateRequests::_print_requests(
(const char *name, std::int64_t offset, std::int64_t size) {
std::cout << std::dec
<< " " << name
<< " offset " << std::setw(8) << nicenumber(offset)
<< " end " << std::setw(8) << nicenumber(offset+size)
<< " size " << std::setw(6) << nicenumber(size) << std::dec << "\n";
<< " offset " << std::setw(8) << SummaryPrintingTimerEx::niceSize(offset)
<< " end " << std::setw(8) << SummaryPrintingTimerEx::niceSize(offset+size)
<< " size " << std::setw(6) << SummaryPrintingTimerEx::niceSize(size) << std::dec << "\n";
};
if (all_requests.empty() ||
(all_requests.size() == 1 && all_requests.front().empty())) {
......
......@@ -15,6 +15,7 @@
#include "test_all.h"
#include "../exception.h"
#include "../impl/file.h"
#include "../impl/fancy_timers.h"
#include "../impl/file_consolidate.h"
#include <iostream>
......@@ -32,19 +33,6 @@ namespace {
static int _nextseq = 0;
static std::string nicenumber(std::int64_t n)
{
std::string neg(n<0?"-":"");
n = std::abs(n);
if (n >= (1024*1024*1024) && (n % (1024*1024*1024)) == 0)
return neg + std::to_string(n / (1024*1024*1024)) + " GB";
if (n >= (1024*1024) && (n % (1024*1024)) == 0)
return neg + std::to_string(n / (1024*1024)) + " MB";
if (n >= (1024) && (n % (1024)) == 0)
return neg + std::to_string(n / (1024)) + " KB";
return neg + std::to_string(n) + " bytes";
}
class MyReceiver
{
public:
......@@ -77,7 +65,7 @@ public:
*firstval = idata[0];
if (false)
std::cout << "Receiver # " << seq
<< ": Deliver " << nicenumber(size) << ": "
<< ": Deliver " << SummaryPrintingTimerEx::niceSize(size) << ": "
<< idata[0] << ", " << idata[1] << ", " << idata[2] << ", ..."
<< "\n";
}
......
......@@ -78,6 +78,24 @@ static void test_timer_summary()
TEST_CHECK(msg.find("\"MySummary\"") != std::string::npos);
}
static void test_timer_nicesize()
{
const std::int64_t K{1024};
TEST_CHECK(SummaryPrintingTimerEx::niceSize(0) == "0 bytes");
TEST_CHECK(SummaryPrintingTimerEx::niceSize(-42) == "-42 bytes");
TEST_CHECK(SummaryPrintingTimerEx::niceSize(3*K) == "3 KB");
TEST_CHECK(SummaryPrintingTimerEx::niceSize(3*K+1) == "3073 bytes");
TEST_CHECK(SummaryPrintingTimerEx::niceSize(7*K*K) == "7 MB");
TEST_CHECK(SummaryPrintingTimerEx::niceSize(-1*K*K*K) == "-1 GB");
TEST_CHECK(SummaryPrintingTimerEx::niceSize(2*K*K*K*K) == "2 TB");
TEST_CHECK(SummaryPrintingTimerEx::niceSize(2*K*K*K+K) == "2097153 KB");
TEST_CHECK(SummaryPrintingTimerEx::niceSize(7*256*K) == "1792 KB");
// The next one used to be handles as a special case: An even number
// of 256 KB bricks were printed as MB even though that might involve
// a decimal point.
//TEST_CHECK(SummaryPrintingTimerEx::niceSize(7*256*K) == "1.75 MB");
}
} // namespace for tests
namespace {
......@@ -88,6 +106,7 @@ namespace {
{
register_test("timer.oneshot", test_timer_oneshot);
register_test("timer.summary", test_timer_summary);
register_test("timer.nicesize", test_timer_nicesize);
}
} dummy;
} // namespace for registration
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