protobuf/third_party/benchmark/src/string_util.cc
Go to the documentation of this file.
1 #include "string_util.h"
2 
3 #include <array>
4 #include <cmath>
5 #include <cstdarg>
6 #include <cstdio>
7 #include <memory>
8 #include <sstream>
9 
10 #include "arraysize.h"
11 
12 namespace benchmark {
13 namespace {
14 
15 // kilo, Mega, Giga, Tera, Peta, Exa, Zetta, Yotta.
16 const char kBigSIUnits[] = "kMGTPEZY";
17 // Kibi, Mebi, Gibi, Tebi, Pebi, Exbi, Zebi, Yobi.
18 const char kBigIECUnits[] = "KMGTPEZY";
19 // milli, micro, nano, pico, femto, atto, zepto, yocto.
20 const char kSmallSIUnits[] = "munpfazy";
21 
22 // We require that all three arrays have the same size.
23 static_assert(arraysize(kBigSIUnits) == arraysize(kBigIECUnits),
24  "SI and IEC unit arrays must be the same size");
25 static_assert(arraysize(kSmallSIUnits) == arraysize(kBigSIUnits),
26  "Small SI and Big SI unit arrays must be the same size");
27 
28 static const int64_t kUnitsSize = arraysize(kBigSIUnits);
29 
30 } // end anonymous namespace
31 
32 void ToExponentAndMantissa(double val, double thresh, int precision,
33  double one_k, std::string* mantissa,
34  int64_t* exponent) {
35  std::stringstream mantissa_stream;
36 
37  if (val < 0) {
38  mantissa_stream << "-";
39  val = -val;
40  }
41 
42  // Adjust threshold so that it never excludes things which can't be rendered
43  // in 'precision' digits.
44  const double adjusted_threshold =
45  std::max(thresh, 1.0 / std::pow(10.0, precision));
46  const double big_threshold = adjusted_threshold * one_k;
47  const double small_threshold = adjusted_threshold;
48  // Values in ]simple_threshold,small_threshold[ will be printed as-is
49  const double simple_threshold = 0.01;
50 
51  if (val > big_threshold) {
52  // Positive powers
53  double scaled = val;
54  for (size_t i = 0; i < arraysize(kBigSIUnits); ++i) {
55  scaled /= one_k;
56  if (scaled <= big_threshold) {
57  mantissa_stream << scaled;
58  *exponent = i + 1;
59  *mantissa = mantissa_stream.str();
60  return;
61  }
62  }
63  mantissa_stream << val;
64  *exponent = 0;
65  } else if (val < small_threshold) {
66  // Negative powers
67  if (val < simple_threshold) {
68  double scaled = val;
69  for (size_t i = 0; i < arraysize(kSmallSIUnits); ++i) {
70  scaled *= one_k;
71  if (scaled >= small_threshold) {
72  mantissa_stream << scaled;
73  *exponent = -static_cast<int64_t>(i + 1);
74  *mantissa = mantissa_stream.str();
75  return;
76  }
77  }
78  }
79  mantissa_stream << val;
80  *exponent = 0;
81  } else {
82  mantissa_stream << val;
83  *exponent = 0;
84  }
85  *mantissa = mantissa_stream.str();
86 }
87 
89  if (exponent == 0) return "";
90 
91  const int64_t index = (exponent > 0 ? exponent - 1 : -exponent - 1);
92  if (index >= kUnitsSize) return "";
93 
94  const char* array =
95  (exponent > 0 ? (iec ? kBigIECUnits : kBigSIUnits) : kSmallSIUnits);
96  if (iec)
97  return array[index] + std::string("i");
98  else
99  return std::string(1, array[index]);
100 }
101 
102 std::string ToBinaryStringFullySpecified(double value, double threshold,
103  int precision) {
106  ToExponentAndMantissa(value, threshold, precision, 1024.0, &mantissa,
107  &exponent);
108  return mantissa + ExponentToPrefix(exponent, false);
109 }
110 
111 void AppendHumanReadable(int n, std::string* str) {
112  std::stringstream ss;
113  // Round down to the nearest SI prefix.
114  ss << ToBinaryStringFullySpecified(n, 1.0, 0);
115  *str += ss.str();
116 }
117 
119  // 1.1 means that figures up to 1.1k should be shown with the next unit down;
120  // this softens edge effects.
121  // 1 means that we should show one decimal place of precision.
122  return ToBinaryStringFullySpecified(n, 1.1, 1);
123 }
124 
125 std::string StringPrintFImp(const char* msg, va_list args) {
126  // we might need a second shot at this, so pre-emptivly make a copy
127  va_list args_cp;
128  va_copy(args_cp, args);
129 
130  // TODO(ericwf): use std::array for first attempt to avoid one memory
131  // allocation guess what the size might be
132  std::array<char, 256> local_buff;
133  std::size_t size = local_buff.size();
134  // 2015-10-08: vsnprintf is used instead of snd::vsnprintf due to a limitation
135  // in the android-ndk
136  auto ret = vsnprintf(local_buff.data(), size, msg, args_cp);
137 
138  va_end(args_cp);
139 
140  // handle empty expansion
141  if (ret == 0) return std::string{};
142  if (static_cast<std::size_t>(ret) < size)
143  return std::string(local_buff.data());
144 
145  // we did not provide a long enough buffer on our first attempt.
146  // add 1 to size to account for null-byte in size cast to prevent overflow
147  size = static_cast<std::size_t>(ret) + 1;
148  auto buff_ptr = std::unique_ptr<char[]>(new char[size]);
149  // 2015-10-08: vsnprintf is used instead of snd::vsnprintf due to a limitation
150  // in the android-ndk
151  ret = vsnprintf(buff_ptr.get(), size, msg, args);
152  return std::string(buff_ptr.get());
153 }
154 
155 std::string StringPrintF(const char* format, ...) {
156  va_list args;
157  va_start(args, format);
159  va_end(args);
160  return tmp;
161 }
162 
164  const std::string& to) {
165  std::size_t start = 0;
166  while ((start = str->find(from, start)) != std::string::npos) {
167  str->replace(start, from.length(), to);
168  start += to.length();
169  }
170 }
171 
172 } // end namespace benchmark
xds_interop_client.str
str
Definition: xds_interop_client.py:487
vsnprintf
int __cdecl vsnprintf(char *buffer, size_t count, const char *format, va_list argptr)
Definition: libc.cpp:135
precision
int precision
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:448
http2_test_server.format
format
Definition: http2_test_server.py:118
benchmark
Definition: bm_alarm.cc:55
benchmark::ToBinaryStringFullySpecified
std::string ToBinaryStringFullySpecified(double value, double threshold, int precision)
Definition: bloaty/third_party/protobuf/third_party/benchmark/src/string_util.cc:102
exponent
int exponent
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1100
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
benchmark::ToExponentAndMantissa
void ToExponentAndMantissa(double val, double thresh, int precision, double one_k, std::string *mantissa, int64_t *exponent)
Definition: bloaty/third_party/protobuf/third_party/benchmark/src/string_util.cc:32
to
size_t to
Definition: abseil-cpp/absl/container/internal/layout_test.cc:1385
benchmark::ReplaceAll
void ReplaceAll(std::string *str, const std::string &from, const std::string &to)
Definition: bloaty/third_party/protobuf/third_party/benchmark/src/string_util.cc:163
from
size_t from
Definition: abseil-cpp/absl/container/internal/layout_test.cc:1384
start
static uint64_t start
Definition: benchmark-pound.c:74
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
array
Definition: undname.c:101
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
max
int max
Definition: bloaty/third_party/zlib/examples/enough.c:170
benchmark::ExponentToPrefix
std::string ExponentToPrefix(int64_t exponent, bool iec)
Definition: bloaty/third_party/protobuf/third_party/benchmark/src/string_util.cc:88
benchmark::HumanReadableNumber
std::string HumanReadableNumber(double n, double one_k)
Definition: benchmark/src/string_util.cc:121
mantissa
MantissaType mantissa
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1098
msg
std::string msg
Definition: client_interceptors_end2end_test.cc:372
arraysize
#define arraysize(array)
Definition: benchmark/src/arraysize.h:28
value
const char * value
Definition: hpack_parser_table.cc:165
arraysize.h
string_util.h
index
int index
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:1184
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
benchmark::StringPrintFImp
std::string StringPrintFImp(const char *msg, va_list args)
Definition: bloaty/third_party/protobuf/third_party/benchmark/src/string_util.cc:125
benchmark::StringPrintF
std::string StringPrintF(const char *format,...)
Definition: bloaty/third_party/protobuf/third_party/benchmark/src/string_util.cc:155
autogen_x86imm.tmp
tmp
Definition: autogen_x86imm.py:12
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
benchmark::AppendHumanReadable
void AppendHumanReadable(int n, std::string *str)
Definition: benchmark/src/string_util.cc:114
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:21