blob: ae8bf426523556afb0f5c861b335b432aa77f74b [file] [log] [blame]
Yao Chen5154a3792017-10-30 22:57:06 -07001// 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 <gmock/gmock.h>
16#include <gtest/gtest.h>
17
18#include "src/condition/ConditionWizard.h"
19#include "src/metrics/duration_helper/MaxDurationTracker.h"
20
21#include <stdio.h>
22#include <set>
23#include <unordered_map>
24#include <vector>
25
26using namespace android::os::statsd;
27using namespace testing;
28using android::sp;
29using std::set;
30using std::unordered_map;
31using std::vector;
32
33#ifdef __ANDROID__
34
35class MockConditionWizard : public ConditionWizard {
36public:
37 MOCK_METHOD2(
38 query,
39 ConditionState(const int conditionIndex,
40 const std::map<std::string, HashableDimensionKey>& conditionParameters));
41};
42
43TEST(MaxDurationTrackerTest, TestSimpleMaxDuration) {
44 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
45
46 vector<DurationBucketInfo> buckets;
47 ConditionKey key1;
48
49 uint64_t bucketStartTimeNs = 10000000000;
50 uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
51
52 MaxDurationTracker tracker(wizard, -1, bucketStartTimeNs, bucketSizeNs, buckets);
53
54 tracker.noteStart("", true, bucketStartTimeNs, key1);
55 tracker.noteStop("", bucketStartTimeNs + 10);
56
57 tracker.noteStart("", true, bucketStartTimeNs + 20, key1);
58 tracker.noteStop("", bucketStartTimeNs + 40);
59
60 tracker.flushIfNeeded(bucketStartTimeNs + bucketSizeNs + 1);
61 EXPECT_EQ(1u, buckets.size());
62 EXPECT_EQ(20, buckets[0].duration_nanos());
63}
64
65TEST(MaxDurationTrackerTest, TestCrossBucketBoundary) {
66 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
67
68 vector<DurationBucketInfo> buckets;
69 ConditionKey key1;
70
71 uint64_t bucketStartTimeNs = 10000000000;
72 uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
73
74 MaxDurationTracker tracker(wizard, -1, bucketStartTimeNs, bucketSizeNs, buckets);
75
76 tracker.noteStart("", true, bucketStartTimeNs + 1, key1);
77 tracker.flushIfNeeded(bucketStartTimeNs + (2 * bucketSizeNs) + 1);
78
79 EXPECT_EQ(2u, buckets.size());
80 EXPECT_EQ((long long)(bucketSizeNs - 1), buckets[0].duration_nanos());
81 EXPECT_EQ((long long)bucketSizeNs, buckets[1].duration_nanos());
82}
83
84TEST(MaxDurationTrackerTest, TestMaxDurationWithCondition) {
85 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
86
87 ConditionKey key1;
88 key1["APP_BACKGROUND"] = "1:maps|";
89
90 EXPECT_CALL(*wizard, query(_, key1)) // #4
91 .WillOnce(Return(ConditionState::kFalse));
92
93 vector<DurationBucketInfo> buckets;
94
95 uint64_t bucketStartTimeNs = 10000000000;
96 uint64_t eventStartTimeNs = bucketStartTimeNs + 1;
97 uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
98 int64_t durationTimeNs = 2 * 1000;
99
100 MaxDurationTracker tracker(wizard, 1, bucketStartTimeNs, bucketSizeNs, buckets);
101
102 tracker.noteStart("2:maps", true, eventStartTimeNs, key1);
103
104 tracker.onSlicedConditionMayChange(eventStartTimeNs + 5);
105
106 tracker.noteStop("2:maps", eventStartTimeNs + durationTimeNs);
107
108 tracker.flushIfNeeded(bucketStartTimeNs + bucketSizeNs + 1);
109 EXPECT_EQ(1u, buckets.size());
110 EXPECT_EQ(5, buckets[0].duration_nanos());
111}
112
113#else
114GTEST_LOG_(INFO) << "This test does nothing.\n";
115#endif