Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 1 | // Copyright (C) 2018 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 CreateStatsdConfig(int num_buckets, int threshold) { |
| 32 | StatsdConfig config; |
| 33 | config.add_allowed_log_source("AID_ROOT"); // LogEvent defaults to UID of root. |
| 34 | auto wakelockAcquireMatcher = CreateAcquireWakelockAtomMatcher(); |
| 35 | |
| 36 | *config.add_atom_matcher() = wakelockAcquireMatcher; |
| 37 | |
| 38 | auto countMetric = config.add_count_metric(); |
| 39 | countMetric->set_id(123456); |
| 40 | countMetric->set_what(wakelockAcquireMatcher.id()); |
| 41 | *countMetric->mutable_dimensions_in_what() = CreateAttributionUidDimensions( |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 42 | util::WAKELOCK_STATE_CHANGED, {Position::FIRST}); |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 43 | countMetric->set_bucket(FIVE_MINUTES); |
| 44 | |
| 45 | auto alert = config.add_alert(); |
| 46 | alert->set_id(StringToId("alert")); |
| 47 | alert->set_metric_id(123456); |
| 48 | alert->set_num_buckets(num_buckets); |
| 49 | alert->set_refractory_period_secs(10); |
| 50 | alert->set_trigger_if_sum_gt(threshold); |
| 51 | |
| 52 | // Two hours |
| 53 | config.set_ttl_in_seconds(2 * 3600); |
| 54 | return config; |
| 55 | } |
| 56 | |
| 57 | } // namespace |
| 58 | |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 59 | TEST(ConfigTtlE2eTest, TestCountMetric) { |
| 60 | const int num_buckets = 1; |
| 61 | const int threshold = 3; |
| 62 | auto config = CreateStatsdConfig(num_buckets, threshold); |
| 63 | const uint64_t alert_id = config.alert(0).id(); |
| 64 | const uint32_t refractory_period_sec = config.alert(0).refractory_period_secs(); |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 65 | |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 66 | int64_t bucketStartTimeNs = 10000000000; |
| 67 | int64_t bucketSizeNs = TimeUnitToBucketSizeInMillis(config.count_metric(0).bucket()) * 1000000; |
| 68 | |
| 69 | ConfigKey cfgKey; |
| 70 | auto processor = CreateStatsLogProcessor(bucketStartTimeNs, bucketStartTimeNs, config, cfgKey); |
| 71 | EXPECT_EQ(processor->mMetricsManagers.size(), 1u); |
| 72 | EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid()); |
| 73 | |
| 74 | std::vector<int> attributionUids1 = {111}; |
| 75 | std::vector<string> attributionTags1 = {"App1"}; |
| 76 | |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 77 | FieldValue fieldValue1(Field(util::WAKELOCK_STATE_CHANGED, (int32_t)0x02010101), |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 78 | Value((int32_t)111)); |
| 79 | HashableDimensionKey whatKey1({fieldValue1}); |
| 80 | MetricDimensionKey dimensionKey1(whatKey1, DEFAULT_DIMENSION_KEY); |
| 81 | |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 82 | FieldValue fieldValue2(Field(util::WAKELOCK_STATE_CHANGED, (int32_t)0x02010101), |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 83 | Value((int32_t)222)); |
| 84 | HashableDimensionKey whatKey2({fieldValue2}); |
| 85 | MetricDimensionKey dimensionKey2(whatKey2, DEFAULT_DIMENSION_KEY); |
| 86 | |
| 87 | auto event = CreateAcquireWakelockEvent(bucketStartTimeNs + 2, attributionUids1, |
| 88 | attributionTags1, "wl1"); |
| 89 | processor->OnLogEvent(event.get()); |
| 90 | |
| 91 | event = CreateAcquireWakelockEvent(bucketStartTimeNs + bucketSizeNs + 2, attributionUids1, |
| 92 | attributionTags1, "wl2"); |
| 93 | processor->OnLogEvent(event.get()); |
| 94 | |
| 95 | event = CreateAcquireWakelockEvent(bucketStartTimeNs + 25 * bucketSizeNs + 2, attributionUids1, |
| 96 | attributionTags1, "wl1"); |
| 97 | processor->OnLogEvent(event.get()); |
| 98 | |
| 99 | EXPECT_EQ((int64_t)(bucketStartTimeNs + 25 * bucketSizeNs + 2 + 2 * 3600 * NS_PER_SEC), |
| 100 | processor->mMetricsManagers.begin()->second->getTtlEndNs()); |
| 101 | |
| 102 | // Clear the data stored on disk as a result of the ttl. |
| 103 | vector<uint8_t> buffer; |
| 104 | processor->onDumpReport(cfgKey, bucketStartTimeNs + 25 * bucketSizeNs + 3, false, true, |
| 105 | ADB_DUMP, FAST, &buffer); |
| 106 | } |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 107 | |
| 108 | #else |
| 109 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 110 | #endif |
| 111 | |
| 112 | } // namespace statsd |
| 113 | } // namespace os |
| 114 | } // namespace android |