Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [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 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 15 | #include "metrics_test_helper.h" |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 16 | #include "src/condition/ConditionWizard.h" |
| 17 | #include "src/metrics/duration_helper/MaxDurationTracker.h" |
| 18 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 19 | #include <gmock/gmock.h> |
| 20 | #include <gtest/gtest.h> |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include <set> |
| 23 | #include <unordered_map> |
| 24 | #include <vector> |
| 25 | |
| 26 | using namespace android::os::statsd; |
| 27 | using namespace testing; |
| 28 | using android::sp; |
| 29 | using std::set; |
| 30 | using std::unordered_map; |
| 31 | using std::vector; |
| 32 | |
| 33 | #ifdef __ANDROID__ |
| 34 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 35 | namespace android { |
| 36 | namespace os { |
| 37 | namespace statsd { |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 38 | |
| 39 | TEST(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 | |
| 61 | TEST(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 | |
| 80 | TEST(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 Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 109 | } // namespace statsd |
| 110 | } // namespace os |
| 111 | } // namespace android |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 112 | #else |
| 113 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 114 | #endif |