summaryrefslogtreecommitdiff
path: root/libperfmgr/tests/RequestGroupTest.cc
blob: f3393c804d4eea91e75c511e562d97f7c9174afd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specic language governing permissions and
 * limitations under the License.
 */

#include <gtest/gtest.h>

#include <algorithm>
#include <thread>

#include "perfmgr/RequestGroup.h"

namespace android {
namespace perfmgr {

using namespace std::chrono_literals;

constexpr double kTIMING_TOLERANCE_MS = std::chrono::milliseconds(25).count();

// Test GetRequestValue()
TEST(RequestGroupTest, GetRequestValueTest) {
    std::string test_str = "TESTREQ_1";
    RequestGroup req(test_str);
    EXPECT_EQ(test_str, req.GetRequestValue());
}

// Test AddRequest()
TEST(RequestGroupTest, AddRequestTest) {
    RequestGroup req("");
    auto start = std::chrono::steady_clock::now();
    auto duration = 500ms;
    bool ret = req.AddRequest("INTERACTION", start + duration);
    EXPECT_EQ(true, ret);
    auto sleep_time = 200ms;
    std::this_thread::sleep_for(sleep_time);
    std::chrono::milliseconds expire_time;
    bool active = req.GetExpireTime(&expire_time);
    EXPECT_NEAR((duration - sleep_time).count(), expire_time.count(),
                kTIMING_TOLERANCE_MS);
    EXPECT_EQ(true, active);
}

// Test AddRequest() with a huge expire time which could be done in some long
// persist power hint such as VR_MODE
TEST(RequestGroupTest, AddRequestNoExpireTest) {
    RequestGroup req("");
    bool ret = req.AddRequest("INTERACTION", ReqTime::max());
    EXPECT_EQ(true, ret);
    std::chrono::milliseconds expire_time;
    bool active = req.GetExpireTime(&expire_time);
    auto expect = std::chrono::duration_cast<std::chrono::milliseconds>(
        ReqTime::max() - std::chrono::steady_clock::now());
    EXPECT_NEAR(expect.count(), expire_time.count(), kTIMING_TOLERANCE_MS);
    // expire time is greater than 1 year
    EXPECT_LE(365 * 24 * 60 * 60 * 1000, expire_time.count());
    EXPECT_EQ(true, active);
}

// Test AddRequest() and expires
TEST(RequestGroupTest, AddRequestTestExpire) {
    RequestGroup req("");
    auto start = std::chrono::steady_clock::now();
    auto duration = 5ms;
    bool ret = req.AddRequest("INTERACTION", start + duration);
    EXPECT_EQ(true, ret);
    ret = req.AddRequest("INTERACTION", start + duration + 1ms);
    EXPECT_EQ(false, ret);
    std::this_thread::sleep_for(duration + 10ms);
    std::chrono::milliseconds expire_time;
    bool active = req.GetExpireTime(&expire_time);
    EXPECT_EQ(std::chrono::milliseconds::max(), expire_time);
    EXPECT_EQ(false, active);
}

// Test AddRequest() with new value
TEST(RequestGroupTest, AddRequestNewValue) {
    RequestGroup req("");
    auto start = std::chrono::steady_clock::now();
    auto duration = 5000ms;
    bool ret = req.AddRequest("INTERACTION", start + duration);
    EXPECT_EQ(true, ret);
    std::chrono::milliseconds expire_time;
    bool active = req.GetExpireTime(&expire_time);
    EXPECT_NEAR(duration.count(), expire_time.count(), kTIMING_TOLERANCE_MS);
    EXPECT_EQ(true, active);
    // Add a request shorter than the current outstanding one, expiration time
    // not changed
    auto shorter_duration = 100ms;
    ret = req.AddRequest("INTERACTION", start + shorter_duration);
    EXPECT_EQ(false, ret);
    active = req.GetExpireTime(&expire_time);
    EXPECT_NEAR(duration.count(), expire_time.count(), kTIMING_TOLERANCE_MS);
    EXPECT_EQ(true, active);
    // Add a request longer than the current outstanding one, expiration time
    // changed
    duration = 10000ms;
    ret = req.AddRequest("INTERACTION", start + duration);
    EXPECT_EQ(false, ret);
    active = req.GetExpireTime(&expire_time);
    EXPECT_NEAR(duration.count(), expire_time.count(), kTIMING_TOLERANCE_MS);
    EXPECT_EQ(true, active);
}

// Test multiple AddRequest() with different hint_type
TEST(RequestGroupTest, AddRequestTestMutiple) {
    RequestGroup req("");
    auto start = std::chrono::steady_clock::now();
    auto duration_interact = 500ms;
    req.AddRequest("INTERACTION", start + duration_interact);
    auto duration_launch = 5000ms;
    req.AddRequest("LAUNCH", start + duration_launch);
    std::chrono::milliseconds expire_time;
    bool active = req.GetExpireTime(&expire_time);
    EXPECT_NEAR(std::min(duration_interact, duration_launch).count(),
                expire_time.count(), kTIMING_TOLERANCE_MS);
    EXPECT_EQ(true, active);
}

// Test RemoveRequest()
TEST(RequestGroupTest, RemoveRequestTest) {
    RequestGroup req("");
    auto start = std::chrono::steady_clock::now();
    auto duration_interact = 500ms;
    req.AddRequest("INTERACTION", start + duration_interact);
    bool ret = req.RemoveRequest("INTERACTION");
    EXPECT_EQ(true, ret);
    std::chrono::milliseconds expire_time;
    bool active = req.GetExpireTime(&expire_time);
    EXPECT_EQ(std::chrono::milliseconds::max(), expire_time);
    EXPECT_EQ(false, active);
    // Test removing an already-removed request
    ret = req.RemoveRequest("INTERACTION");
    EXPECT_EQ(false, ret);
}

// Test multiple RemoveRequest() with different hint_type
TEST(RequestGroupTest, RemoveRequestTestMutiple) {
    RequestGroup req("");
    auto start = std::chrono::steady_clock::now();
    auto duration_interact = 500ms;
    req.AddRequest("INTERACTION", start + duration_interact);
    auto duration_launch = 50000ms;
    req.AddRequest("LAUNCH", start + duration_launch);
    req.RemoveRequest("INTERACTION");
    std::chrono::milliseconds expire_time;
    bool active = req.GetExpireTime(&expire_time);
    EXPECT_NEAR(duration_launch.count(), expire_time.count(),
                kTIMING_TOLERANCE_MS);
    EXPECT_EQ(true, active);
}

}  // namespace perfmgr
}  // namespace android