Yangster-mac | 9369446 | 2018-01-22 20:49:31 -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" |
| 18 | #include "src/stats_log_util.h" |
| 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 | |
| 29 | namespace { |
| 30 | |
| 31 | StatsdConfig CreateCountMetricWithNoLinkConfig() { |
| 32 | StatsdConfig config; |
| 33 | auto screenBrightnessChangeAtomMatcher = CreateScreenBrightnessChangedAtomMatcher(); |
| 34 | *config.add_atom_matcher() = screenBrightnessChangeAtomMatcher; |
| 35 | *config.add_atom_matcher() = CreateScreenTurnedOnAtomMatcher(); |
| 36 | *config.add_atom_matcher() = CreateScreenTurnedOffAtomMatcher(); |
| 37 | *config.add_atom_matcher() = CreateAcquireWakelockAtomMatcher(); |
| 38 | *config.add_atom_matcher() = CreateReleaseWakelockAtomMatcher(); |
| 39 | |
| 40 | auto screenIsOffPredicate = CreateScreenIsOffPredicate(); |
| 41 | *config.add_predicate() = screenIsOffPredicate; |
| 42 | |
| 43 | auto holdingWakelockPredicate = CreateHoldingWakelockPredicate(); |
| 44 | // The predicate is dimensioning by any attribution node and both by uid and tag. |
| 45 | *holdingWakelockPredicate.mutable_simple_predicate()->mutable_dimensions() = |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 46 | CreateAttributionUidAndTagDimensions(android::util::WAKELOCK_STATE_CHANGED, |
| 47 | {Position::FIRST}); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 48 | *config.add_predicate() = holdingWakelockPredicate; |
| 49 | |
| 50 | auto combinationPredicate = config.add_predicate(); |
| 51 | combinationPredicate->set_id(987654); |
| 52 | combinationPredicate->mutable_combination()->set_operation(LogicalOperation::OR); |
| 53 | addPredicateToPredicateCombination(screenIsOffPredicate, combinationPredicate); |
| 54 | addPredicateToPredicateCombination(holdingWakelockPredicate, combinationPredicate); |
| 55 | |
| 56 | auto metric = config.add_count_metric(); |
| 57 | metric->set_id(StringToId("ScreenBrightnessChangeMetric")); |
| 58 | metric->set_what(screenBrightnessChangeAtomMatcher.id()); |
| 59 | metric->set_condition(combinationPredicate->id()); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 60 | *metric->mutable_dimensions_in_what() = |
| 61 | CreateDimensions(android::util::SCREEN_BRIGHTNESS_CHANGED, {1 /* level */}); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 62 | *metric->mutable_dimensions_in_condition() = CreateAttributionUidDimensions( |
| 63 | android::util::WAKELOCK_STATE_CHANGED, {Position::FIRST}); |
yro | 59cc24d | 2018-02-13 20:17:32 -0800 | [diff] [blame] | 64 | metric->set_bucket(FIVE_MINUTES); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 65 | return config; |
| 66 | } |
| 67 | |
| 68 | } // namespace |
| 69 | |
| 70 | TEST(DimensionInConditionE2eTest, TestCountMetricNoLink) { |
| 71 | ConfigKey cfgKey; |
| 72 | auto config = CreateCountMetricWithNoLinkConfig(); |
| 73 | int64_t bucketStartTimeNs = 10000000000; |
| 74 | int64_t bucketSizeNs = |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 75 | TimeUnitToBucketSizeInMillis(config.count_metric(0).bucket()) * 1000000LL; |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 76 | |
| 77 | auto processor = CreateStatsLogProcessor(bucketStartTimeNs / NS_PER_SEC, config, cfgKey); |
| 78 | EXPECT_EQ(processor->mMetricsManagers.size(), 1u); |
| 79 | EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid()); |
| 80 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 81 | std::vector<AttributionNode> attributions1 = {CreateAttribution(111, "App1"), |
| 82 | CreateAttribution(222, "GMSCoreModule1"), |
| 83 | CreateAttribution(222, "GMSCoreModule2")}; |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 84 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 85 | std::vector<AttributionNode> attributions2 = {CreateAttribution(333, "App2"), |
| 86 | CreateAttribution(222, "GMSCoreModule1"), |
| 87 | CreateAttribution(555, "GMSCoreModule2")}; |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 88 | |
| 89 | std::vector<std::unique_ptr<LogEvent>> events; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 90 | events.push_back( |
| 91 | CreateScreenStateChangedEvent(android::view::DISPLAY_STATE_ON, bucketStartTimeNs + 10)); |
| 92 | events.push_back(CreateScreenStateChangedEvent(android::view::DISPLAY_STATE_OFF, |
| 93 | bucketStartTimeNs + 100)); |
| 94 | events.push_back(CreateScreenStateChangedEvent(android::view::DISPLAY_STATE_ON, |
| 95 | bucketStartTimeNs + bucketSizeNs + 1)); |
| 96 | events.push_back(CreateScreenStateChangedEvent(android::view::DISPLAY_STATE_OFF, |
| 97 | bucketStartTimeNs + 2 * bucketSizeNs - 10)); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 98 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 99 | events.push_back(CreateAcquireWakelockEvent(attributions1, "wl1", bucketStartTimeNs + 200)); |
| 100 | events.push_back( |
| 101 | CreateReleaseWakelockEvent(attributions1, "wl1", bucketStartTimeNs + bucketSizeNs + 1)); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 102 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 103 | events.push_back(CreateAcquireWakelockEvent(attributions2, "wl2", |
| 104 | bucketStartTimeNs + bucketSizeNs - 100)); |
| 105 | events.push_back(CreateReleaseWakelockEvent(attributions2, "wl2", |
| 106 | bucketStartTimeNs + 2 * bucketSizeNs - 50)); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 107 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 108 | events.push_back(CreateScreenBrightnessChangedEvent(123, bucketStartTimeNs + 11)); |
| 109 | events.push_back(CreateScreenBrightnessChangedEvent(123, bucketStartTimeNs + 101)); |
| 110 | events.push_back(CreateScreenBrightnessChangedEvent(123, bucketStartTimeNs + 201)); |
| 111 | events.push_back(CreateScreenBrightnessChangedEvent(456, bucketStartTimeNs + 203)); |
| 112 | events.push_back( |
| 113 | CreateScreenBrightnessChangedEvent(456, bucketStartTimeNs + bucketSizeNs - 99)); |
| 114 | events.push_back(CreateScreenBrightnessChangedEvent(456, bucketStartTimeNs + bucketSizeNs - 2)); |
| 115 | events.push_back(CreateScreenBrightnessChangedEvent(789, bucketStartTimeNs + bucketSizeNs - 1)); |
| 116 | events.push_back(CreateScreenBrightnessChangedEvent(456, bucketStartTimeNs + bucketSizeNs + 2)); |
| 117 | events.push_back( |
| 118 | CreateScreenBrightnessChangedEvent(789, bucketStartTimeNs + 2 * bucketSizeNs - 11)); |
| 119 | events.push_back( |
| 120 | CreateScreenBrightnessChangedEvent(789, bucketStartTimeNs + 2 * bucketSizeNs - 9)); |
| 121 | events.push_back( |
| 122 | CreateScreenBrightnessChangedEvent(789, bucketStartTimeNs + 2 * bucketSizeNs - 1)); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 123 | |
| 124 | sortLogEventsByTimestamp(&events); |
| 125 | |
| 126 | for (const auto& event : events) { |
| 127 | processor->OnLogEvent(event.get()); |
| 128 | } |
| 129 | |
| 130 | ConfigMetricsReportList reports; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 131 | vector<uint8_t> buffer; |
| 132 | processor->onDumpReport(cfgKey, bucketStartTimeNs + 2 * bucketSizeNs + 1, &buffer); |
| 133 | EXPECT_TRUE(buffer.size() > 0); |
| 134 | EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size())); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 135 | |
| 136 | EXPECT_EQ(reports.reports_size(), 1); |
| 137 | EXPECT_EQ(reports.reports(0).metrics_size(), 1); |
| 138 | StatsLogReport::CountMetricDataWrapper countMetrics; |
| 139 | sortMetricDataByDimensionsValue(reports.reports(0).metrics(0).count_metrics(), &countMetrics); |
| 140 | |
| 141 | EXPECT_EQ(countMetrics.data_size(), 7); |
| 142 | auto data = countMetrics.data(0); |
| 143 | EXPECT_EQ(data.bucket_info_size(), 1); |
| 144 | EXPECT_EQ(data.bucket_info(0).count(), 1); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 145 | EXPECT_EQ(data.bucket_info(0).start_bucket_elapsed_nanos(), bucketStartTimeNs); |
| 146 | EXPECT_EQ(data.bucket_info(0).end_bucket_elapsed_nanos(), bucketStartTimeNs + bucketSizeNs); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 147 | EXPECT_EQ(data.dimensions_in_what().field(), android::util::SCREEN_BRIGHTNESS_CHANGED); |
| 148 | EXPECT_EQ(data.dimensions_in_what().value_tuple().dimensions_value_size(), 1); |
| 149 | EXPECT_EQ(data.dimensions_in_what().value_tuple().dimensions_value(0).field(), 1); |
| 150 | EXPECT_EQ(data.dimensions_in_what().value_tuple().dimensions_value(0).value_int(), 123); |
| 151 | EXPECT_FALSE(data.dimensions_in_condition().has_field()); |
| 152 | |
| 153 | data = countMetrics.data(1); |
| 154 | EXPECT_EQ(data.bucket_info_size(), 1); |
| 155 | EXPECT_EQ(data.bucket_info(0).count(), 1); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 156 | EXPECT_EQ(data.bucket_info(0).start_bucket_elapsed_nanos(), bucketStartTimeNs); |
| 157 | EXPECT_EQ(data.bucket_info(0).end_bucket_elapsed_nanos(), bucketStartTimeNs + bucketSizeNs); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 158 | EXPECT_EQ(data.dimensions_in_what().field(), android::util::SCREEN_BRIGHTNESS_CHANGED); |
| 159 | EXPECT_EQ(data.dimensions_in_what().value_tuple().dimensions_value_size(), 1); |
| 160 | EXPECT_EQ(data.dimensions_in_what().value_tuple().dimensions_value(0).field(), 1); |
| 161 | EXPECT_EQ(data.dimensions_in_what().value_tuple().dimensions_value(0).value_int(), 123); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 162 | ValidateAttributionUidDimension(data.dimensions_in_condition(), |
| 163 | android::util::WAKELOCK_STATE_CHANGED, 111); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 164 | |
| 165 | data = countMetrics.data(2); |
| 166 | EXPECT_EQ(data.bucket_info_size(), 1); |
| 167 | EXPECT_EQ(data.bucket_info(0).count(), 3); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 168 | EXPECT_EQ(data.bucket_info(0).start_bucket_elapsed_nanos(), bucketStartTimeNs); |
| 169 | EXPECT_EQ(data.bucket_info(0).end_bucket_elapsed_nanos(), bucketStartTimeNs + bucketSizeNs); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 170 | EXPECT_EQ(data.dimensions_in_what().field(), android::util::SCREEN_BRIGHTNESS_CHANGED); |
| 171 | EXPECT_EQ(data.dimensions_in_what().value_tuple().dimensions_value_size(), 1); |
| 172 | EXPECT_EQ(data.dimensions_in_what().value_tuple().dimensions_value(0).field(), 1); |
| 173 | EXPECT_EQ(data.dimensions_in_what().value_tuple().dimensions_value(0).value_int(), 456); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 174 | ValidateAttributionUidDimension(data.dimensions_in_condition(), |
| 175 | android::util::WAKELOCK_STATE_CHANGED, 111); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 176 | |
| 177 | data = countMetrics.data(3); |
| 178 | EXPECT_EQ(data.bucket_info_size(), 2); |
| 179 | EXPECT_EQ(data.bucket_info(0).count(), 2); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 180 | EXPECT_EQ(data.bucket_info(0).start_bucket_elapsed_nanos(), bucketStartTimeNs); |
| 181 | EXPECT_EQ(data.bucket_info(0).end_bucket_elapsed_nanos(), bucketStartTimeNs + bucketSizeNs); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 182 | EXPECT_EQ(data.bucket_info(1).count(), 1); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 183 | EXPECT_EQ(data.bucket_info(1).start_bucket_elapsed_nanos(), bucketStartTimeNs + bucketSizeNs); |
| 184 | EXPECT_EQ(data.bucket_info(1).end_bucket_elapsed_nanos(), bucketStartTimeNs + 2 * bucketSizeNs); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 185 | EXPECT_EQ(data.dimensions_in_what().field(), android::util::SCREEN_BRIGHTNESS_CHANGED); |
| 186 | EXPECT_EQ(data.dimensions_in_what().value_tuple().dimensions_value_size(), 1); |
| 187 | EXPECT_EQ(data.dimensions_in_what().value_tuple().dimensions_value(0).field(), 1); |
| 188 | EXPECT_EQ(data.dimensions_in_what().value_tuple().dimensions_value(0).value_int(), 456); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 189 | ValidateAttributionUidDimension(data.dimensions_in_condition(), |
| 190 | android::util::WAKELOCK_STATE_CHANGED, 333); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 191 | |
| 192 | data = countMetrics.data(4); |
| 193 | EXPECT_EQ(data.bucket_info_size(), 1); |
| 194 | EXPECT_EQ(data.bucket_info(0).count(), 2); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 195 | EXPECT_EQ(data.bucket_info(0).start_bucket_elapsed_nanos(), bucketStartTimeNs + bucketSizeNs); |
| 196 | EXPECT_EQ(data.bucket_info(0).end_bucket_elapsed_nanos(), bucketStartTimeNs + 2 * bucketSizeNs); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 197 | EXPECT_EQ(data.dimensions_in_what().field(), android::util::SCREEN_BRIGHTNESS_CHANGED); |
| 198 | EXPECT_EQ(data.dimensions_in_what().value_tuple().dimensions_value_size(), 1); |
| 199 | EXPECT_EQ(data.dimensions_in_what().value_tuple().dimensions_value(0).field(), 1); |
| 200 | EXPECT_EQ(data.dimensions_in_what().value_tuple().dimensions_value(0).value_int(), 789); |
| 201 | EXPECT_FALSE(data.dimensions_in_condition().has_field()); |
| 202 | |
| 203 | data = countMetrics.data(5); |
| 204 | EXPECT_EQ(data.bucket_info_size(), 1); |
| 205 | EXPECT_EQ(data.bucket_info(0).count(), 1); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 206 | EXPECT_EQ(data.bucket_info(0).start_bucket_elapsed_nanos(), bucketStartTimeNs); |
| 207 | EXPECT_EQ(data.bucket_info(0).end_bucket_elapsed_nanos(), bucketStartTimeNs + bucketSizeNs); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 208 | EXPECT_EQ(data.dimensions_in_what().field(), android::util::SCREEN_BRIGHTNESS_CHANGED); |
| 209 | EXPECT_EQ(data.dimensions_in_what().value_tuple().dimensions_value_size(), 1); |
| 210 | EXPECT_EQ(data.dimensions_in_what().value_tuple().dimensions_value(0).field(), 1); |
| 211 | EXPECT_EQ(data.dimensions_in_what().value_tuple().dimensions_value(0).value_int(), 789); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 212 | ValidateAttributionUidDimension(data.dimensions_in_condition(), |
| 213 | android::util::WAKELOCK_STATE_CHANGED, 111); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 214 | |
| 215 | data = countMetrics.data(6); |
| 216 | EXPECT_EQ(data.bucket_info_size(), 1); |
| 217 | EXPECT_EQ(data.bucket_info(0).count(), 1); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 218 | EXPECT_EQ(data.bucket_info(0).start_bucket_elapsed_nanos(), bucketStartTimeNs); |
| 219 | EXPECT_EQ(data.bucket_info(0).end_bucket_elapsed_nanos(), bucketStartTimeNs + bucketSizeNs); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 220 | EXPECT_EQ(data.dimensions_in_what().field(), android::util::SCREEN_BRIGHTNESS_CHANGED); |
| 221 | EXPECT_EQ(data.dimensions_in_what().value_tuple().dimensions_value_size(), 1); |
| 222 | EXPECT_EQ(data.dimensions_in_what().value_tuple().dimensions_value(0).field(), 1); |
| 223 | EXPECT_EQ(data.dimensions_in_what().value_tuple().dimensions_value(0).value_int(), 789); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 224 | ValidateAttributionUidDimension(data.dimensions_in_condition(), |
| 225 | android::util::WAKELOCK_STATE_CHANGED, 333); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | namespace { |
| 229 | |
| 230 | StatsdConfig CreateCountMetricWithLinkConfig() { |
| 231 | StatsdConfig config; |
| 232 | auto appCrashMatcher = CreateProcessCrashAtomMatcher(); |
| 233 | *config.add_atom_matcher() = appCrashMatcher; |
| 234 | *config.add_atom_matcher() = CreateScreenTurnedOnAtomMatcher(); |
| 235 | *config.add_atom_matcher() = CreateScreenTurnedOffAtomMatcher(); |
| 236 | *config.add_atom_matcher() = CreateSyncStartAtomMatcher(); |
| 237 | *config.add_atom_matcher() = CreateSyncEndAtomMatcher(); |
| 238 | |
| 239 | auto screenIsOffPredicate = CreateScreenIsOffPredicate(); |
| 240 | auto isSyncingPredicate = CreateIsSyncingPredicate(); |
| 241 | auto syncDimension = isSyncingPredicate.mutable_simple_predicate()->mutable_dimensions(); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 242 | *syncDimension = CreateAttributionUidAndTagDimensions(android::util::SYNC_STATE_CHANGED, |
| 243 | {Position::FIRST}); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 244 | syncDimension->add_child()->set_field(2 /* name field*/); |
| 245 | |
| 246 | *config.add_predicate() = screenIsOffPredicate; |
| 247 | *config.add_predicate() = isSyncingPredicate; |
| 248 | auto combinationPredicate = config.add_predicate(); |
| 249 | combinationPredicate->set_id(987654); |
| 250 | combinationPredicate->mutable_combination()->set_operation(LogicalOperation::OR); |
| 251 | addPredicateToPredicateCombination(screenIsOffPredicate, combinationPredicate); |
| 252 | addPredicateToPredicateCombination(isSyncingPredicate, combinationPredicate); |
| 253 | |
| 254 | auto metric = config.add_count_metric(); |
yro | 59cc24d | 2018-02-13 20:17:32 -0800 | [diff] [blame] | 255 | metric->set_bucket(FIVE_MINUTES); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 256 | metric->set_id(StringToId("AppCrashMetric")); |
| 257 | metric->set_what(appCrashMatcher.id()); |
| 258 | metric->set_condition(combinationPredicate->id()); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 259 | *metric->mutable_dimensions_in_what() = |
| 260 | CreateDimensions(android::util::PROCESS_LIFE_CYCLE_STATE_CHANGED, {1 /* uid */}); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 261 | *metric->mutable_dimensions_in_condition() = CreateAttributionUidAndTagDimensions( |
| 262 | android::util::SYNC_STATE_CHANGED, {Position::FIRST}); |
| 263 | |
| 264 | // Links between crash atom and condition of app is in syncing. |
| 265 | auto links = metric->add_links(); |
| 266 | links->set_condition(isSyncingPredicate.id()); |
| 267 | auto dimensionWhat = links->mutable_fields_in_what(); |
| 268 | dimensionWhat->set_field(android::util::PROCESS_LIFE_CYCLE_STATE_CHANGED); |
| 269 | dimensionWhat->add_child()->set_field(1); // uid field. |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 270 | *links->mutable_fields_in_condition() = |
| 271 | CreateAttributionUidDimensions(android::util::SYNC_STATE_CHANGED, {Position::FIRST}); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 272 | return config; |
| 273 | } |
| 274 | |
| 275 | } // namespace |
| 276 | |
| 277 | TEST(DimensionInConditionE2eTest, TestCountMetricWithLink) { |
| 278 | ConfigKey cfgKey; |
| 279 | auto config = CreateCountMetricWithLinkConfig(); |
| 280 | int64_t bucketStartTimeNs = 10000000000; |
| 281 | int64_t bucketSizeNs = |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 282 | TimeUnitToBucketSizeInMillis(config.count_metric(0).bucket()) * 1000000LL; |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 283 | |
| 284 | auto processor = CreateStatsLogProcessor(bucketStartTimeNs / NS_PER_SEC, config, cfgKey); |
| 285 | EXPECT_EQ(processor->mMetricsManagers.size(), 1u); |
| 286 | EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid()); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 287 | std::vector<AttributionNode> attributions1 = {CreateAttribution(111, "App1"), |
| 288 | CreateAttribution(222, "GMSCoreModule1"), |
| 289 | CreateAttribution(222, "GMSCoreModule2")}; |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 290 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 291 | std::vector<AttributionNode> attributions2 = {CreateAttribution(333, "App2"), |
| 292 | CreateAttribution(222, "GMSCoreModule1"), |
| 293 | CreateAttribution(555, "GMSCoreModule2")}; |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 294 | |
| 295 | std::vector<std::unique_ptr<LogEvent>> events; |
| 296 | |
| 297 | events.push_back(CreateAppCrashEvent(111, bucketStartTimeNs + 11)); |
| 298 | events.push_back(CreateAppCrashEvent(111, bucketStartTimeNs + 101)); |
| 299 | events.push_back(CreateAppCrashEvent(222, bucketStartTimeNs + 101)); |
| 300 | |
| 301 | events.push_back(CreateAppCrashEvent(222, bucketStartTimeNs + 201)); |
| 302 | events.push_back(CreateAppCrashEvent(111, bucketStartTimeNs + 211)); |
| 303 | events.push_back(CreateAppCrashEvent(333, bucketStartTimeNs + 211)); |
| 304 | |
| 305 | events.push_back(CreateAppCrashEvent(111, bucketStartTimeNs + 401)); |
| 306 | events.push_back(CreateAppCrashEvent(333, bucketStartTimeNs + 401)); |
| 307 | events.push_back(CreateAppCrashEvent(555, bucketStartTimeNs + 401)); |
| 308 | |
| 309 | events.push_back(CreateAppCrashEvent(111, bucketStartTimeNs + bucketSizeNs + 301)); |
| 310 | events.push_back(CreateAppCrashEvent(333, bucketStartTimeNs + bucketSizeNs + 301)); |
| 311 | |
| 312 | events.push_back(CreateAppCrashEvent(777, bucketStartTimeNs + bucketSizeNs + 701)); |
| 313 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 314 | events.push_back( |
| 315 | CreateScreenStateChangedEvent(android::view::DISPLAY_STATE_ON, bucketStartTimeNs + 10)); |
| 316 | events.push_back(CreateScreenStateChangedEvent(android::view::DISPLAY_STATE_OFF, |
| 317 | bucketStartTimeNs + 100)); |
| 318 | events.push_back(CreateScreenStateChangedEvent(android::view::DISPLAY_STATE_ON, |
| 319 | bucketStartTimeNs + 202)); |
| 320 | events.push_back(CreateScreenStateChangedEvent(android::view::DISPLAY_STATE_OFF, |
| 321 | bucketStartTimeNs + bucketSizeNs + 700)); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 322 | |
| 323 | events.push_back(CreateSyncStartEvent(attributions1, "ReadEmail", bucketStartTimeNs + 200)); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 324 | events.push_back( |
| 325 | CreateSyncEndEvent(attributions1, "ReadEmail", bucketStartTimeNs + bucketSizeNs + 300)); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 326 | |
| 327 | events.push_back(CreateSyncStartEvent(attributions1, "ReadDoc", bucketStartTimeNs + 400)); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 328 | events.push_back( |
| 329 | CreateSyncEndEvent(attributions1, "ReadDoc", bucketStartTimeNs + bucketSizeNs - 1)); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 330 | |
| 331 | events.push_back(CreateSyncStartEvent(attributions2, "ReadEmail", bucketStartTimeNs + 400)); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 332 | events.push_back( |
| 333 | CreateSyncEndEvent(attributions2, "ReadEmail", bucketStartTimeNs + bucketSizeNs + 600)); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 334 | |
| 335 | sortLogEventsByTimestamp(&events); |
| 336 | |
| 337 | for (const auto& event : events) { |
| 338 | processor->OnLogEvent(event.get()); |
| 339 | } |
| 340 | |
| 341 | ConfigMetricsReportList reports; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 342 | vector<uint8_t> buffer; |
| 343 | processor->onDumpReport(cfgKey, bucketStartTimeNs + 2 * bucketSizeNs + 1, &buffer); |
| 344 | EXPECT_TRUE(buffer.size() > 0); |
| 345 | EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size())); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 346 | |
| 347 | EXPECT_EQ(reports.reports_size(), 1); |
| 348 | EXPECT_EQ(reports.reports(0).metrics_size(), 1); |
| 349 | StatsLogReport::CountMetricDataWrapper countMetrics; |
| 350 | sortMetricDataByDimensionsValue(reports.reports(0).metrics(0).count_metrics(), &countMetrics); |
| 351 | |
| 352 | EXPECT_EQ(countMetrics.data_size(), 5); |
| 353 | auto data = countMetrics.data(0); |
| 354 | EXPECT_EQ(data.dimensions_in_what().field(), android::util::PROCESS_LIFE_CYCLE_STATE_CHANGED); |
| 355 | EXPECT_EQ(data.dimensions_in_what().value_tuple().dimensions_value_size(), 1); |
| 356 | EXPECT_EQ(data.dimensions_in_what().value_tuple().dimensions_value(0).field(), 1); |
| 357 | EXPECT_EQ(data.dimensions_in_what().value_tuple().dimensions_value(0).value_int(), 111); |
| 358 | EXPECT_FALSE(data.dimensions_in_condition().has_field()); |
| 359 | EXPECT_EQ(data.bucket_info_size(), 1); |
| 360 | EXPECT_EQ(data.bucket_info(0).count(), 1); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 361 | EXPECT_EQ(data.bucket_info(0).start_bucket_elapsed_nanos(), bucketStartTimeNs); |
| 362 | EXPECT_EQ(data.bucket_info(0).end_bucket_elapsed_nanos(), bucketStartTimeNs + bucketSizeNs); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 363 | |
| 364 | data = countMetrics.data(1); |
| 365 | EXPECT_EQ(data.dimensions_in_what().field(), android::util::PROCESS_LIFE_CYCLE_STATE_CHANGED); |
| 366 | EXPECT_EQ(data.dimensions_in_what().value_tuple().dimensions_value_size(), 1); |
| 367 | EXPECT_EQ(data.dimensions_in_what().value_tuple().dimensions_value(0).field(), 1); |
| 368 | EXPECT_EQ(data.dimensions_in_what().value_tuple().dimensions_value(0).value_int(), 111); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 369 | ValidateAttributionUidAndTagDimension(data.dimensions_in_condition(), |
| 370 | android::util::SYNC_STATE_CHANGED, 111, "App1"); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 371 | EXPECT_EQ(data.bucket_info_size(), 1); |
| 372 | EXPECT_EQ(data.bucket_info(0).count(), 2); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 373 | EXPECT_EQ(data.bucket_info(0).start_bucket_elapsed_nanos(), bucketStartTimeNs); |
| 374 | EXPECT_EQ(data.bucket_info(0).end_bucket_elapsed_nanos(), bucketStartTimeNs + bucketSizeNs); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 375 | |
| 376 | data = countMetrics.data(2); |
| 377 | EXPECT_EQ(data.dimensions_in_what().field(), android::util::PROCESS_LIFE_CYCLE_STATE_CHANGED); |
| 378 | EXPECT_EQ(data.dimensions_in_what().value_tuple().dimensions_value_size(), 1); |
| 379 | EXPECT_EQ(data.dimensions_in_what().value_tuple().dimensions_value(0).field(), 1); |
| 380 | EXPECT_EQ(data.dimensions_in_what().value_tuple().dimensions_value(0).value_int(), 222); |
| 381 | EXPECT_FALSE(data.dimensions_in_condition().has_field()); |
| 382 | EXPECT_EQ(data.bucket_info_size(), 1); |
| 383 | EXPECT_EQ(data.bucket_info(0).count(), 2); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 384 | EXPECT_EQ(data.bucket_info(0).start_bucket_elapsed_nanos(), bucketStartTimeNs); |
| 385 | EXPECT_EQ(data.bucket_info(0).end_bucket_elapsed_nanos(), bucketStartTimeNs + bucketSizeNs); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 386 | |
| 387 | data = countMetrics.data(3); |
| 388 | EXPECT_EQ(data.dimensions_in_what().field(), android::util::PROCESS_LIFE_CYCLE_STATE_CHANGED); |
| 389 | EXPECT_EQ(data.dimensions_in_what().value_tuple().dimensions_value_size(), 1); |
| 390 | EXPECT_EQ(data.dimensions_in_what().value_tuple().dimensions_value(0).field(), 1); |
| 391 | EXPECT_EQ(data.dimensions_in_what().value_tuple().dimensions_value(0).value_int(), 333); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 392 | ValidateAttributionUidAndTagDimension(data.dimensions_in_condition(), |
| 393 | android::util::SYNC_STATE_CHANGED, 333, "App2"); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 394 | EXPECT_EQ(data.bucket_info_size(), 2); |
| 395 | EXPECT_EQ(data.bucket_info(0).count(), 1); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 396 | EXPECT_EQ(data.bucket_info(0).start_bucket_elapsed_nanos(), bucketStartTimeNs); |
| 397 | EXPECT_EQ(data.bucket_info(0).end_bucket_elapsed_nanos(), bucketStartTimeNs + bucketSizeNs); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 398 | EXPECT_EQ(data.bucket_info(1).count(), 1); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 399 | EXPECT_EQ(data.bucket_info(1).start_bucket_elapsed_nanos(), bucketStartTimeNs + bucketSizeNs); |
| 400 | EXPECT_EQ(data.bucket_info(1).end_bucket_elapsed_nanos(), bucketStartTimeNs + 2 * bucketSizeNs); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 401 | |
| 402 | data = countMetrics.data(4); |
| 403 | EXPECT_EQ(data.dimensions_in_what().field(), android::util::PROCESS_LIFE_CYCLE_STATE_CHANGED); |
| 404 | EXPECT_EQ(data.dimensions_in_what().value_tuple().dimensions_value_size(), 1); |
| 405 | EXPECT_EQ(data.dimensions_in_what().value_tuple().dimensions_value(0).field(), 1); |
| 406 | EXPECT_EQ(data.dimensions_in_what().value_tuple().dimensions_value(0).value_int(), 777); |
| 407 | EXPECT_FALSE(data.dimensions_in_condition().has_field()); |
| 408 | EXPECT_EQ(data.bucket_info_size(), 1); |
| 409 | EXPECT_EQ(data.bucket_info(0).count(), 1); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 410 | EXPECT_EQ(data.bucket_info(0).start_bucket_elapsed_nanos(), bucketStartTimeNs + bucketSizeNs); |
| 411 | EXPECT_EQ(data.bucket_info(0).end_bucket_elapsed_nanos(), bucketStartTimeNs + 2 * bucketSizeNs); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | namespace { |
| 415 | |
| 416 | StatsdConfig CreateDurationMetricConfigNoLink(DurationMetric::AggregationType aggregationType) { |
| 417 | StatsdConfig config; |
| 418 | *config.add_atom_matcher() = CreateBatterySaverModeStartAtomMatcher(); |
| 419 | *config.add_atom_matcher() = CreateBatterySaverModeStopAtomMatcher(); |
| 420 | *config.add_atom_matcher() = CreateScreenTurnedOnAtomMatcher(); |
| 421 | *config.add_atom_matcher() = CreateScreenTurnedOffAtomMatcher(); |
| 422 | *config.add_atom_matcher() = CreateSyncStartAtomMatcher(); |
| 423 | *config.add_atom_matcher() = CreateSyncEndAtomMatcher(); |
| 424 | |
| 425 | auto inBatterySaverModePredicate = CreateBatterySaverModePredicate(); |
| 426 | |
| 427 | auto screenIsOffPredicate = CreateScreenIsOffPredicate(); |
| 428 | auto isSyncingPredicate = CreateIsSyncingPredicate(); |
| 429 | auto syncDimension = isSyncingPredicate.mutable_simple_predicate()->mutable_dimensions(); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 430 | *syncDimension = CreateAttributionUidAndTagDimensions(android::util::SYNC_STATE_CHANGED, |
| 431 | {Position::FIRST}); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 432 | syncDimension->add_child()->set_field(2 /* name field */); |
| 433 | |
| 434 | *config.add_predicate() = inBatterySaverModePredicate; |
| 435 | *config.add_predicate() = screenIsOffPredicate; |
| 436 | *config.add_predicate() = isSyncingPredicate; |
| 437 | auto combinationPredicate = config.add_predicate(); |
| 438 | combinationPredicate->set_id(987654); |
| 439 | combinationPredicate->mutable_combination()->set_operation(LogicalOperation::OR); |
| 440 | addPredicateToPredicateCombination(screenIsOffPredicate, combinationPredicate); |
| 441 | addPredicateToPredicateCombination(isSyncingPredicate, combinationPredicate); |
| 442 | |
| 443 | auto metric = config.add_duration_metric(); |
yro | 59cc24d | 2018-02-13 20:17:32 -0800 | [diff] [blame] | 444 | metric->set_bucket(FIVE_MINUTES); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 445 | metric->set_id(StringToId("BatterySaverModeDurationMetric")); |
| 446 | metric->set_what(inBatterySaverModePredicate.id()); |
| 447 | metric->set_condition(combinationPredicate->id()); |
| 448 | *metric->mutable_dimensions_in_condition() = CreateAttributionUidAndTagDimensions( |
| 449 | android::util::SYNC_STATE_CHANGED, {Position::FIRST}); |
| 450 | return config; |
| 451 | } |
| 452 | |
| 453 | } // namespace |
| 454 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 455 | TEST(DimensionInConditionE2eTest, TestDurationMetricNoLink) { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 456 | for (auto aggregationType : {DurationMetric::SUM, DurationMetric::MAX_SPARSE}) { |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 457 | ConfigKey cfgKey; |
| 458 | auto config = CreateDurationMetricConfigNoLink(aggregationType); |
| 459 | int64_t bucketStartTimeNs = 10000000000; |
| 460 | int64_t bucketSizeNs = |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 461 | TimeUnitToBucketSizeInMillis(config.duration_metric(0).bucket()) * 1000000LL; |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 462 | |
| 463 | auto processor = CreateStatsLogProcessor(bucketStartTimeNs / NS_PER_SEC, config, cfgKey); |
| 464 | EXPECT_EQ(processor->mMetricsManagers.size(), 1u); |
| 465 | EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid()); |
| 466 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 467 | std::vector<AttributionNode> attributions1 = {CreateAttribution(111, "App1"), |
| 468 | CreateAttribution(222, "GMSCoreModule1"), |
| 469 | CreateAttribution(222, "GMSCoreModule2")}; |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 470 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 471 | std::vector<AttributionNode> attributions2 = {CreateAttribution(333, "App2"), |
| 472 | CreateAttribution(222, "GMSCoreModule1"), |
| 473 | CreateAttribution(555, "GMSCoreModule2")}; |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 474 | |
| 475 | std::vector<std::unique_ptr<LogEvent>> events; |
| 476 | |
| 477 | events.push_back(CreateBatterySaverOffEvent(bucketStartTimeNs + 1)); |
| 478 | events.push_back(CreateBatterySaverOnEvent(bucketStartTimeNs + 101)); |
| 479 | events.push_back(CreateBatterySaverOffEvent(bucketStartTimeNs + 110)); |
| 480 | |
| 481 | events.push_back(CreateBatterySaverOnEvent(bucketStartTimeNs + 201)); |
| 482 | events.push_back(CreateBatterySaverOffEvent(bucketStartTimeNs + 500)); |
| 483 | |
| 484 | events.push_back(CreateBatterySaverOnEvent(bucketStartTimeNs + 600)); |
| 485 | events.push_back(CreateBatterySaverOffEvent(bucketStartTimeNs + bucketSizeNs + 850)); |
| 486 | |
| 487 | events.push_back(CreateBatterySaverOnEvent(bucketStartTimeNs + bucketSizeNs + 870)); |
| 488 | events.push_back(CreateBatterySaverOffEvent(bucketStartTimeNs + bucketSizeNs + 900)); |
| 489 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 490 | events.push_back(CreateScreenStateChangedEvent(android::view::DISPLAY_STATE_ON, |
| 491 | bucketStartTimeNs + 10)); |
| 492 | events.push_back(CreateScreenStateChangedEvent(android::view::DISPLAY_STATE_OFF, |
| 493 | bucketStartTimeNs + 100)); |
| 494 | events.push_back(CreateScreenStateChangedEvent(android::view::DISPLAY_STATE_ON, |
| 495 | bucketStartTimeNs + 202)); |
| 496 | events.push_back(CreateScreenStateChangedEvent(android::view::DISPLAY_STATE_OFF, |
| 497 | bucketStartTimeNs + bucketSizeNs + 800)); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 498 | |
| 499 | events.push_back(CreateSyncStartEvent(attributions1, "ReadEmail", bucketStartTimeNs + 200)); |
| 500 | events.push_back(CreateSyncEndEvent(attributions1, "ReadEmail", |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 501 | bucketStartTimeNs + bucketSizeNs + 300)); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 502 | |
| 503 | events.push_back(CreateSyncStartEvent(attributions1, "ReadDoc", bucketStartTimeNs + 400)); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 504 | events.push_back( |
| 505 | CreateSyncEndEvent(attributions1, "ReadDoc", bucketStartTimeNs + bucketSizeNs - 1)); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 506 | |
| 507 | events.push_back(CreateSyncStartEvent(attributions2, "ReadEmail", bucketStartTimeNs + 401)); |
| 508 | events.push_back(CreateSyncEndEvent(attributions2, "ReadEmail", |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 509 | bucketStartTimeNs + bucketSizeNs + 700)); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 510 | |
| 511 | sortLogEventsByTimestamp(&events); |
| 512 | |
| 513 | for (const auto& event : events) { |
| 514 | processor->OnLogEvent(event.get()); |
| 515 | } |
| 516 | |
| 517 | ConfigMetricsReportList reports; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 518 | vector<uint8_t> buffer; |
| 519 | processor->onDumpReport(cfgKey, bucketStartTimeNs + 2 * bucketSizeNs + 1, &buffer); |
| 520 | EXPECT_TRUE(buffer.size() > 0); |
| 521 | EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size())); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 522 | |
| 523 | EXPECT_EQ(reports.reports_size(), 1); |
| 524 | EXPECT_EQ(reports.reports(0).metrics_size(), 1); |
| 525 | StatsLogReport::DurationMetricDataWrapper metrics; |
| 526 | sortMetricDataByDimensionsValue(reports.reports(0).metrics(0).duration_metrics(), &metrics); |
| 527 | |
| 528 | EXPECT_EQ(metrics.data_size(), 3); |
| 529 | auto data = metrics.data(0); |
| 530 | EXPECT_FALSE(data.dimensions_in_what().has_field()); |
| 531 | EXPECT_FALSE(data.dimensions_in_condition().has_field()); |
| 532 | EXPECT_EQ(data.bucket_info_size(), 2); |
| 533 | EXPECT_EQ(data.bucket_info(0).duration_nanos(), 9); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 534 | EXPECT_EQ(data.bucket_info(0).start_bucket_elapsed_nanos(), bucketStartTimeNs); |
| 535 | EXPECT_EQ(data.bucket_info(0).end_bucket_elapsed_nanos(), bucketStartTimeNs + bucketSizeNs); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 536 | EXPECT_EQ(data.bucket_info(1).duration_nanos(), 30); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 537 | EXPECT_EQ(data.bucket_info(1).start_bucket_elapsed_nanos(), bucketStartTimeNs + bucketSizeNs); |
| 538 | EXPECT_EQ(data.bucket_info(1).end_bucket_elapsed_nanos(), bucketStartTimeNs + 2 * bucketSizeNs); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 539 | |
| 540 | data = metrics.data(1); |
| 541 | EXPECT_FALSE(data.dimensions_in_what().has_field()); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 542 | ValidateAttributionUidAndTagDimension(data.dimensions_in_condition(), |
| 543 | android::util::SYNC_STATE_CHANGED, 111, "App1"); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 544 | EXPECT_EQ(data.bucket_info_size(), 2); |
| 545 | EXPECT_EQ(data.bucket_info(0).duration_nanos(), 500 - 201 + bucketSizeNs - 600); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 546 | EXPECT_EQ(data.bucket_info(0).start_bucket_elapsed_nanos(), bucketStartTimeNs); |
| 547 | EXPECT_EQ(data.bucket_info(0).end_bucket_elapsed_nanos(), bucketStartTimeNs + bucketSizeNs); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 548 | EXPECT_EQ(data.bucket_info(1).duration_nanos(), 300); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 549 | EXPECT_EQ(data.bucket_info(1).start_bucket_elapsed_nanos(), bucketStartTimeNs + bucketSizeNs); |
| 550 | EXPECT_EQ(data.bucket_info(1).end_bucket_elapsed_nanos(), bucketStartTimeNs + 2 * bucketSizeNs); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 551 | |
| 552 | data = metrics.data(2); |
| 553 | EXPECT_FALSE(data.dimensions_in_what().has_field()); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 554 | ValidateAttributionUidAndTagDimension(data.dimensions_in_condition(), |
| 555 | android::util::SYNC_STATE_CHANGED, 333, "App2"); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 556 | EXPECT_EQ(data.bucket_info_size(), 2); |
| 557 | EXPECT_EQ(data.bucket_info(0).duration_nanos(), 500 - 401 + bucketSizeNs - 600); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 558 | EXPECT_EQ(data.bucket_info(0).start_bucket_elapsed_nanos(), bucketStartTimeNs); |
| 559 | EXPECT_EQ(data.bucket_info(0).end_bucket_elapsed_nanos(), bucketStartTimeNs + bucketSizeNs); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 560 | EXPECT_EQ(data.bucket_info(1).duration_nanos(), 700); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 561 | EXPECT_EQ(data.bucket_info(1).start_bucket_elapsed_nanos(), bucketStartTimeNs + bucketSizeNs); |
| 562 | EXPECT_EQ(data.bucket_info(1).end_bucket_elapsed_nanos(), bucketStartTimeNs + 2 * bucketSizeNs); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 563 | } |
| 564 | } |
| 565 | |
| 566 | namespace { |
| 567 | |
| 568 | StatsdConfig CreateDurationMetricConfigWithLink(DurationMetric::AggregationType aggregationType) { |
| 569 | StatsdConfig config; |
| 570 | *config.add_atom_matcher() = CreateMoveToBackgroundAtomMatcher(); |
| 571 | *config.add_atom_matcher() = CreateMoveToForegroundAtomMatcher(); |
| 572 | *config.add_atom_matcher() = CreateScreenTurnedOnAtomMatcher(); |
| 573 | *config.add_atom_matcher() = CreateScreenTurnedOffAtomMatcher(); |
| 574 | *config.add_atom_matcher() = CreateSyncStartAtomMatcher(); |
| 575 | *config.add_atom_matcher() = CreateSyncEndAtomMatcher(); |
| 576 | |
| 577 | auto screenIsOffPredicate = CreateScreenIsOffPredicate(); |
| 578 | auto isSyncingPredicate = CreateIsSyncingPredicate(); |
| 579 | auto syncDimension = isSyncingPredicate.mutable_simple_predicate()->mutable_dimensions(); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 580 | *syncDimension = CreateAttributionUidAndTagDimensions(android::util::SYNC_STATE_CHANGED, |
| 581 | {Position::FIRST}); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 582 | syncDimension->add_child()->set_field(2 /* name field */); |
| 583 | |
| 584 | auto isInBackgroundPredicate = CreateIsInBackgroundPredicate(); |
| 585 | *isInBackgroundPredicate.mutable_simple_predicate()->mutable_dimensions() = |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 586 | CreateDimensions(android::util::ACTIVITY_FOREGROUND_STATE_CHANGED, {1 /* uid field */}); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 587 | |
| 588 | *config.add_predicate() = screenIsOffPredicate; |
| 589 | *config.add_predicate() = isSyncingPredicate; |
| 590 | *config.add_predicate() = isInBackgroundPredicate; |
| 591 | auto combinationPredicate = config.add_predicate(); |
| 592 | combinationPredicate->set_id(987654); |
| 593 | combinationPredicate->mutable_combination()->set_operation(LogicalOperation::OR); |
| 594 | addPredicateToPredicateCombination(screenIsOffPredicate, combinationPredicate); |
| 595 | addPredicateToPredicateCombination(isSyncingPredicate, combinationPredicate); |
| 596 | |
| 597 | auto metric = config.add_duration_metric(); |
yro | 59cc24d | 2018-02-13 20:17:32 -0800 | [diff] [blame] | 598 | metric->set_bucket(FIVE_MINUTES); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 599 | metric->set_id(StringToId("AppInBackgroundMetric")); |
| 600 | metric->set_what(isInBackgroundPredicate.id()); |
| 601 | metric->set_condition(combinationPredicate->id()); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 602 | *metric->mutable_dimensions_in_what() = |
| 603 | CreateDimensions(android::util::ACTIVITY_FOREGROUND_STATE_CHANGED, {1 /* uid field */}); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 604 | *metric->mutable_dimensions_in_condition() = CreateAttributionUidAndTagDimensions( |
| 605 | android::util::SYNC_STATE_CHANGED, {Position::FIRST}); |
| 606 | |
| 607 | // Links between crash atom and condition of app is in syncing. |
| 608 | auto links = metric->add_links(); |
| 609 | links->set_condition(isSyncingPredicate.id()); |
| 610 | auto dimensionWhat = links->mutable_fields_in_what(); |
| 611 | dimensionWhat->set_field(android::util::ACTIVITY_FOREGROUND_STATE_CHANGED); |
| 612 | dimensionWhat->add_child()->set_field(1); // uid field. |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 613 | *links->mutable_fields_in_condition() = |
| 614 | CreateAttributionUidDimensions(android::util::SYNC_STATE_CHANGED, {Position::FIRST}); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 615 | return config; |
| 616 | } |
| 617 | |
| 618 | } // namespace |
| 619 | |
| 620 | TEST(DimensionInConditionE2eTest, TestDurationMetricWithLink) { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 621 | for (auto aggregationType : {DurationMetric::SUM, DurationMetric::MAX_SPARSE}) { |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 622 | ConfigKey cfgKey; |
| 623 | auto config = CreateDurationMetricConfigWithLink(aggregationType); |
| 624 | int64_t bucketStartTimeNs = 10000000000; |
| 625 | int64_t bucketSizeNs = |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 626 | TimeUnitToBucketSizeInMillis(config.duration_metric(0).bucket()) * 1000000LL; |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 627 | |
| 628 | auto processor = CreateStatsLogProcessor(bucketStartTimeNs / NS_PER_SEC, config, cfgKey); |
| 629 | EXPECT_EQ(processor->mMetricsManagers.size(), 1u); |
| 630 | EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid()); |
| 631 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 632 | std::vector<AttributionNode> attributions1 = {CreateAttribution(111, "App1"), |
| 633 | CreateAttribution(222, "GMSCoreModule1"), |
| 634 | CreateAttribution(222, "GMSCoreModule2")}; |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 635 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 636 | std::vector<AttributionNode> attributions2 = {CreateAttribution(333, "App2"), |
| 637 | CreateAttribution(222, "GMSCoreModule1"), |
| 638 | CreateAttribution(555, "GMSCoreModule2")}; |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 639 | |
| 640 | std::vector<std::unique_ptr<LogEvent>> events; |
| 641 | |
| 642 | events.push_back(CreateMoveToBackgroundEvent(111, bucketStartTimeNs + 101)); |
| 643 | events.push_back(CreateMoveToForegroundEvent(111, bucketStartTimeNs + 110)); |
| 644 | |
| 645 | events.push_back(CreateMoveToBackgroundEvent(111, bucketStartTimeNs + 201)); |
| 646 | events.push_back(CreateMoveToForegroundEvent(111, bucketStartTimeNs + bucketSizeNs + 100)); |
| 647 | |
| 648 | events.push_back(CreateMoveToBackgroundEvent(333, bucketStartTimeNs + 399)); |
| 649 | events.push_back(CreateMoveToForegroundEvent(333, bucketStartTimeNs + bucketSizeNs + 800)); |
| 650 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 651 | events.push_back(CreateScreenStateChangedEvent(android::view::DISPLAY_STATE_ON, |
| 652 | bucketStartTimeNs + 10)); |
| 653 | events.push_back(CreateScreenStateChangedEvent(android::view::DISPLAY_STATE_OFF, |
| 654 | bucketStartTimeNs + 100)); |
| 655 | events.push_back(CreateScreenStateChangedEvent(android::view::DISPLAY_STATE_ON, |
| 656 | bucketStartTimeNs + 202)); |
| 657 | events.push_back(CreateScreenStateChangedEvent(android::view::DISPLAY_STATE_OFF, |
| 658 | bucketStartTimeNs + bucketSizeNs + 801)); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 659 | |
| 660 | events.push_back(CreateSyncStartEvent(attributions1, "ReadEmail", bucketStartTimeNs + 200)); |
| 661 | events.push_back(CreateSyncEndEvent(attributions1, "ReadEmail", |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 662 | bucketStartTimeNs + bucketSizeNs + 300)); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 663 | |
| 664 | events.push_back(CreateSyncStartEvent(attributions1, "ReadDoc", bucketStartTimeNs + 400)); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 665 | events.push_back( |
| 666 | CreateSyncEndEvent(attributions1, "ReadDoc", bucketStartTimeNs + bucketSizeNs - 1)); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 667 | |
| 668 | events.push_back(CreateSyncStartEvent(attributions2, "ReadEmail", bucketStartTimeNs + 401)); |
| 669 | events.push_back(CreateSyncEndEvent(attributions2, "ReadEmail", |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 670 | bucketStartTimeNs + bucketSizeNs + 700)); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 671 | |
| 672 | sortLogEventsByTimestamp(&events); |
| 673 | |
| 674 | for (const auto& event : events) { |
| 675 | processor->OnLogEvent(event.get()); |
| 676 | } |
| 677 | |
| 678 | ConfigMetricsReportList reports; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 679 | vector<uint8_t> buffer; |
| 680 | processor->onDumpReport(cfgKey, bucketStartTimeNs + 2 * bucketSizeNs + 1, &buffer); |
| 681 | EXPECT_TRUE(buffer.size() > 0); |
| 682 | EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size())); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 683 | |
| 684 | EXPECT_EQ(reports.reports_size(), 1); |
| 685 | EXPECT_EQ(reports.reports(0).metrics_size(), 1); |
| 686 | StatsLogReport::DurationMetricDataWrapper metrics; |
| 687 | sortMetricDataByDimensionsValue(reports.reports(0).metrics(0).duration_metrics(), &metrics); |
| 688 | |
| 689 | EXPECT_EQ(metrics.data_size(), 3); |
| 690 | auto data = metrics.data(0); |
| 691 | EXPECT_EQ(data.dimensions_in_what().value_tuple().dimensions_value(0).field(), 1); |
| 692 | EXPECT_EQ(data.dimensions_in_what().value_tuple().dimensions_value(0).value_int(), 111); |
| 693 | EXPECT_FALSE(data.dimensions_in_condition().has_field()); |
| 694 | EXPECT_EQ(data.bucket_info_size(), 1); |
| 695 | EXPECT_EQ(data.bucket_info(0).duration_nanos(), 9); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 696 | EXPECT_EQ(data.bucket_info(0).start_bucket_elapsed_nanos(), bucketStartTimeNs); |
| 697 | EXPECT_EQ(data.bucket_info(0).end_bucket_elapsed_nanos(), bucketStartTimeNs + bucketSizeNs); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 698 | |
| 699 | data = metrics.data(1); |
| 700 | EXPECT_EQ(data.dimensions_in_what().value_tuple().dimensions_value(0).field(), 1); |
| 701 | EXPECT_EQ(data.dimensions_in_what().value_tuple().dimensions_value(0).value_int(), 111); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 702 | ValidateAttributionUidAndTagDimension(data.dimensions_in_condition(), |
| 703 | android::util::SYNC_STATE_CHANGED, 111, "App1"); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 704 | EXPECT_EQ(data.bucket_info_size(), 2); |
| 705 | EXPECT_EQ(data.bucket_info(0).duration_nanos(), bucketSizeNs - 201); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 706 | EXPECT_EQ(data.bucket_info(0).start_bucket_elapsed_nanos(), bucketStartTimeNs); |
| 707 | EXPECT_EQ(data.bucket_info(0).end_bucket_elapsed_nanos(), bucketStartTimeNs + bucketSizeNs); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 708 | EXPECT_EQ(data.bucket_info(1).duration_nanos(), 100); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 709 | EXPECT_EQ(data.bucket_info(1).start_bucket_elapsed_nanos(), bucketStartTimeNs + bucketSizeNs); |
| 710 | EXPECT_EQ(data.bucket_info(1).end_bucket_elapsed_nanos(), bucketStartTimeNs + 2 * bucketSizeNs); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 711 | |
| 712 | data = metrics.data(2); |
| 713 | EXPECT_EQ(data.dimensions_in_what().value_tuple().dimensions_value(0).field(), 1); |
| 714 | EXPECT_EQ(data.dimensions_in_what().value_tuple().dimensions_value(0).value_int(), 333); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 715 | ValidateAttributionUidAndTagDimension(data.dimensions_in_condition(), |
| 716 | android::util::SYNC_STATE_CHANGED, 333, "App2"); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 717 | EXPECT_EQ(data.bucket_info_size(), 2); |
| 718 | EXPECT_EQ(data.bucket_info(0).duration_nanos(), bucketSizeNs - 401); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 719 | EXPECT_EQ(data.bucket_info(0).start_bucket_elapsed_nanos(), bucketStartTimeNs); |
| 720 | EXPECT_EQ(data.bucket_info(0).end_bucket_elapsed_nanos(), bucketStartTimeNs + bucketSizeNs); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 721 | EXPECT_EQ(data.bucket_info(1).duration_nanos(), 700); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 722 | EXPECT_EQ(data.bucket_info(1).start_bucket_elapsed_nanos(), bucketStartTimeNs + bucketSizeNs); |
| 723 | EXPECT_EQ(data.bucket_info(1).end_bucket_elapsed_nanos(), bucketStartTimeNs + 2 * bucketSizeNs); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 724 | } |
| 725 | } |
| 726 | |
| 727 | #else |
| 728 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 729 | #endif |
| 730 | |
| 731 | } // namespace statsd |
| 732 | } // namespace os |
yro | 59cc24d | 2018-02-13 20:17:32 -0800 | [diff] [blame] | 733 | } // namespace android |