Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 1 | // Copyright (C) 2017 The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include <gtest/gtest.h> |
| 16 | |
| 17 | #include "src/StatsLogProcessor.h" |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 18 | #include "src/stats_log_util.h" |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 19 | #include "tests/statsd_test_util.h" |
| 20 | |
| 21 | #include <vector> |
| 22 | |
| 23 | namespace android { |
| 24 | namespace os { |
| 25 | namespace statsd { |
| 26 | |
| 27 | #ifdef __ANDROID__ |
| 28 | |
Yangster-mac | 87718e2 | 2018-01-11 16:16:26 -0800 | [diff] [blame] | 29 | namespace { |
| 30 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 31 | StatsdConfig CreateStatsdConfig(DurationMetric::AggregationType aggregationType) { |
| 32 | StatsdConfig config; |
David Chen | 8faaa01 | 2018-02-28 15:54:36 -0800 | [diff] [blame] | 33 | config.add_allowed_log_source("AID_ROOT"); // LogEvent defaults to UID of root. |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 34 | *config.add_atom_matcher() = CreateScreenTurnedOnAtomMatcher(); |
| 35 | *config.add_atom_matcher() = CreateScreenTurnedOffAtomMatcher(); |
| 36 | *config.add_atom_matcher() = CreateAcquireWakelockAtomMatcher(); |
| 37 | *config.add_atom_matcher() = CreateReleaseWakelockAtomMatcher(); |
| 38 | |
| 39 | auto screenIsOffPredicate = CreateScreenIsOffPredicate(); |
| 40 | *config.add_predicate() = screenIsOffPredicate; |
| 41 | |
| 42 | auto holdingWakelockPredicate = CreateHoldingWakelockPredicate(); |
| 43 | // The predicate is dimensioning by any attribution node and both by uid and tag. |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 44 | FieldMatcher dimensions = CreateAttributionUidAndTagDimensions( |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 45 | android::util::WAKELOCK_STATE_CHANGED, {Position::FIRST, Position::LAST}); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 46 | // Also slice by the wakelock tag |
| 47 | dimensions.add_child()->set_field(3); // The wakelock tag is set in field 3 of the wakelock. |
| 48 | *holdingWakelockPredicate.mutable_simple_predicate()->mutable_dimensions() = dimensions; |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 49 | *config.add_predicate() = holdingWakelockPredicate; |
| 50 | |
| 51 | auto durationMetric = config.add_duration_metric(); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 52 | durationMetric->set_id(StringToId("WakelockDuration")); |
| 53 | durationMetric->set_what(holdingWakelockPredicate.id()); |
| 54 | durationMetric->set_condition(screenIsOffPredicate.id()); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 55 | durationMetric->set_aggregation_type(aggregationType); |
| 56 | // The metric is dimensioning by first attribution node and only by uid. |
Yangster-mac | 468ff04 | 2018-01-17 12:26:34 -0800 | [diff] [blame] | 57 | *durationMetric->mutable_dimensions_in_what() = |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 58 | CreateAttributionUidDimensions( |
| 59 | android::util::WAKELOCK_STATE_CHANGED, {Position::FIRST}); |
yro | 59cc24d | 2018-02-13 20:17:32 -0800 | [diff] [blame] | 60 | durationMetric->set_bucket(FIVE_MINUTES); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 61 | return config; |
| 62 | } |
| 63 | |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 64 | std::vector<AttributionNodeInternal> attributions1 = {CreateAttribution(111, "App1"), |
| 65 | CreateAttribution(222, "GMSCoreModule1"), |
| 66 | CreateAttribution(222, "GMSCoreModule2")}; |
Yangster-mac | 87718e2 | 2018-01-11 16:16:26 -0800 | [diff] [blame] | 67 | |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 68 | std::vector<AttributionNodeInternal> attributions2 = {CreateAttribution(111, "App2"), |
| 69 | CreateAttribution(222, "GMSCoreModule1"), |
| 70 | CreateAttribution(222, "GMSCoreModule2")}; |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 71 | |
| 72 | /* |
| 73 | Events: |
| 74 | Screen off is met from (200ns,1 min+500ns]. |
| 75 | Acquire event for wl1 from 2ns to 1min+2ns |
| 76 | Acquire event for wl2 from 1min-10ns to 2min-15ns |
| 77 | */ |
| 78 | void FeedEvents(StatsdConfig config, sp<StatsLogProcessor> processor) { |
| 79 | uint64_t bucketStartTimeNs = 10000000000; |
| 80 | uint64_t bucketSizeNs = |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 81 | TimeUnitToBucketSizeInMillis(config.duration_metric(0).bucket()) * 1000000LL; |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 82 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 83 | auto screenTurnedOnEvent = CreateScreenStateChangedEvent( |
| 84 | android::view::DisplayStateEnum::DISPLAY_STATE_ON, bucketStartTimeNs + 1); |
| 85 | auto screenTurnedOffEvent = CreateScreenStateChangedEvent( |
| 86 | android::view::DisplayStateEnum::DISPLAY_STATE_OFF, bucketStartTimeNs + 200); |
| 87 | auto screenTurnedOnEvent2 = |
Bookatz | 1a1b046 | 2018-01-12 11:47:03 -0800 | [diff] [blame] | 88 | CreateScreenStateChangedEvent(android::view::DisplayStateEnum::DISPLAY_STATE_ON, |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 89 | bucketStartTimeNs + bucketSizeNs + 500); |
| 90 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 91 | auto acquireEvent1 = CreateAcquireWakelockEvent(attributions1, "wl1", bucketStartTimeNs + 2); |
| 92 | auto releaseEvent1 = |
| 93 | CreateReleaseWakelockEvent(attributions1, "wl1", bucketStartTimeNs + bucketSizeNs + 2); |
| 94 | auto acquireEvent2 = |
| 95 | CreateAcquireWakelockEvent(attributions2, "wl2", bucketStartTimeNs + bucketSizeNs - 10); |
| 96 | auto releaseEvent2 = CreateReleaseWakelockEvent(attributions2, "wl2", |
| 97 | bucketStartTimeNs + 2 * bucketSizeNs - 15); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 98 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 99 | std::vector<std::unique_ptr<LogEvent>> events; |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 100 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 101 | events.push_back(std::move(screenTurnedOnEvent)); |
| 102 | events.push_back(std::move(screenTurnedOffEvent)); |
| 103 | events.push_back(std::move(screenTurnedOnEvent2)); |
| 104 | events.push_back(std::move(acquireEvent1)); |
| 105 | events.push_back(std::move(acquireEvent2)); |
| 106 | events.push_back(std::move(releaseEvent1)); |
| 107 | events.push_back(std::move(releaseEvent2)); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 108 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 109 | sortLogEventsByTimestamp(&events); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 110 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 111 | for (const auto& event : events) { |
| 112 | processor->OnLogEvent(event.get()); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 116 | } // namespace |
| 117 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 118 | TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForSumDuration1) { |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 119 | ConfigKey cfgKey; |
| 120 | auto config = CreateStatsdConfig(DurationMetric::SUM); |
| 121 | uint64_t bucketStartTimeNs = 10000000000; |
| 122 | uint64_t bucketSizeNs = |
| 123 | TimeUnitToBucketSizeInMillis(config.duration_metric(0).bucket()) * 1000000LL; |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 124 | auto processor = CreateStatsLogProcessor(bucketStartTimeNs, bucketStartTimeNs, config, cfgKey); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 125 | EXPECT_EQ(processor->mMetricsManagers.size(), 1u); |
| 126 | EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid()); |
| 127 | FeedEvents(config, processor); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 128 | vector<uint8_t> buffer; |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 129 | ConfigMetricsReportList reports; |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 130 | processor->onDumpReport(cfgKey, bucketStartTimeNs + 2 * bucketSizeNs - 1, false, true, |
| 131 | ADB_DUMP, &buffer); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 132 | EXPECT_TRUE(buffer.size() > 0); |
| 133 | EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size())); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 134 | backfillDimensionPath(&reports); |
| 135 | backfillStringInReport(&reports); |
| 136 | backfillStartEndTimestamp(&reports); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 137 | |
| 138 | EXPECT_EQ(reports.reports_size(), 1); |
| 139 | EXPECT_EQ(reports.reports(0).metrics_size(), 1); |
| 140 | // Only 1 dimension output. The tag dimension in the predicate has been aggregated. |
| 141 | EXPECT_EQ(reports.reports(0).metrics(0).duration_metrics().data_size(), 1); |
| 142 | |
| 143 | auto data = reports.reports(0).metrics(0).duration_metrics().data(0); |
| 144 | // Validate dimension value. |
| 145 | ValidateAttributionUidDimension(data.dimensions_in_what(), |
| 146 | android::util::WAKELOCK_STATE_CHANGED, 111); |
| 147 | // Validate bucket info. |
| 148 | EXPECT_EQ(reports.reports(0).metrics(0).duration_metrics().data(0).bucket_info_size(), 1); |
| 149 | data = reports.reports(0).metrics(0).duration_metrics().data(0); |
| 150 | // The wakelock holding interval starts from the screen off event and to the end of the 1st |
| 151 | // bucket. |
| 152 | EXPECT_EQ((unsigned long long)data.bucket_info(0).duration_nanos(), bucketSizeNs - 200); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 153 | } |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 154 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 155 | TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForSumDuration2) { |
| 156 | ConfigKey cfgKey; |
| 157 | auto config = CreateStatsdConfig(DurationMetric::SUM); |
| 158 | uint64_t bucketStartTimeNs = 10000000000; |
| 159 | uint64_t bucketSizeNs = |
| 160 | TimeUnitToBucketSizeInMillis(config.duration_metric(0).bucket()) * 1000000LL; |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 161 | auto processor = CreateStatsLogProcessor(bucketStartTimeNs, bucketStartTimeNs, config, cfgKey); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 162 | EXPECT_EQ(processor->mMetricsManagers.size(), 1u); |
| 163 | EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid()); |
| 164 | FeedEvents(config, processor); |
| 165 | vector<uint8_t> buffer; |
| 166 | ConfigMetricsReportList reports; |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 167 | processor->onDumpReport(cfgKey, bucketStartTimeNs + 2 * bucketSizeNs + 1, false, true, |
| 168 | ADB_DUMP, &buffer); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 169 | EXPECT_TRUE(buffer.size() > 0); |
| 170 | EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size())); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 171 | backfillDimensionPath(&reports); |
| 172 | backfillStringInReport(&reports); |
| 173 | backfillStartEndTimestamp(&reports); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 174 | EXPECT_EQ(reports.reports_size(), 1); |
| 175 | EXPECT_EQ(reports.reports(0).metrics_size(), 1); |
| 176 | EXPECT_EQ(reports.reports(0).metrics(0).duration_metrics().data_size(), 1); |
| 177 | // Dump the report after the end of 2nd bucket. |
| 178 | EXPECT_EQ(reports.reports(0).metrics(0).duration_metrics().data(0).bucket_info_size(), 2); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 179 | auto data = reports.reports(0).metrics(0).duration_metrics().data(0); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 180 | // Validate dimension value. |
| 181 | ValidateAttributionUidDimension(data.dimensions_in_what(), |
| 182 | android::util::WAKELOCK_STATE_CHANGED, 111); |
| 183 | // Two output buckets. |
| 184 | // The wakelock holding interval in the 1st bucket starts from the screen off event and to |
| 185 | // the end of the 1st bucket. |
| 186 | EXPECT_EQ((unsigned long long)data.bucket_info(0).duration_nanos(), |
| 187 | bucketStartTimeNs + bucketSizeNs - (bucketStartTimeNs + 200)); |
| 188 | // The wakelock holding interval in the 2nd bucket starts at the beginning of the bucket and |
| 189 | // ends at the second screen on event. |
| 190 | EXPECT_EQ((unsigned long long)data.bucket_info(1).duration_nanos(), 500UL); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 191 | } |
| 192 | TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForSumDuration3) { |
| 193 | ConfigKey cfgKey; |
| 194 | auto config = CreateStatsdConfig(DurationMetric::SUM); |
| 195 | uint64_t bucketStartTimeNs = 10000000000; |
| 196 | uint64_t bucketSizeNs = |
| 197 | TimeUnitToBucketSizeInMillis(config.duration_metric(0).bucket()) * 1000000LL; |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 198 | auto processor = CreateStatsLogProcessor(bucketStartTimeNs, bucketStartTimeNs, config, cfgKey); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 199 | EXPECT_EQ(processor->mMetricsManagers.size(), 1u); |
| 200 | EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid()); |
| 201 | FeedEvents(config, processor); |
| 202 | vector<uint8_t> buffer; |
| 203 | ConfigMetricsReportList reports; |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 204 | |
| 205 | std::vector<std::unique_ptr<LogEvent>> events; |
| 206 | events.push_back( |
| 207 | CreateScreenStateChangedEvent(android::view::DisplayStateEnum::DISPLAY_STATE_OFF, |
| 208 | bucketStartTimeNs + 2 * bucketSizeNs + 90)); |
| 209 | events.push_back(CreateAcquireWakelockEvent(attributions1, "wl3", |
| 210 | bucketStartTimeNs + 2 * bucketSizeNs + 100)); |
| 211 | events.push_back(CreateReleaseWakelockEvent(attributions1, "wl3", |
| 212 | bucketStartTimeNs + 5 * bucketSizeNs + 100)); |
| 213 | sortLogEventsByTimestamp(&events); |
| 214 | for (const auto& event : events) { |
| 215 | processor->OnLogEvent(event.get()); |
| 216 | } |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 217 | |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 218 | processor->onDumpReport(cfgKey, bucketStartTimeNs + 6 * bucketSizeNs + 1, false, true, |
| 219 | ADB_DUMP, &buffer); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 220 | EXPECT_TRUE(buffer.size() > 0); |
| 221 | EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size())); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 222 | backfillDimensionPath(&reports); |
| 223 | backfillStringInReport(&reports); |
| 224 | backfillStartEndTimestamp(&reports); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 225 | EXPECT_EQ(reports.reports_size(), 1); |
| 226 | EXPECT_EQ(reports.reports(0).metrics_size(), 1); |
| 227 | EXPECT_EQ(reports.reports(0).metrics(0).duration_metrics().data_size(), 1); |
| 228 | EXPECT_EQ(reports.reports(0).metrics(0).duration_metrics().data(0).bucket_info_size(), 6); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 229 | auto data = reports.reports(0).metrics(0).duration_metrics().data(0); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 230 | ValidateAttributionUidDimension(data.dimensions_in_what(), |
| 231 | android::util::WAKELOCK_STATE_CHANGED, 111); |
| 232 | // The last wakelock holding spans 4 buckets. |
| 233 | EXPECT_EQ((unsigned long long)data.bucket_info(2).duration_nanos(), bucketSizeNs - 100); |
| 234 | EXPECT_EQ((unsigned long long)data.bucket_info(3).duration_nanos(), bucketSizeNs); |
| 235 | EXPECT_EQ((unsigned long long)data.bucket_info(4).duration_nanos(), bucketSizeNs); |
| 236 | EXPECT_EQ((unsigned long long)data.bucket_info(5).duration_nanos(), 100UL); |
| 237 | } |
| 238 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 239 | TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForMaxDuration1) { |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 240 | ConfigKey cfgKey; |
| 241 | auto config = CreateStatsdConfig(DurationMetric::MAX_SPARSE); |
| 242 | uint64_t bucketStartTimeNs = 10000000000; |
| 243 | uint64_t bucketSizeNs = |
| 244 | TimeUnitToBucketSizeInMillis(config.duration_metric(0).bucket()) * 1000000LL; |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 245 | auto processor = CreateStatsLogProcessor(bucketStartTimeNs, bucketStartTimeNs, config, cfgKey); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 246 | EXPECT_EQ(processor->mMetricsManagers.size(), 1u); |
| 247 | EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid()); |
| 248 | FeedEvents(config, processor); |
| 249 | ConfigMetricsReportList reports; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 250 | vector<uint8_t> buffer; |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 251 | processor->onDumpReport(cfgKey, bucketStartTimeNs + 2 * bucketSizeNs - 1, false, true, |
| 252 | ADB_DUMP, &buffer); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 253 | EXPECT_TRUE(buffer.size() > 0); |
| 254 | |
| 255 | EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size())); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 256 | backfillDimensionPath(&reports); |
| 257 | backfillStringInReport(&reports); |
| 258 | backfillStartEndTimestamp(&reports); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 259 | |
| 260 | EXPECT_EQ(reports.reports_size(), 1); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 261 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 262 | // When using ProtoOutputStream, if nothing written to a sub msg, it won't be treated as |
| 263 | // one. It was previsouly 1 because we had a fake onDumpReport which calls add_metric() by |
| 264 | // itself. |
Yang Lu | b472291 | 2018-11-15 11:02:03 -0800 | [diff] [blame] | 265 | EXPECT_EQ(1, reports.reports(0).metrics_size()); |
| 266 | EXPECT_EQ(0, reports.reports(0).metrics(0).duration_metrics().data_size()); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForMaxDuration2) { |
| 270 | ConfigKey cfgKey; |
| 271 | auto config = CreateStatsdConfig(DurationMetric::MAX_SPARSE); |
| 272 | uint64_t bucketStartTimeNs = 10000000000; |
| 273 | uint64_t bucketSizeNs = |
| 274 | TimeUnitToBucketSizeInMillis(config.duration_metric(0).bucket()) * 1000000LL; |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 275 | auto processor = CreateStatsLogProcessor(bucketStartTimeNs, bucketStartTimeNs, config, cfgKey); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 276 | EXPECT_EQ(processor->mMetricsManagers.size(), 1u); |
| 277 | EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid()); |
| 278 | FeedEvents(config, processor); |
| 279 | ConfigMetricsReportList reports; |
| 280 | vector<uint8_t> buffer; |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 281 | processor->onDumpReport(cfgKey, bucketStartTimeNs + 2 * bucketSizeNs + 1, false, true, |
| 282 | ADB_DUMP, &buffer); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 283 | EXPECT_TRUE(buffer.size() > 0); |
| 284 | EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size())); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 285 | backfillDimensionPath(&reports); |
| 286 | backfillStringInReport(&reports); |
| 287 | backfillStartEndTimestamp(&reports); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 288 | EXPECT_EQ(reports.reports_size(), 1); |
| 289 | EXPECT_EQ(reports.reports(0).metrics_size(), 1); |
| 290 | EXPECT_EQ(reports.reports(0).metrics(0).duration_metrics().data_size(), 1); |
| 291 | // Dump the report after the end of 2nd bucket. One dimension with one bucket. |
| 292 | EXPECT_EQ(reports.reports(0).metrics(0).duration_metrics().data(0).bucket_info_size(), 1); |
| 293 | auto data = reports.reports(0).metrics(0).duration_metrics().data(0); |
| 294 | // Validate dimension value. |
| 295 | ValidateAttributionUidDimension(data.dimensions_in_what(), |
| 296 | android::util::WAKELOCK_STATE_CHANGED, 111); |
| 297 | // The max is acquire event for wl1 to screen off start. |
| 298 | EXPECT_EQ((unsigned long long)data.bucket_info(0).duration_nanos(), bucketSizeNs + 2 - 200); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForMaxDuration3) { |
| 302 | ConfigKey cfgKey; |
| 303 | auto config = CreateStatsdConfig(DurationMetric::MAX_SPARSE); |
| 304 | uint64_t bucketStartTimeNs = 10000000000; |
| 305 | uint64_t bucketSizeNs = |
| 306 | TimeUnitToBucketSizeInMillis(config.duration_metric(0).bucket()) * 1000000LL; |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 307 | auto processor = CreateStatsLogProcessor(bucketStartTimeNs, bucketStartTimeNs, config, cfgKey); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 308 | EXPECT_EQ(processor->mMetricsManagers.size(), 1u); |
| 309 | EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid()); |
| 310 | FeedEvents(config, processor); |
| 311 | ConfigMetricsReportList reports; |
| 312 | vector<uint8_t> buffer; |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 313 | |
| 314 | std::vector<std::unique_ptr<LogEvent>> events; |
| 315 | events.push_back( |
| 316 | CreateScreenStateChangedEvent(android::view::DisplayStateEnum::DISPLAY_STATE_OFF, |
| 317 | bucketStartTimeNs + 2 * bucketSizeNs + 90)); |
| 318 | events.push_back(CreateAcquireWakelockEvent(attributions1, "wl3", |
| 319 | bucketStartTimeNs + 2 * bucketSizeNs + 100)); |
| 320 | events.push_back(CreateReleaseWakelockEvent(attributions1, "wl3", |
| 321 | bucketStartTimeNs + 5 * bucketSizeNs + 100)); |
| 322 | sortLogEventsByTimestamp(&events); |
| 323 | for (const auto& event : events) { |
| 324 | processor->OnLogEvent(event.get()); |
| 325 | } |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 326 | |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 327 | processor->onDumpReport(cfgKey, bucketStartTimeNs + 6 * bucketSizeNs + 1, false, true, |
| 328 | ADB_DUMP, &buffer); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 329 | EXPECT_TRUE(buffer.size() > 0); |
| 330 | EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size())); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 331 | backfillDimensionPath(&reports); |
| 332 | backfillStringInReport(&reports); |
| 333 | backfillStartEndTimestamp(&reports); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 334 | EXPECT_EQ(reports.reports_size(), 1); |
| 335 | EXPECT_EQ(reports.reports(0).metrics_size(), 1); |
| 336 | EXPECT_EQ(reports.reports(0).metrics(0).duration_metrics().data_size(), 1); |
| 337 | EXPECT_EQ(reports.reports(0).metrics(0).duration_metrics().data(0).bucket_info_size(), 2); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 338 | auto data = reports.reports(0).metrics(0).duration_metrics().data(0); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 339 | ValidateAttributionUidDimension(data.dimensions_in_what(), |
| 340 | android::util::WAKELOCK_STATE_CHANGED, 111); |
| 341 | // The last wakelock holding spans 4 buckets. |
| 342 | EXPECT_EQ((unsigned long long)data.bucket_info(1).duration_nanos(), 3 * bucketSizeNs); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 343 | EXPECT_EQ((unsigned long long)data.bucket_info(1).start_bucket_elapsed_nanos(), |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 344 | bucketStartTimeNs + 5 * bucketSizeNs); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 345 | EXPECT_EQ((unsigned long long)data.bucket_info(1).end_bucket_elapsed_nanos(), |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 346 | bucketStartTimeNs + 6 * bucketSizeNs); |
| 347 | } |
| 348 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 349 | #else |
| 350 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 351 | #endif |
| 352 | |
| 353 | } // namespace statsd |
| 354 | } // namespace os |
yro | 59cc24d | 2018-02-13 20:17:32 -0800 | [diff] [blame] | 355 | } // namespace android |