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/OringDurationTracker.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 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 26 | using namespace testing; |
| 27 | using android::sp; |
| 28 | using std::set; |
| 29 | using std::unordered_map; |
| 30 | using std::vector; |
| 31 | |
| 32 | #ifdef __ANDROID__ |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 33 | namespace android { |
| 34 | namespace os { |
| 35 | namespace statsd { |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 36 | |
| 37 | TEST(OringDurationTrackerTest, TestDurationOverlap) { |
| 38 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 39 | |
| 40 | ConditionKey key1; |
| 41 | key1["APP_BACKGROUND"] = "1:maps|"; |
| 42 | |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 43 | vector<DurationBucket> buckets; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 44 | |
| 45 | uint64_t bucketStartTimeNs = 10000000000; |
| 46 | uint64_t eventStartTimeNs = bucketStartTimeNs + 1; |
| 47 | uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL; |
| 48 | int64_t durationTimeNs = 2 * 1000; |
| 49 | |
| 50 | OringDurationTracker tracker(wizard, 1, bucketStartTimeNs, bucketSizeNs, buckets); |
| 51 | |
| 52 | tracker.noteStart("2:maps", true, eventStartTimeNs, key1); |
| 53 | tracker.noteStart("2:maps", true, eventStartTimeNs + 10, key1); // overlapping wl |
| 54 | |
| 55 | tracker.noteStop("2:maps", eventStartTimeNs + durationTimeNs); |
| 56 | |
| 57 | tracker.flushIfNeeded(bucketStartTimeNs + bucketSizeNs + 1); |
| 58 | EXPECT_EQ(1u, buckets.size()); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 59 | EXPECT_EQ(durationTimeNs, buckets[0].mDuration); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | TEST(OringDurationTrackerTest, TestDurationConditionChange) { |
| 63 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 64 | |
| 65 | ConditionKey key1; |
| 66 | key1["APP_BACKGROUND"] = "1:maps|"; |
| 67 | |
| 68 | EXPECT_CALL(*wizard, query(_, key1)) // #4 |
| 69 | .WillOnce(Return(ConditionState::kFalse)); |
| 70 | |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 71 | vector<DurationBucket> buckets; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 72 | |
| 73 | uint64_t bucketStartTimeNs = 10000000000; |
| 74 | uint64_t eventStartTimeNs = bucketStartTimeNs + 1; |
| 75 | uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL; |
| 76 | int64_t durationTimeNs = 2 * 1000; |
| 77 | |
| 78 | OringDurationTracker tracker(wizard, 1, bucketStartTimeNs, bucketSizeNs, buckets); |
| 79 | |
| 80 | tracker.noteStart("2:maps", true, eventStartTimeNs, key1); |
| 81 | |
| 82 | tracker.onSlicedConditionMayChange(eventStartTimeNs + 5); |
| 83 | |
| 84 | tracker.noteStop("2:maps", eventStartTimeNs + durationTimeNs); |
| 85 | |
| 86 | tracker.flushIfNeeded(bucketStartTimeNs + bucketSizeNs + 1); |
| 87 | EXPECT_EQ(1u, buckets.size()); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 88 | EXPECT_EQ(5, buckets[0].mDuration); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 89 | } |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 90 | } // namespace statsd |
| 91 | } // namespace os |
| 92 | } // namespace android |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 93 | #else |
| 94 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 95 | #endif |