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; |
| 124 | auto processor = CreateStatsLogProcessor(bucketStartTimeNs / NS_PER_SEC, config, cfgKey); |
| 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; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 130 | processor->onDumpReport(cfgKey, bucketStartTimeNs + 2 * bucketSizeNs - 1, &buffer); |
| 131 | EXPECT_TRUE(buffer.size() > 0); |
| 132 | EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size())); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 133 | |
| 134 | EXPECT_EQ(reports.reports_size(), 1); |
| 135 | EXPECT_EQ(reports.reports(0).metrics_size(), 1); |
| 136 | // Only 1 dimension output. The tag dimension in the predicate has been aggregated. |
| 137 | EXPECT_EQ(reports.reports(0).metrics(0).duration_metrics().data_size(), 1); |
| 138 | |
| 139 | auto data = reports.reports(0).metrics(0).duration_metrics().data(0); |
| 140 | // Validate dimension value. |
| 141 | ValidateAttributionUidDimension(data.dimensions_in_what(), |
| 142 | android::util::WAKELOCK_STATE_CHANGED, 111); |
| 143 | // Validate bucket info. |
| 144 | EXPECT_EQ(reports.reports(0).metrics(0).duration_metrics().data(0).bucket_info_size(), 1); |
| 145 | data = reports.reports(0).metrics(0).duration_metrics().data(0); |
| 146 | // The wakelock holding interval starts from the screen off event and to the end of the 1st |
| 147 | // bucket. |
| 148 | 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] | 149 | } |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 150 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 151 | TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForSumDuration2) { |
| 152 | ConfigKey cfgKey; |
| 153 | auto config = CreateStatsdConfig(DurationMetric::SUM); |
| 154 | uint64_t bucketStartTimeNs = 10000000000; |
| 155 | uint64_t bucketSizeNs = |
| 156 | TimeUnitToBucketSizeInMillis(config.duration_metric(0).bucket()) * 1000000LL; |
| 157 | auto processor = CreateStatsLogProcessor(bucketStartTimeNs / NS_PER_SEC, config, cfgKey); |
| 158 | EXPECT_EQ(processor->mMetricsManagers.size(), 1u); |
| 159 | EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid()); |
| 160 | FeedEvents(config, processor); |
| 161 | vector<uint8_t> buffer; |
| 162 | ConfigMetricsReportList reports; |
| 163 | |
| 164 | processor->onDumpReport(cfgKey, bucketStartTimeNs + 2 * bucketSizeNs + 1, &buffer); |
| 165 | EXPECT_TRUE(buffer.size() > 0); |
| 166 | EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size())); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 167 | EXPECT_EQ(reports.reports_size(), 1); |
| 168 | EXPECT_EQ(reports.reports(0).metrics_size(), 1); |
| 169 | EXPECT_EQ(reports.reports(0).metrics(0).duration_metrics().data_size(), 1); |
| 170 | // Dump the report after the end of 2nd bucket. |
| 171 | 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] | 172 | auto data = reports.reports(0).metrics(0).duration_metrics().data(0); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 173 | // Validate dimension value. |
| 174 | ValidateAttributionUidDimension(data.dimensions_in_what(), |
| 175 | android::util::WAKELOCK_STATE_CHANGED, 111); |
| 176 | // Two output buckets. |
| 177 | // The wakelock holding interval in the 1st bucket starts from the screen off event and to |
| 178 | // the end of the 1st bucket. |
| 179 | EXPECT_EQ((unsigned long long)data.bucket_info(0).duration_nanos(), |
| 180 | bucketStartTimeNs + bucketSizeNs - (bucketStartTimeNs + 200)); |
| 181 | // The wakelock holding interval in the 2nd bucket starts at the beginning of the bucket and |
| 182 | // ends at the second screen on event. |
| 183 | EXPECT_EQ((unsigned long long)data.bucket_info(1).duration_nanos(), 500UL); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 184 | } |
| 185 | TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForSumDuration3) { |
| 186 | ConfigKey cfgKey; |
| 187 | auto config = CreateStatsdConfig(DurationMetric::SUM); |
| 188 | uint64_t bucketStartTimeNs = 10000000000; |
| 189 | uint64_t bucketSizeNs = |
| 190 | TimeUnitToBucketSizeInMillis(config.duration_metric(0).bucket()) * 1000000LL; |
| 191 | auto processor = CreateStatsLogProcessor(bucketStartTimeNs / NS_PER_SEC, config, cfgKey); |
| 192 | EXPECT_EQ(processor->mMetricsManagers.size(), 1u); |
| 193 | EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid()); |
| 194 | FeedEvents(config, processor); |
| 195 | vector<uint8_t> buffer; |
| 196 | ConfigMetricsReportList reports; |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 197 | |
| 198 | std::vector<std::unique_ptr<LogEvent>> events; |
| 199 | events.push_back( |
| 200 | CreateScreenStateChangedEvent(android::view::DisplayStateEnum::DISPLAY_STATE_OFF, |
| 201 | bucketStartTimeNs + 2 * bucketSizeNs + 90)); |
| 202 | events.push_back(CreateAcquireWakelockEvent(attributions1, "wl3", |
| 203 | bucketStartTimeNs + 2 * bucketSizeNs + 100)); |
| 204 | events.push_back(CreateReleaseWakelockEvent(attributions1, "wl3", |
| 205 | bucketStartTimeNs + 5 * bucketSizeNs + 100)); |
| 206 | sortLogEventsByTimestamp(&events); |
| 207 | for (const auto& event : events) { |
| 208 | processor->OnLogEvent(event.get()); |
| 209 | } |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 210 | |
| 211 | processor->onDumpReport(cfgKey, bucketStartTimeNs + 6 * bucketSizeNs + 1, &buffer); |
| 212 | EXPECT_TRUE(buffer.size() > 0); |
| 213 | EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size())); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 214 | EXPECT_EQ(reports.reports_size(), 1); |
| 215 | EXPECT_EQ(reports.reports(0).metrics_size(), 1); |
| 216 | EXPECT_EQ(reports.reports(0).metrics(0).duration_metrics().data_size(), 1); |
| 217 | 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] | 218 | auto data = reports.reports(0).metrics(0).duration_metrics().data(0); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 219 | ValidateAttributionUidDimension(data.dimensions_in_what(), |
| 220 | android::util::WAKELOCK_STATE_CHANGED, 111); |
| 221 | // The last wakelock holding spans 4 buckets. |
| 222 | EXPECT_EQ((unsigned long long)data.bucket_info(2).duration_nanos(), bucketSizeNs - 100); |
| 223 | EXPECT_EQ((unsigned long long)data.bucket_info(3).duration_nanos(), bucketSizeNs); |
| 224 | EXPECT_EQ((unsigned long long)data.bucket_info(4).duration_nanos(), bucketSizeNs); |
| 225 | EXPECT_EQ((unsigned long long)data.bucket_info(5).duration_nanos(), 100UL); |
| 226 | } |
| 227 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 228 | TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForMaxDuration1) { |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 229 | ConfigKey cfgKey; |
| 230 | auto config = CreateStatsdConfig(DurationMetric::MAX_SPARSE); |
| 231 | uint64_t bucketStartTimeNs = 10000000000; |
| 232 | uint64_t bucketSizeNs = |
| 233 | TimeUnitToBucketSizeInMillis(config.duration_metric(0).bucket()) * 1000000LL; |
| 234 | auto processor = CreateStatsLogProcessor(bucketStartTimeNs / NS_PER_SEC, config, cfgKey); |
| 235 | EXPECT_EQ(processor->mMetricsManagers.size(), 1u); |
| 236 | EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid()); |
| 237 | FeedEvents(config, processor); |
| 238 | ConfigMetricsReportList reports; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 239 | vector<uint8_t> buffer; |
| 240 | processor->onDumpReport(cfgKey, bucketStartTimeNs + 2 * bucketSizeNs - 1, &buffer); |
| 241 | EXPECT_TRUE(buffer.size() > 0); |
| 242 | |
| 243 | EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size())); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 244 | |
| 245 | EXPECT_EQ(reports.reports_size(), 1); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 246 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 247 | // When using ProtoOutputStream, if nothing written to a sub msg, it won't be treated as |
| 248 | // one. It was previsouly 1 because we had a fake onDumpReport which calls add_metric() by |
| 249 | // itself. |
| 250 | EXPECT_EQ(0, reports.reports(0).metrics_size()); |
| 251 | } |
| 252 | |
| 253 | TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForMaxDuration2) { |
| 254 | ConfigKey cfgKey; |
| 255 | auto config = CreateStatsdConfig(DurationMetric::MAX_SPARSE); |
| 256 | uint64_t bucketStartTimeNs = 10000000000; |
| 257 | uint64_t bucketSizeNs = |
| 258 | TimeUnitToBucketSizeInMillis(config.duration_metric(0).bucket()) * 1000000LL; |
| 259 | auto processor = CreateStatsLogProcessor(bucketStartTimeNs / NS_PER_SEC, config, cfgKey); |
| 260 | EXPECT_EQ(processor->mMetricsManagers.size(), 1u); |
| 261 | EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid()); |
| 262 | FeedEvents(config, processor); |
| 263 | ConfigMetricsReportList reports; |
| 264 | vector<uint8_t> buffer; |
| 265 | processor->onDumpReport(cfgKey, bucketStartTimeNs + 2 * bucketSizeNs + 1, &buffer); |
| 266 | EXPECT_TRUE(buffer.size() > 0); |
| 267 | EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size())); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 268 | EXPECT_EQ(reports.reports_size(), 1); |
| 269 | EXPECT_EQ(reports.reports(0).metrics_size(), 1); |
| 270 | EXPECT_EQ(reports.reports(0).metrics(0).duration_metrics().data_size(), 1); |
| 271 | // Dump the report after the end of 2nd bucket. One dimension with one bucket. |
| 272 | EXPECT_EQ(reports.reports(0).metrics(0).duration_metrics().data(0).bucket_info_size(), 1); |
| 273 | auto data = reports.reports(0).metrics(0).duration_metrics().data(0); |
| 274 | // Validate dimension value. |
| 275 | ValidateAttributionUidDimension(data.dimensions_in_what(), |
| 276 | android::util::WAKELOCK_STATE_CHANGED, 111); |
| 277 | // The max is acquire event for wl1 to screen off start. |
| 278 | 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] | 279 | } |
| 280 | |
| 281 | TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForMaxDuration3) { |
| 282 | ConfigKey cfgKey; |
| 283 | auto config = CreateStatsdConfig(DurationMetric::MAX_SPARSE); |
| 284 | uint64_t bucketStartTimeNs = 10000000000; |
| 285 | uint64_t bucketSizeNs = |
| 286 | TimeUnitToBucketSizeInMillis(config.duration_metric(0).bucket()) * 1000000LL; |
| 287 | auto processor = CreateStatsLogProcessor(bucketStartTimeNs / NS_PER_SEC, config, cfgKey); |
| 288 | EXPECT_EQ(processor->mMetricsManagers.size(), 1u); |
| 289 | EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid()); |
| 290 | FeedEvents(config, processor); |
| 291 | ConfigMetricsReportList reports; |
| 292 | vector<uint8_t> buffer; |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 293 | |
| 294 | std::vector<std::unique_ptr<LogEvent>> events; |
| 295 | events.push_back( |
| 296 | CreateScreenStateChangedEvent(android::view::DisplayStateEnum::DISPLAY_STATE_OFF, |
| 297 | bucketStartTimeNs + 2 * bucketSizeNs + 90)); |
| 298 | events.push_back(CreateAcquireWakelockEvent(attributions1, "wl3", |
| 299 | bucketStartTimeNs + 2 * bucketSizeNs + 100)); |
| 300 | events.push_back(CreateReleaseWakelockEvent(attributions1, "wl3", |
| 301 | bucketStartTimeNs + 5 * bucketSizeNs + 100)); |
| 302 | sortLogEventsByTimestamp(&events); |
| 303 | for (const auto& event : events) { |
| 304 | processor->OnLogEvent(event.get()); |
| 305 | } |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 306 | |
| 307 | processor->onDumpReport(cfgKey, bucketStartTimeNs + 6 * bucketSizeNs + 1, &buffer); |
| 308 | EXPECT_TRUE(buffer.size() > 0); |
| 309 | EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size())); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 310 | EXPECT_EQ(reports.reports_size(), 1); |
| 311 | EXPECT_EQ(reports.reports(0).metrics_size(), 1); |
| 312 | EXPECT_EQ(reports.reports(0).metrics(0).duration_metrics().data_size(), 1); |
| 313 | 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] | 314 | auto data = reports.reports(0).metrics(0).duration_metrics().data(0); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 315 | ValidateAttributionUidDimension(data.dimensions_in_what(), |
| 316 | android::util::WAKELOCK_STATE_CHANGED, 111); |
| 317 | // The last wakelock holding spans 4 buckets. |
| 318 | 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] | 319 | 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] | 320 | bucketStartTimeNs + 5 * bucketSizeNs); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 321 | 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] | 322 | bucketStartTimeNs + 6 * bucketSizeNs); |
| 323 | } |
| 324 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 325 | #else |
| 326 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 327 | #endif |
| 328 | |
| 329 | } // namespace statsd |
| 330 | } // namespace os |
yro | 59cc24d | 2018-02-13 20:17:32 -0800 | [diff] [blame] | 331 | } // namespace android |