Commit 2caf3abc authored by Tom Jackson's avatar Tom Jackson Committed by Facebook Github Bot

Pretty Big Numbers

Summary: Terabytes aren't Web Scale.

Reviewed By: ot, philippv, luciang

Differential Revision: D7479752

fbshipit-source-id: fd353933e642b1ee024c28107c8fcb6a55fe755e
parent 29fd00d2
...@@ -284,6 +284,8 @@ const PrettySuffix kPrettyTimeSuffixes[] = { ...@@ -284,6 +284,8 @@ const PrettySuffix kPrettyTimeSuffixes[] = {
}; };
const PrettySuffix kPrettyBytesMetricSuffixes[] = { const PrettySuffix kPrettyBytesMetricSuffixes[] = {
{ "EB", 1e18L },
{ "PB", 1e15L },
{ "TB", 1e12L }, { "TB", 1e12L },
{ "GB", 1e9L }, { "GB", 1e9L },
{ "MB", 1e6L }, { "MB", 1e6L },
...@@ -293,6 +295,8 @@ const PrettySuffix kPrettyBytesMetricSuffixes[] = { ...@@ -293,6 +295,8 @@ const PrettySuffix kPrettyBytesMetricSuffixes[] = {
}; };
const PrettySuffix kPrettyBytesBinarySuffixes[] = { const PrettySuffix kPrettyBytesBinarySuffixes[] = {
{ "EB", int64_t(1) << 60 },
{ "PB", int64_t(1) << 50 },
{ "TB", int64_t(1) << 40 }, { "TB", int64_t(1) << 40 },
{ "GB", int64_t(1) << 30 }, { "GB", int64_t(1) << 30 },
{ "MB", int64_t(1) << 20 }, { "MB", int64_t(1) << 20 },
...@@ -302,6 +306,8 @@ const PrettySuffix kPrettyBytesBinarySuffixes[] = { ...@@ -302,6 +306,8 @@ const PrettySuffix kPrettyBytesBinarySuffixes[] = {
}; };
const PrettySuffix kPrettyBytesBinaryIECSuffixes[] = { const PrettySuffix kPrettyBytesBinaryIECSuffixes[] = {
{ "EiB", int64_t(1) << 60 },
{ "PiB", int64_t(1) << 50 },
{ "TiB", int64_t(1) << 40 }, { "TiB", int64_t(1) << 40 },
{ "GiB", int64_t(1) << 30 }, { "GiB", int64_t(1) << 30 },
{ "MiB", int64_t(1) << 20 }, { "MiB", int64_t(1) << 20 },
...@@ -311,6 +317,8 @@ const PrettySuffix kPrettyBytesBinaryIECSuffixes[] = { ...@@ -311,6 +317,8 @@ const PrettySuffix kPrettyBytesBinaryIECSuffixes[] = {
}; };
const PrettySuffix kPrettyUnitsMetricSuffixes[] = { const PrettySuffix kPrettyUnitsMetricSuffixes[] = {
{ "qntl", 1e18L },
{ "qdrl", 1e15L },
{ "tril", 1e12L }, { "tril", 1e12L },
{ "bil", 1e9L }, { "bil", 1e9L },
{ "M", 1e6L }, { "M", 1e6L },
...@@ -320,6 +328,8 @@ const PrettySuffix kPrettyUnitsMetricSuffixes[] = { ...@@ -320,6 +328,8 @@ const PrettySuffix kPrettyUnitsMetricSuffixes[] = {
}; };
const PrettySuffix kPrettyUnitsBinarySuffixes[] = { const PrettySuffix kPrettyUnitsBinarySuffixes[] = {
{ "E", int64_t(1) << 60 },
{ "P", int64_t(1) << 50 },
{ "T", int64_t(1) << 40 }, { "T", int64_t(1) << 40 },
{ "G", int64_t(1) << 30 }, { "G", int64_t(1) << 30 },
{ "M", int64_t(1) << 20 }, { "M", int64_t(1) << 20 },
...@@ -329,6 +339,8 @@ const PrettySuffix kPrettyUnitsBinarySuffixes[] = { ...@@ -329,6 +339,8 @@ const PrettySuffix kPrettyUnitsBinarySuffixes[] = {
}; };
const PrettySuffix kPrettyUnitsBinaryIECSuffixes[] = { const PrettySuffix kPrettyUnitsBinaryIECSuffixes[] = {
{ "Ei", int64_t(1) << 60 },
{ "Pi", int64_t(1) << 50 },
{ "Ti", int64_t(1) << 40 }, { "Ti", int64_t(1) << 40 },
{ "Gi", int64_t(1) << 30 }, { "Gi", int64_t(1) << 30 },
{ "Mi", int64_t(1) << 20 }, { "Mi", int64_t(1) << 20 },
...@@ -430,8 +442,7 @@ double prettyToDouble(folly::StringPiece *const prettyString, ...@@ -430,8 +442,7 @@ double prettyToDouble(folly::StringPiece *const prettyString,
} }
if (bestPrefixId == -1) { //No valid suffix rule found if (bestPrefixId == -1) { //No valid suffix rule found
throw std::invalid_argument(folly::to<std::string>( throw std::invalid_argument(folly::to<std::string>(
"Unable to parse suffix \"", "Unable to parse suffix \"", *prettyString, "\""));
prettyString->toString(), "\""));
} }
prettyString->advance(size_t(longestPrefixLen)); prettyString->advance(size_t(longestPrefixLen));
return suffixes[bestPrefixId].val ? value * suffixes[bestPrefixId].val : return suffixes[bestPrefixId].val ? value * suffixes[bestPrefixId].val :
......
...@@ -334,6 +334,8 @@ PrettyTestCase prettyTestCases[] = ...@@ -334,6 +334,8 @@ PrettyTestCase prettyTestCases[] =
{string("1 MB"), pow2(20), PRETTY_BYTES}, {string("1 MB"), pow2(20), PRETTY_BYTES},
{string("1 GB"), pow2(30), PRETTY_BYTES}, {string("1 GB"), pow2(30), PRETTY_BYTES},
{string("1 TB"), pow2(40), PRETTY_BYTES}, {string("1 TB"), pow2(40), PRETTY_BYTES},
{string("1 PB"), pow2(50), PRETTY_BYTES},
{string("1 EB"), pow2(60), PRETTY_BYTES},
{string("853 B "), 853., PRETTY_BYTES_IEC}, {string("853 B "), 853., PRETTY_BYTES_IEC},
{string("833 KiB"), 853.e3, PRETTY_BYTES_IEC}, {string("833 KiB"), 853.e3, PRETTY_BYTES_IEC},
...@@ -341,6 +343,8 @@ PrettyTestCase prettyTestCases[] = ...@@ -341,6 +343,8 @@ PrettyTestCase prettyTestCases[] =
{string("7.944 GiB"), 8.53e9, PRETTY_BYTES_IEC}, {string("7.944 GiB"), 8.53e9, PRETTY_BYTES_IEC},
{string("794.4 GiB"), 853.e9, PRETTY_BYTES_IEC}, {string("794.4 GiB"), 853.e9, PRETTY_BYTES_IEC},
{string("775.8 TiB"), 853.e12, PRETTY_BYTES_IEC}, {string("775.8 TiB"), 853.e12, PRETTY_BYTES_IEC},
{string("1.776 PiB"), 2e15, PRETTY_BYTES_IEC},
{string("1.735 EiB"), 2e18, PRETTY_BYTES_IEC},
{string("0 B "), 0, PRETTY_BYTES_IEC}, {string("0 B "), 0, PRETTY_BYTES_IEC},
{string("1 B "), pow2(0), PRETTY_BYTES_IEC}, {string("1 B "), pow2(0), PRETTY_BYTES_IEC},
...@@ -348,6 +352,8 @@ PrettyTestCase prettyTestCases[] = ...@@ -348,6 +352,8 @@ PrettyTestCase prettyTestCases[] =
{string("1 MiB"), pow2(20), PRETTY_BYTES_IEC}, {string("1 MiB"), pow2(20), PRETTY_BYTES_IEC},
{string("1 GiB"), pow2(30), PRETTY_BYTES_IEC}, {string("1 GiB"), pow2(30), PRETTY_BYTES_IEC},
{string("1 TiB"), pow2(40), PRETTY_BYTES_IEC}, {string("1 TiB"), pow2(40), PRETTY_BYTES_IEC},
{string("1 PiB"), pow2(50), PRETTY_BYTES_IEC},
{string("1 EiB"), pow2(60), PRETTY_BYTES_IEC},
// check bytes metric printing // check bytes metric printing
{string("853 B "), 853., PRETTY_BYTES_METRIC}, {string("853 B "), 853., PRETTY_BYTES_METRIC},
...@@ -361,9 +367,10 @@ PrettyTestCase prettyTestCases[] = ...@@ -361,9 +367,10 @@ PrettyTestCase prettyTestCases[] =
{string("1 B "), 1.0, PRETTY_BYTES_METRIC}, {string("1 B "), 1.0, PRETTY_BYTES_METRIC},
{string("1 kB"), 1.0e+3, PRETTY_BYTES_METRIC}, {string("1 kB"), 1.0e+3, PRETTY_BYTES_METRIC},
{string("1 MB"), 1.0e+6, PRETTY_BYTES_METRIC}, {string("1 MB"), 1.0e+6, PRETTY_BYTES_METRIC},
{string("1 GB"), 1.0e+9, PRETTY_BYTES_METRIC}, {string("1 GB"), 1.0e+9, PRETTY_BYTES_METRIC},
{string("1 TB"), 1.0e+12, PRETTY_BYTES_METRIC}, {string("1 TB"), 1.0e+12, PRETTY_BYTES_METRIC},
{string("1 PB"), 1.0e+15, PRETTY_BYTES_METRIC},
{string("1 EB"), 1.0e+18, PRETTY_BYTES_METRIC},
// check metric-units (powers of 1000) printing // check metric-units (powers of 1000) printing
{string("853 "), 853., PRETTY_UNITS_METRIC}, {string("853 "), 853., PRETTY_UNITS_METRIC},
...@@ -432,7 +439,7 @@ TEST(PrettyToDouble, Basic) { ...@@ -432,7 +439,7 @@ TEST(PrettyToDouble, Basic) {
double recoveredX = 0; double recoveredX = 0;
try{ try{
recoveredX = prettyToDouble(testString, formatType); recoveredX = prettyToDouble(testString, formatType);
} catch (const std::range_error& ex) { } catch (const std::exception& ex) {
ADD_FAILURE() << testCase.prettyString << " -> " << ex.what(); ADD_FAILURE() << testCase.prettyString << " -> " << ex.what();
} }
double relativeError = fabs(x) < 1e-5 ? (x-recoveredX) : double relativeError = fabs(x) < 1e-5 ? (x-recoveredX) :
...@@ -450,8 +457,8 @@ TEST(PrettyToDouble, Basic) { ...@@ -450,8 +457,8 @@ TEST(PrettyToDouble, Basic) {
try{ try{
recoveredX = prettyToDouble(prettyPrint(x, formatType, addSpace), recoveredX = prettyToDouble(prettyPrint(x, formatType, addSpace),
formatType); formatType);
} catch (std::range_error&) { } catch (const std::exception& ex) {
ADD_FAILURE(); ADD_FAILURE() << folly::exceptionStr(ex);
} }
double relativeError = (x - recoveredX) / x; double relativeError = (x - recoveredX) / x;
EXPECT_NEAR(0, relativeError, 1e-3); EXPECT_NEAR(0, relativeError, 1e-3);
......
Markdown is supported
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