19 #include <gtest/gtest.h>
20 #include <boost/scoped_ptr.hpp>
28 using namespace rapidjson;
32 template <
typename M,
typename T>
34 const string& human_readable) {
35 EXPECT_EQ(metric->value(), value);
36 if (!human_readable.empty()) {
37 EXPECT_EQ(metric->ToHumanReadable(), human_readable);
41 TEST(MetricsTest, CounterMetrics) {
45 int_counter->Increment(1);
47 int_counter->Increment(10);
49 int_counter->set_value(3456);
53 metrics.
AddCounter(
"counter_with_units", 10L, TUnit::BYTES);
57 TEST(MetricsTest, GaugeMetrics) {
61 int_gauge->Increment(-1);
63 int_gauge->Increment(10);
65 int_gauge->set_value(3456);
69 metrics.
AddGauge(
"gauge_with_units", 10L, TUnit::TIME_S);
73 TEST(MetricsTest, PropertyMetrics) {
77 bool_property->set_value(
true);
83 string_property->set_value(
"string2");
87 TEST(MetricsTest, NonFiniteValues) {
89 double inf = numeric_limits<double>::infinity();
92 double nan = numeric_limits<double>::quiet_NaN();
93 gauge->set_value(nan);
94 EXPECT_TRUE(isnan(gauge->value()));
95 EXPECT_TRUE(gauge->ToHumanReadable() ==
"nan");
98 TEST(MetricsTest, SetMetrics) {
101 item_set.insert(4); item_set.insert(5); item_set.insert(6);
114 TEST(MetricsTest, StatsMetrics) {
121 stats_metric->
Update(0.0);
122 stats_metric->
Update(1.0);
123 stats_metric->
Update(2.0);
125 EXPECT_EQ(stats_metric->
ToHumanReadable(),
"count: 3, last: 2.000000, min: 0.000000, "
126 "max: 2.000000, mean: 1.000000, stddev: 0.816497");
130 EXPECT_EQ(stats_metric_with_units->ToHumanReadable(),
"count: 0");
132 stats_metric_with_units->Update(0.0);
133 stats_metric_with_units->Update(1.0);
134 stats_metric_with_units->Update(2.0);
136 EXPECT_EQ(stats_metric_with_units->ToHumanReadable(),
"count: 3, last: 2.00 B, min: 0, "
137 "max: 2.00 B, mean: 1.00 B, stddev: 0.82 B");
141 #ifndef ADDRESS_SANITIZER
147 DCHECK(bytes_in_use != NULL);
149 uint64_t cur_in_use = bytes_in_use->value();
150 EXPECT_GT(cur_in_use, 0);
155 scoped_ptr<vector<uint64_t> > chunk(
new vector<uint64_t>(10 * 1024 * 1024));
156 EXPECT_GT(bytes_in_use->value(), cur_in_use);
160 DCHECK(total_bytes_reserved != NULL);
161 DCHECK_GT(total_bytes_reserved->value(), 0);
165 DCHECK(pageheap_free_bytes != NULL);
169 EXPECT_TRUE(pageheap_unmapped_bytes != NULL);
173 TEST(MetricsTest, JvmMetrics) {
178 "jvm.total.current-usage-bytes");
179 ASSERT_TRUE(jvm_total_used != NULL);
180 EXPECT_GT(jvm_total_used->value(), 0);
183 "jvm.total.peak-current-usage-bytes");
184 ASSERT_TRUE(jvm_peak_total_used != NULL);
185 EXPECT_GT(jvm_peak_total_used->value(), 0);
189 const string& description,
const string& kind=
"",
const string& units=
"") {
190 EXPECT_EQ(val[
"name"].GetString(), name);
191 EXPECT_EQ(val[
"human_readable"].GetString(), value);
192 EXPECT_EQ(val[
"description"].GetString(), description);
193 if (!kind.empty()) EXPECT_EQ(val[
"kind"].GetString(), kind);
194 if (!units.empty()) EXPECT_EQ(val[
"units"].GetString(), units);
197 TEST(MetricsJsonTest, Counters) {
202 metrics.
ToJson(
true, &document, &val);
203 const Value& counter_val = val[
"metrics"][0u];
204 AssertJson(counter_val,
"counter",
"0",
"",
"COUNTER",
"UNIT");
205 EXPECT_EQ(counter_val[
"value"].GetInt(), 0);
208 TEST(MetricsJsonTest, Gauges) {
213 metrics.
ToJson(
true, &document, &val);
214 AssertJson(val[
"metrics"][0u],
"gauge",
"10",
"",
"GAUGE",
"NONE");
215 EXPECT_EQ(val[
"metrics"][0u][
"value"].GetInt(), 10);
218 TEST(MetricsJsonTest, Properties) {
220 metrics.
AddProperty(
"property",
string(
"my value"));
223 metrics.
ToJson(
true, &document, &val);
225 AssertJson(val[
"metrics"][0u],
"property",
"my value",
"",
"PROPERTY",
"NONE");
226 EXPECT_EQ(
string(val[
"metrics"][0u][
"value"].GetString()),
"my value");
229 TEST(MetricsJsonTest, SetMetrics) {
232 item_set.insert(4); item_set.insert(5); item_set.insert(6);
237 metrics.
ToJson(
true, &document, &val);
238 const Value& set_val = val[
"metrics"][0u];
239 ASSERT_TRUE(set_val[
"items"].IsArray());
240 EXPECT_EQ(set_val[
"items"].Size(), 3);
241 EXPECT_EQ(set_val[
"items"][0u].GetInt(), 4);
242 EXPECT_EQ(set_val[
"items"][1].GetInt(), 5);
243 EXPECT_EQ(set_val[
"items"][2].GetInt(), 6);
248 TEST(MetricsJsonTest, StatsMetrics) {
256 metrics.
ToJson(
true, &document, &val);
257 const Value& stats_val = val[
"metrics"][0u];
258 AssertJson(stats_val,
"stats_metric",
"count: 2, last: 20.000000, min: 10.000000, "
259 "max: 20.000000, mean: 15.000000, stddev: 5.000000",
"",
"",
"UNIT");
261 EXPECT_EQ(stats_val[
"count"].GetInt(), 2);
262 EXPECT_EQ(stats_val[
"last"].GetDouble(), 20.0);
263 EXPECT_EQ(stats_val[
"min"].GetDouble(), 10.0);
264 EXPECT_EQ(stats_val[
"max"].GetDouble(), 20.0);
265 EXPECT_EQ(stats_val[
"mean"].GetDouble(), 15.0);
266 EXPECT_EQ(stats_val[
"stddev"].GetDouble(), 5.0);
269 TEST(MetricsJsonTest, UnitsAndDescription) {
271 metrics.
AddCounter(
"counter", 2048, TUnit::BYTES,
"description");
274 metrics.
ToJson(
true, &document, &val);
275 const Value& counter_val = val[
"metrics"][0u];
276 AssertJson(counter_val,
"counter",
"2.00 KB",
"description",
"COUNTER",
"BYTES");
277 EXPECT_EQ(counter_val[
"value"].GetInt(), 2048);
280 TEST(MetricGroupTest, JsonTest) {
282 metrics.
AddCounter(
"counter1", 2048, TUnit::BYTES,
"description");
283 metrics.
AddCounter(
"counter2", 2048, TUnit::BYTES,
"description");
289 ASSERT_NE(counter, reinterpret_cast<IntCounter*>(NULL));
293 metrics.
ToJson(
true, &document, &val);
294 EXPECT_EQ(val[
"metrics"].Size(), 2);
295 EXPECT_EQ(val[
"name"].GetString(),
string(
"JsonTest"));
297 EXPECT_EQ(val[
"child_groups"].Size(), 2);
298 EXPECT_EQ(val[
"child_groups"][0u][
"name"].GetString(),
string(
"child1"));
299 EXPECT_EQ(val[
"child_groups"][0u][
"metrics"].Size(), 0);
300 EXPECT_EQ(val[
"child_groups"][1][
"name"].GetString(),
string(
"child2"));
301 EXPECT_EQ(val[
"child_groups"][1][
"metrics"].Size(), 1);
306 int main(
int argc,
char **argv) {
307 google::InitGoogleLogging(argv[0]);
308 ::testing::InitGoogleTest(&argc, argv);
312 return RUN_ALL_TESTS();
SimpleMetric< T, TMetricKind::COUNTER > * AddCounter(const std::string &key, const T &value, const TUnit::type unit=TUnit::UNIT, const std::string &description="")
class SimpleMetric< std::string, TMetricKind::PROPERTY > StringProperty
void Remove(const T &item)
Remove an item from this set by value.
TEST(MetricGroupTest, JsonTest)
class SimpleMetric< int64_t, TMetricKind::COUNTER > IntCounter
M * RegisterMetric(M *metric)
MetricGroup * GetChildGroup(const std::string &name)
Creates or returns an already existing child metric group.
Status RegisterMemoryMetrics(MetricGroup *metrics, bool register_jvm_metrics)
MetricGroups may be organised hierarchically as a tree.
void Add(const T &item)
Put an item in this set.
static Status Init()
Find JniUtil class, and get JniUtil.throwableToString method id.
void AssertJson(const Value &val, const string &name, const string &value, const string &description, const string &kind="", const string &units="")
M * FindMetricForTesting(const std::string &key)
Used for testing only.
virtual std::string ToHumanReadable()
void InitThreading()
Initialises the threading subsystem. Must be called before a Thread is created.
SimpleMetric< T, TMetricKind::PROPERTY > * AddProperty(const std::string &key, const T &value, const std::string &description="")
virtual std::string ToHumanReadable()
void Update(const T &value)
Metric whose value is a set of items.
void AssertValue(M *metric, const T &value, const string &human_readable)
class SimpleMetric< bool, TMetricKind::PROPERTY > BooleanProperty
int main(int argc, char **argv)
class SimpleMetric< double, TMetricKind::GAUGE > DoubleGauge
void ToJson(bool include_children, rapidjson::Document *document, rapidjson::Value *out_val)
Converts this metric group (and optionally all of its children recursively) to JSON.
SimpleMetric< T > * AddGauge(const std::string &key, const T &value, const TUnit::type unit=TUnit::NONE, const std::string &description="")
Create a gauge metric object with given key and initial value (owned by this object) ...