blob: 16adbddf7dec4d72ca9ef28349db959c217abb60 [file] [log] [blame]
Yangster-macb142cc82018-03-30 15:22:08 -07001// 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
23namespace android {
24namespace os {
25namespace statsd {
26
27#ifdef __ANDROID__
28
29namespace {
30
31StatsdConfig 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 Huang3eb84d42020-03-17 10:31:22 -070042 util::WAKELOCK_STATE_CHANGED, {Position::FIRST});
Yangster-macb142cc82018-03-30 15:22:08 -070043 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
tsaichristine7747d372020-02-28 17:36:59 -080059TEST(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-macb142cc82018-03-30 15:22:08 -070065
tsaichristine7747d372020-02-28 17:36:59 -080066 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 Huang3eb84d42020-03-17 10:31:22 -070077 FieldValue fieldValue1(Field(util::WAKELOCK_STATE_CHANGED, (int32_t)0x02010101),
tsaichristine7747d372020-02-28 17:36:59 -080078 Value((int32_t)111));
79 HashableDimensionKey whatKey1({fieldValue1});
80 MetricDimensionKey dimensionKey1(whatKey1, DEFAULT_DIMENSION_KEY);
81
Jeffrey Huang3eb84d42020-03-17 10:31:22 -070082 FieldValue fieldValue2(Field(util::WAKELOCK_STATE_CHANGED, (int32_t)0x02010101),
tsaichristine7747d372020-02-28 17:36:59 -080083 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-macb142cc82018-03-30 15:22:08 -0700107
108#else
109GTEST_LOG_(INFO) << "This test does nothing.\n";
110#endif
111
112} // namespace statsd
113} // namespace os
114} // namespace android