Gauge metric e2e test

Test: new test passed
Change-Id: I6a8bceb43fbb6e7b82f47951e71b5620779f2ceb
This commit is contained in:
Yangster-mac
2018-01-11 16:16:26 -08:00
parent 278050ed4e
commit 87718e283a
10 changed files with 263 additions and 25 deletions

View File

@@ -186,7 +186,8 @@ LOCAL_SRC_FILES := \
tests/statsd_test_util.cpp \
tests/e2e/WakelockDuration_e2e_test.cpp \
tests/e2e/MetricConditionLink_e2e_test.cpp \
tests/e2e/Attribution_e2e_test.cpp
tests/e2e/Attribution_e2e_test.cpp \
tests/e2e/GaugeMetric_e2e_test.cpp
LOCAL_STATIC_LIBRARIES := \
$(statsd_common_static_libraries) \

View File

@@ -99,6 +99,7 @@ private:
FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensions);
FRIEND_TEST(MetricConditionLinkE2eTest, TestMultiplePredicatesAndLinks);
FRIEND_TEST(AttributionE2eTest, TestAttributionMatchAndSlice);
FRIEND_TEST(GaugeMetricE2eTest, TestMultipleFieldsForPushedEvent);
};

View File

@@ -32,6 +32,10 @@ namespace statsd {
const DimensionsValue* getSingleLeafValue(const DimensionsValue* value);
DimensionsValue getSingleLeafValue(const DimensionsValue& value);
// Appends the leaf node to the parent tree.
void appendLeafNodeToParent(const Field& field, const DimensionsValue& value,
DimensionsValue* parentValue);
// Constructs the DimensionsValue protos from the FieldMatcher. Each DimensionsValue proto
// represents a tree. When the input proto has repeated fields and the input "dimensions" wants
// "ANY" locations, it will return multiple trees.

View File

@@ -114,6 +114,9 @@ GaugeMetricProducer::~GaugeMetricProducer() {
void GaugeMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs, StatsLogReport* report) {
flushIfNeededLocked(dumpTimeNs);
ProtoOutputStream pbOutput;
onDumpReportLocked(dumpTimeNs, &pbOutput);
parseProtoOutputStream(pbOutput, report);
}
void GaugeMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs,

View File

@@ -141,6 +141,7 @@ private:
FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensions);
FRIEND_TEST(MetricConditionLinkE2eTest, TestMultiplePredicatesAndLinks);
FRIEND_TEST(AttributionE2eTest, TestAttributionMatchAndSlice);
FRIEND_TEST(GaugeMetricE2eTest, TestMultipleFieldsForPushedEvent);
};
} // namespace statsd

View File

@@ -43,6 +43,19 @@ int64_t TimeUnitToBucketSizeInMillis(TimeUnit unit);
// Helper function to write PulledAtomStats to ProtoOutputStream
void writePullerStatsToStream(const std::pair<int, StatsdStats::PulledAtomStats>& pair,
util::ProtoOutputStream* protoOutput);
template<class T>
bool parseProtoOutputStream(util::ProtoOutputStream& protoOutput, T* message) {
std::string pbBytes;
auto iter = protoOutput.data();
while (iter.readBuffer() != NULL) {
size_t toRead = iter.currentToRead();
pbBytes.append(reinterpret_cast<const char*>(iter.readBuffer()), toRead);
iter.rp()->move(toRead);
}
return message->ParseFromArray(pbBytes.c_str(), pbBytes.size());
}
} // namespace statsd
} // namespace os
} // namespace android

View File

@@ -0,0 +1,193 @@
// Copyright (C) 2017 The Android Open Source Project
//
// 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 <gtest/gtest.h>
#include "src/StatsLogProcessor.h"
#include "src/stats_log_util.h"
#include "tests/statsd_test_util.h"
#include <vector>
namespace android {
namespace os {
namespace statsd {
#ifdef __ANDROID__
namespace {
StatsdConfig CreateStatsdConfigForPushedEvent() {
StatsdConfig config;
*config.add_atom_matcher() = CreateMoveToBackgroundAtomMatcher();
*config.add_atom_matcher() = CreateMoveToForegroundAtomMatcher();
auto atomMatcher = CreateSimpleAtomMatcher("", android::util::APP_START_CHANGED);
*config.add_atom_matcher() = atomMatcher;
auto isInBackgroundPredicate = CreateIsInBackgroundPredicate();
*isInBackgroundPredicate.mutable_simple_predicate()->mutable_dimensions() =
CreateDimensions(android::util::ACTIVITY_FOREGROUND_STATE_CHANGED, {1 /* uid field */ });
*config.add_predicate() = isInBackgroundPredicate;
auto gaugeMetric = config.add_gauge_metric();
gaugeMetric->set_id(123456);
gaugeMetric->set_what(atomMatcher.id());
gaugeMetric->set_condition(isInBackgroundPredicate.id());
gaugeMetric->mutable_gauge_fields_filter()->set_include_all(false);
auto fieldMatcher = gaugeMetric->mutable_gauge_fields_filter()->mutable_fields();
fieldMatcher->set_field(android::util::APP_START_CHANGED);
fieldMatcher->add_child()->set_field(3); // type (enum)
fieldMatcher->add_child()->set_field(4); // activity_name(str)
fieldMatcher->add_child()->set_field(7); // activity_start_msec(int64)
*gaugeMetric->mutable_dimensions() =
CreateDimensions(android::util::APP_START_CHANGED, {1 /* uid field */ });
gaugeMetric->set_bucket(ONE_MINUTE);
auto links = gaugeMetric->add_links();
links->set_condition(isInBackgroundPredicate.id());
auto dimensionWhat = links->mutable_dimensions_in_what();
dimensionWhat->set_field(android::util::APP_START_CHANGED);
dimensionWhat->add_child()->set_field(1); // uid field.
auto dimensionCondition = links->mutable_dimensions_in_condition();
dimensionCondition->set_field(android::util::ACTIVITY_FOREGROUND_STATE_CHANGED);
dimensionCondition->add_child()->set_field(1); // uid field.
return config;
}
std::unique_ptr<LogEvent> CreateAppStartChangedEvent(
const int uid, const string& pkg_name, AppStartChanged::TransitionType type,
const string& activity_name, const string& calling_pkg_name, const bool is_instant_app,
int64_t activity_start_msec, uint64_t timestampNs) {
auto logEvent = std::make_unique<LogEvent>(
android::util::APP_START_CHANGED, timestampNs);
logEvent->write(uid);
logEvent->write(pkg_name);
logEvent->write(type);
logEvent->write(activity_name);
logEvent->write(calling_pkg_name);
logEvent->write(is_instant_app);
logEvent->write(activity_start_msec);
logEvent->init();
return logEvent;
}
} // namespace
TEST(GaugeMetricE2eTest, TestMultipleFieldsForPushedEvent) {
auto config = CreateStatsdConfigForPushedEvent();
int64_t bucketStartTimeNs = 10000000000;
int64_t bucketSizeNs =
TimeUnitToBucketSizeInMillis(config.gauge_metric(0).bucket()) * 1000000;
ConfigKey cfgKey;
auto processor = CreateStatsLogProcessor(bucketStartTimeNs / NS_PER_SEC, config, cfgKey);
EXPECT_EQ(processor->mMetricsManagers.size(), 1u);
EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid());
int appUid1 = 123;
int appUid2 = 456;
std::vector<std::unique_ptr<LogEvent>> events;
events.push_back(CreateMoveToBackgroundEvent(appUid1, bucketStartTimeNs + 15));
events.push_back(CreateMoveToForegroundEvent(appUid1, bucketStartTimeNs + bucketSizeNs + 250));
events.push_back(CreateMoveToBackgroundEvent(appUid1, bucketStartTimeNs + bucketSizeNs + 350));
events.push_back(CreateMoveToForegroundEvent(
appUid1, bucketStartTimeNs + 2 * bucketSizeNs + 100));
events.push_back(CreateAppStartChangedEvent(
appUid1, "app1", AppStartChanged::WARM, "activity_name1", "calling_pkg_name1",
true /*is_instant_app*/, 101 /*activity_start_msec*/, bucketStartTimeNs + 10));
events.push_back(CreateAppStartChangedEvent(
appUid1, "app1", AppStartChanged::HOT, "activity_name2", "calling_pkg_name2",
true /*is_instant_app*/, 102 /*activity_start_msec*/, bucketStartTimeNs + 20));
events.push_back(CreateAppStartChangedEvent(
appUid1, "app1", AppStartChanged::COLD, "activity_name3", "calling_pkg_name3",
true /*is_instant_app*/, 103 /*activity_start_msec*/, bucketStartTimeNs + 30));
events.push_back(CreateAppStartChangedEvent(
appUid1, "app1", AppStartChanged::WARM, "activity_name4", "calling_pkg_name4",
true /*is_instant_app*/, 104 /*activity_start_msec*/,
bucketStartTimeNs + bucketSizeNs + 30));
events.push_back(CreateAppStartChangedEvent(
appUid1, "app1", AppStartChanged::COLD, "activity_name5", "calling_pkg_name5",
true /*is_instant_app*/, 105 /*activity_start_msec*/,
bucketStartTimeNs + 2 * bucketSizeNs));
events.push_back(CreateAppStartChangedEvent(
appUid1, "app1", AppStartChanged::HOT, "activity_name6", "calling_pkg_name6",
false /*is_instant_app*/, 106 /*activity_start_msec*/,
bucketStartTimeNs + 2 * bucketSizeNs + 10));
events.push_back(CreateMoveToBackgroundEvent(appUid2, bucketStartTimeNs + bucketSizeNs + 10));
events.push_back(CreateAppStartChangedEvent(
appUid2, "app2", AppStartChanged::COLD, "activity_name7", "calling_pkg_name7",
true /*is_instant_app*/, 201 /*activity_start_msec*/,
bucketStartTimeNs + 2 * bucketSizeNs + 10));
sortLogEventsByTimestamp(&events);
for (const auto& event : events) {
processor->OnLogEvent(event.get());
}
ConfigMetricsReportList reports;
processor->onDumpReport(cfgKey, bucketStartTimeNs + 3 * bucketSizeNs, &reports);
EXPECT_EQ(reports.reports_size(), 1);
EXPECT_EQ(reports.reports(0).metrics_size(), 1);
StatsLogReport::GaugeMetricDataWrapper gaugeMetrics;
sortMetricDataByDimensionsValue(reports.reports(0).metrics(0).gauge_metrics(), &gaugeMetrics);
EXPECT_EQ(gaugeMetrics.data_size(), 2);
auto data = gaugeMetrics.data(0);
EXPECT_EQ(data.dimension().field(), android::util::APP_START_CHANGED);
EXPECT_EQ(data.dimension().value_tuple().dimensions_value_size(), 1);
EXPECT_EQ(data.dimension().value_tuple().dimensions_value(0).field(), 1 /* uid field */);
EXPECT_EQ(data.dimension().value_tuple().dimensions_value(0).value_int(), appUid1);
EXPECT_EQ(data.bucket_info_size(), 3);
EXPECT_EQ(data.bucket_info(0).start_bucket_nanos(), bucketStartTimeNs);
EXPECT_EQ(data.bucket_info(0).end_bucket_nanos(), bucketStartTimeNs + bucketSizeNs);
EXPECT_EQ(data.bucket_info(0).atom().app_start_changed().type(), AppStartChanged::HOT);
EXPECT_EQ(data.bucket_info(0).atom().app_start_changed().activity_name(), "activity_name2");
EXPECT_EQ(data.bucket_info(0).atom().app_start_changed().activity_start_msec(), 102L);
EXPECT_EQ(data.bucket_info(1).start_bucket_nanos(), bucketStartTimeNs + bucketSizeNs);
EXPECT_EQ(data.bucket_info(1).end_bucket_nanos(), bucketStartTimeNs + 2 * bucketSizeNs);
EXPECT_EQ(data.bucket_info(1).atom().app_start_changed().type(), AppStartChanged::WARM);
EXPECT_EQ(data.bucket_info(1).atom().app_start_changed().activity_name(), "activity_name4");
EXPECT_EQ(data.bucket_info(1).atom().app_start_changed().activity_start_msec(), 104L);
EXPECT_EQ(data.bucket_info(2).start_bucket_nanos(), bucketStartTimeNs + 2 * bucketSizeNs);
EXPECT_EQ(data.bucket_info(2).end_bucket_nanos(), bucketStartTimeNs + 3 * bucketSizeNs);
EXPECT_EQ(data.bucket_info(2).atom().app_start_changed().type(), AppStartChanged::COLD);
EXPECT_EQ(data.bucket_info(2).atom().app_start_changed().activity_name(), "activity_name5");
EXPECT_EQ(data.bucket_info(2).atom().app_start_changed().activity_start_msec(), 105L);
data = gaugeMetrics.data(1);
EXPECT_EQ(data.dimension().field(), android::util::APP_START_CHANGED);
EXPECT_EQ(data.dimension().value_tuple().dimensions_value_size(), 1);
EXPECT_EQ(data.dimension().value_tuple().dimensions_value(0).field(), 1 /* uid field */);
EXPECT_EQ(data.dimension().value_tuple().dimensions_value(0).value_int(), appUid2);
EXPECT_EQ(data.bucket_info_size(), 1);
EXPECT_EQ(data.bucket_info(0).start_bucket_nanos(), bucketStartTimeNs + 2 * bucketSizeNs);
EXPECT_EQ(data.bucket_info(0).end_bucket_nanos(), bucketStartTimeNs + 3 * bucketSizeNs);
EXPECT_EQ(data.bucket_info(0).atom().app_start_changed().type(), AppStartChanged::COLD);
EXPECT_EQ(data.bucket_info(0).atom().app_start_changed().activity_name(), "activity_name7");
EXPECT_EQ(data.bucket_info(0).atom().app_start_changed().activity_start_msec(), 201L);
}
#else
GTEST_LOG_(INFO) << "This test does nothing.\n";
#endif
} // namespace statsd
} // namespace os
} // namespace android

View File

@@ -26,6 +26,8 @@ namespace statsd {
#ifdef __ANDROID__
namespace {
StatsdConfig CreateStatsdConfig(DurationMetric::AggregationType aggregationType) {
StatsdConfig config;
*config.add_atom_matcher() = CreateScreenTurnedOnAtomMatcher();
@@ -56,6 +58,8 @@ StatsdConfig CreateStatsdConfig(DurationMetric::AggregationType aggregationType)
return config;
}
} // namespace
TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensions) {
ConfigKey cfgKey;
for (auto aggregationType : { DurationMetric::SUM, DurationMetric::MAX_SPARSE }) {
@@ -94,6 +98,7 @@ TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensions) {
auto releaseEvent2 = CreateReleaseWakelockEvent(
attributions2, "wl2", bucketStartTimeNs + 2 * bucketSizeNs - 15);
std::vector<std::unique_ptr<LogEvent>> events;
events.push_back(std::move(screenTurnedOnEvent));
@@ -119,18 +124,8 @@ TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensions) {
auto data = reports.reports(0).metrics(0).duration_metrics().data(0);
// Validate dimension value.
EXPECT_EQ(data.dimension().field(),
android::util::WAKELOCK_STATE_CHANGED);
EXPECT_EQ(data.dimension().value_tuple().dimensions_value_size(), 1);
// Attribution field.
EXPECT_EQ(data.dimension().value_tuple().dimensions_value(0).field(), 1);
// Uid only.
EXPECT_EQ(data.dimension().value_tuple().dimensions_value(0)
.value_tuple().dimensions_value_size(), 1);
EXPECT_EQ(data.dimension().value_tuple().dimensions_value(0)
.value_tuple().dimensions_value(0).field(), 1);
EXPECT_EQ(data.dimension().value_tuple().dimensions_value(0)
.value_tuple().dimensions_value(0).value_int(), 111);
ValidateAttributionUidDimension(
data.dimension(), android::util::WAKELOCK_STATE_CHANGED, 111);
// Validate bucket info.
EXPECT_EQ(reports.reports(0).metrics(0).duration_metrics().data(0).bucket_info_size(), 1);
data = reports.reports(0).metrics(0).duration_metrics().data(0);
@@ -147,18 +142,8 @@ TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensions) {
EXPECT_EQ(reports.reports(0).metrics(0).duration_metrics().data(0).bucket_info_size(), 2);
data = reports.reports(0).metrics(0).duration_metrics().data(0);
// Validate dimension value.
EXPECT_EQ(data.dimension().field(),
android::util::WAKELOCK_STATE_CHANGED);
EXPECT_EQ(data.dimension().value_tuple().dimensions_value_size(), 1);
// Attribution field.
EXPECT_EQ(data.dimension().value_tuple().dimensions_value(0).field(), 1);
// Uid only.
EXPECT_EQ(data.dimension().value_tuple().dimensions_value(0)
.value_tuple().dimensions_value_size(), 1);
EXPECT_EQ(data.dimension().value_tuple().dimensions_value(0)
.value_tuple().dimensions_value(0).field(), 1);
EXPECT_EQ(data.dimension().value_tuple().dimensions_value(0)
.value_tuple().dimensions_value(0).value_int(), 111);
ValidateAttributionUidDimension(
data.dimension(), android::util::WAKELOCK_STATE_CHANGED, 111);
// Two output buckets.
// The wakelock holding interval in the 1st bucket starts from the screen off event and to
// the end of the 1st bucket.
@@ -167,6 +152,32 @@ TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensions) {
// The wakelock holding interval in the 2nd bucket starts at the beginning of the bucket and
// ends at the second screen on event.
EXPECT_EQ((unsigned long long)data.bucket_info(1).duration_nanos(), 500UL);
events.clear();
events.push_back(CreateScreenStateChangedEvent(
ScreenStateChanged::STATE_OFF, bucketStartTimeNs + 2 * bucketSizeNs + 90));
events.push_back(CreateAcquireWakelockEvent(
attributions1, "wl3", bucketStartTimeNs + 2 * bucketSizeNs + 100));
events.push_back(CreateReleaseWakelockEvent(
attributions1, "wl3", bucketStartTimeNs + 5 * bucketSizeNs + 100));
sortLogEventsByTimestamp(&events);
for (const auto& event : events) {
processor->OnLogEvent(event.get());
}
reports.Clear();
processor->onDumpReport(cfgKey, bucketStartTimeNs + 6 * bucketSizeNs + 1, &reports);
EXPECT_EQ(reports.reports_size(), 1);
EXPECT_EQ(reports.reports(0).metrics_size(), 1);
EXPECT_EQ(reports.reports(0).metrics(0).duration_metrics().data_size(), 1);
EXPECT_EQ(reports.reports(0).metrics(0).duration_metrics().data(0).bucket_info_size(), 6);
data = reports.reports(0).metrics(0).duration_metrics().data(0);
ValidateAttributionUidDimension(
data.dimension(), android::util::WAKELOCK_STATE_CHANGED, 111);
// The last wakelock holding spans 4 buckets.
EXPECT_EQ((unsigned long long)data.bucket_info(2).duration_nanos(), bucketSizeNs - 100);
EXPECT_EQ((unsigned long long)data.bucket_info(3).duration_nanos(), bucketSizeNs);
EXPECT_EQ((unsigned long long)data.bucket_info(4).duration_nanos(), bucketSizeNs);
EXPECT_EQ((unsigned long long)data.bucket_info(5).duration_nanos(), 100UL);
}
}

View File

@@ -19,6 +19,14 @@ namespace android {
namespace os {
namespace statsd {
AtomMatcher CreateSimpleAtomMatcher(const string& name, int atomId) {
AtomMatcher atom_matcher;
atom_matcher.set_id(StringToId(name));
auto simple_atom_matcher = atom_matcher.mutable_simple_atom_matcher();
simple_atom_matcher->set_atom_id(atomId);
return atom_matcher;
}
AtomMatcher CreateWakelockStateChangedAtomMatcher(const string& name,
WakelockStateChanged::State state) {
AtomMatcher atom_matcher;

View File

@@ -23,6 +23,9 @@ namespace android {
namespace os {
namespace statsd {
// Create AtomMatcher proto to simply match a specific atom type.
AtomMatcher CreateSimpleAtomMatcher(const string& name, int atomId);
// Create AtomMatcher proto for acquiring wakelock.
AtomMatcher CreateAcquireWakelockAtomMatcher();