blob: f2abe7b70720476fe8b5a4341d8290171fbd8520 [file] [log] [blame]
Yao Chen5154a372017-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
Yao Chen93fe3a32017-11-02 13:52:59 -070015#include "metrics_test_helper.h"
Yao Chen5154a372017-10-30 22:57:06 -070016#include "src/condition/ConditionWizard.h"
17#include "src/metrics/duration_helper/MaxDurationTracker.h"
18
Yao Chen93fe3a32017-11-02 13:52:59 -070019#include <gmock/gmock.h>
20#include <gtest/gtest.h>
Yao Chen5154a372017-10-30 22:57:06 -070021#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
Yao Chen93fe3a32017-11-02 13:52:59 -070035namespace android {
36namespace os {
37namespace statsd {
Yao Chen5154a372017-10-30 22:57:06 -070038
39TEST(MaxDurationTrackerTest, TestSimpleMaxDuration) {
40 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
41
42 vector<DurationBucketInfo> buckets;
43 ConditionKey key1;
44
45 uint64_t bucketStartTimeNs = 10000000000;
46 uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
47
48 MaxDurationTracker tracker(wizard, -1, bucketStartTimeNs, bucketSizeNs, buckets);
49
50 tracker.noteStart("", true, bucketStartTimeNs, key1);
51 tracker.noteStop("", bucketStartTimeNs + 10);
52
53 tracker.noteStart("", true, bucketStartTimeNs + 20, key1);
54 tracker.noteStop("", bucketStartTimeNs + 40);
55
56 tracker.flushIfNeeded(bucketStartTimeNs + bucketSizeNs + 1);
57 EXPECT_EQ(1u, buckets.size());
58 EXPECT_EQ(20, buckets[0].duration_nanos());
59}
60
61TEST(MaxDurationTrackerTest, TestCrossBucketBoundary) {
62 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
63
64 vector<DurationBucketInfo> buckets;
65 ConditionKey key1;
66
67 uint64_t bucketStartTimeNs = 10000000000;
68 uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
69
70 MaxDurationTracker tracker(wizard, -1, bucketStartTimeNs, bucketSizeNs, buckets);
71
72 tracker.noteStart("", true, bucketStartTimeNs + 1, key1);
73 tracker.flushIfNeeded(bucketStartTimeNs + (2 * bucketSizeNs) + 1);
74
75 EXPECT_EQ(2u, buckets.size());
76 EXPECT_EQ((long long)(bucketSizeNs - 1), buckets[0].duration_nanos());
77 EXPECT_EQ((long long)bucketSizeNs, buckets[1].duration_nanos());
78}
79
80TEST(MaxDurationTrackerTest, TestMaxDurationWithCondition) {
81 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
82
83 ConditionKey key1;
84 key1["APP_BACKGROUND"] = "1:maps|";
85
86 EXPECT_CALL(*wizard, query(_, key1)) // #4
87 .WillOnce(Return(ConditionState::kFalse));
88
89 vector<DurationBucketInfo> buckets;
90
91 uint64_t bucketStartTimeNs = 10000000000;
92 uint64_t eventStartTimeNs = bucketStartTimeNs + 1;
93 uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
94 int64_t durationTimeNs = 2 * 1000;
95
96 MaxDurationTracker tracker(wizard, 1, bucketStartTimeNs, bucketSizeNs, buckets);
97
98 tracker.noteStart("2:maps", true, eventStartTimeNs, key1);
99
100 tracker.onSlicedConditionMayChange(eventStartTimeNs + 5);
101
102 tracker.noteStop("2:maps", eventStartTimeNs + durationTimeNs);
103
104 tracker.flushIfNeeded(bucketStartTimeNs + bucketSizeNs + 1);
105 EXPECT_EQ(1u, buckets.size());
106 EXPECT_EQ(5, buckets[0].duration_nanos());
107}
108
Yao Chen93fe3a32017-11-02 13:52:59 -0700109} // namespace statsd
110} // namespace os
111} // namespace android
Yao Chen5154a372017-10-30 22:57:06 -0700112#else
113GTEST_LOG_(INFO) << "This test does nothing.\n";
114#endif