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