SleepTest.cpp 8.2 KB
Newer Older
M
mamingshuai 已提交
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
/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * 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 specific language governing permissions and
 * limitations under the License.
 */

#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
#include <gtest/gtest.h>
#include "log.h"
#include "utils.h"
#include "ClockID.h"

using namespace testing::ext;

const int SLEEP_ACCURACY = 21 * 1000; // 20 ms, with 1ms deviation
const int ACCURACY_TEST_LOOPS = 5;    // loops for accuracy test, than count average value

N
nan-xiansen 已提交
30
class UsleepParamTest : public testing::TestWithParam<int> {
N
nanxiansen 已提交
31
};
N
nan-xiansen 已提交
32
class SleepParamTest : public testing::TestWithParam<int> {
N
nanxiansen 已提交
33
};
N
nan-xiansen 已提交
34
class SleepTest : public testing::Test {
N
nanxiansen 已提交
35
};
M
mamingshuai 已提交
36

N
nan-xiansen 已提交
37
static void UsleepAccuracyTest(int interVal)
M
mamingshuai 已提交
38
{
N
nanxiansen 已提交
39
    int interval = interVal;
M
mamingshuai 已提交
40 41 42 43
    LOG("\ntest interval:%d", interval);
    struct timespec time1 = {0}, time2 = {0};
    long duration; // unit: us
    double d = 0.0;
N
nanxiansen 已提交
44 45
    for (int i = 1; i <= ACCURACY_TEST_LOOPS; i++)
    {
M
mamingshuai 已提交
46 47 48 49
        clock_gettime(CLOCK_MONOTONIC, &time1);
        int rt = usleep(interval);
        clock_gettime(CLOCK_MONOTONIC, &time2);
        EXPECT_EQ(rt, 0);
N
nanxiansen 已提交
50
        duration = (time2.tv_sec * 1000000 + time2.tv_nsec / 1000) - (time1.tv_sec * 1000000 + time1.tv_nsec / 1000);
M
mamingshuai 已提交
51 52 53 54 55 56 57
        d += duration;
    }
    d = d / ACCURACY_TEST_LOOPS; // average
    LOG("average duration: %.2f", d);
    EXPECT_GE(d, interval) << "actual sleep time shoud greater or equal to the input-parameter\n";
    ASSERT_NEAR(d, interval, SLEEP_ACCURACY) << "usleep accuracy check fail\n";
}
N
nanxiansen 已提交
58

N
nan-xiansen 已提交
59
static void *TestUsleepAccuracyThread(void *param)
N
nanxiansen 已提交
60 61
{
    int interval = (int)param;
N
nan-xiansen 已提交
62 63
    UsleepAccuracyTest(interval);
    return nullptr;
N
nanxiansen 已提交
64 65 66 67 68 69 70 71 72 73 74
}

/**
 * @tc.number SUB_KERNEL_TIME_API_USLEEP_0100
 * @tc.name   usleep accuracy test
 * @tc.desc   [C- SOFTWARE -0200]
 */
HWTEST_P(UsleepParamTest, testUsleepAccuracy, Performance | SmallTest | Level1)
{
    int interVal = GetParam();
    pthread_t tid;
N
nan-xiansen 已提交
75
    pthread_create(&tid, 0, TestUsleepAccuracyThread, (void *)interVal);
N
nanxiansen 已提交
76 77 78
    pthread_setschedprio(tid, 1);
    pthread_join(tid, 0);
}
M
mamingshuai 已提交
79
INSTANTIATE_TEST_CASE_P(SleepTest, UsleepParamTest,
N
nanxiansen 已提交
80
                        testing::Values(100, 1000, 10 * 1000, 20 * 1000, 30 * 1000, 300 * 1000, 3000 * 1000));
M
mamingshuai 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94

/**
 * @tc.number SUB_KERNEL_TIME_API_SLEEP_0100
 * @tc.name   sleep accuracy test
 * @tc.desc   [C- SOFTWARE -0200]
 */
HWTEST_P(SleepParamTest, testSleepAccuracy, Performance | SmallTest | Level1)
{
    int testLoop = 3;
    int interval = GetParam();
    LOG("\ntest interval:%d", interval);
    struct timespec time1 = {0}, time2 = {0};
    double duration;
    double d = 0.0;
N
nanxiansen 已提交
95 96
    for (int i = 1; i <= testLoop; i++)
    {
M
mamingshuai 已提交
97 98 99 100
        clock_gettime(CLOCK_MONOTONIC, &time1);
        int rt = sleep(interval);
        clock_gettime(CLOCK_MONOTONIC, &time2);
        EXPECT_EQ(rt, 0);
N
nanxiansen 已提交
101
        duration = (time2.tv_sec - time1.tv_sec) + (time2.tv_nsec - time1.tv_nsec) / 1000000000.0;
M
mamingshuai 已提交
102 103 104 105 106
        LOG("testloop %d, actual sleep duration: %.1f s", i, duration);
        d += duration;
    }
    d = d / testLoop; // average
    LOG("average duration: %.2f", d);
N
nanxiansen 已提交
107
    ASSERT_NEAR(d, interval, interval * 0.03) << "sleep accuracy check fail\n";
M
mamingshuai 已提交
108 109 110 111 112 113 114 115 116 117
}
INSTANTIATE_TEST_CASE_P(SleepTest, SleepParamTest, testing::Values(1, 5, 30));

/**
 * @tc.number SUB_KERNEL_TIME_API_NANOSLEEP_0100
 * @tc.name   nanosleep accuracy test
 * @tc.desc   [C- SOFTWARE -0200]
 */
HWTEST_F(SleepTest, testNanosleepAccuracy, Performance | SmallTest | Level2)
{
N
nanxiansen 已提交
118
    long interval = 50 * 1000 * 1000;
M
mamingshuai 已提交
119 120 121 122 123 124
    struct timespec req = {0, interval};
    struct timespec rem = {0, 0};

    struct timespec time1 = {0}, time2 = {0};
    double duration;
    double d = 0.0;
N
nanxiansen 已提交
125 126
    for (int i = 1; i <= ACCURACY_TEST_LOOPS; i++)
    {
M
mamingshuai 已提交
127 128 129 130
        clock_gettime(CLOCK_MONOTONIC, &time1);
        int rt = nanosleep(&req, &rem);
        clock_gettime(CLOCK_MONOTONIC, &time2);
        EXPECT_EQ(rt, 0);
N
nanxiansen 已提交
131
        duration = (time2.tv_sec * 1000000 + time2.tv_nsec / 1000) - (time1.tv_sec * 1000000 + time1.tv_nsec / 1000);
M
mamingshuai 已提交
132 133 134 135 136
        LOG("testloop %d, actual sleep duration: %.1f s", i, duration);
        d += duration;
    }
    d = d / ACCURACY_TEST_LOOPS; // average
    LOG("average duration: %.2f", d);
N
nanxiansen 已提交
137
    ASSERT_NEAR(d, interval / 1000, SLEEP_ACCURACY) << "sleep accuracy check fail\n";
M
mamingshuai 已提交
138 139 140 141 142 143 144 145 146
}

/**
 * @tc.number SUB_KERNEL_TIME_API_CLOCK_NANOSLEEP_0100
 * @tc.name   clock_nanosleep accuracy test
 * @tc.desc   [C- SOFTWARE -0200]
 */
HWTEST_F(SleepTest, testClockNanosleepAccuracy, Performance | SmallTest | Level2)
{
N
nanxiansen 已提交
147
    long interval = 25 * 1000 * 1000;
M
mamingshuai 已提交
148 149 150 151 152
    struct timespec req = {0, interval};
    struct timespec rem = {0, 0};
    struct timespec time1 = {0}, time2 = {0};
    double duration;
    double d = 0.0;
N
nanxiansen 已提交
153 154
    for (int i = 1; i <= ACCURACY_TEST_LOOPS; i++)
    {
M
mamingshuai 已提交
155 156 157 158
        clock_gettime(CLOCK_MONOTONIC, &time1);
        int rt = clock_nanosleep(CLOCK_REALTIME, 0, &req, &rem);
        clock_gettime(CLOCK_MONOTONIC, &time2);
        EXPECT_EQ(rt, 0);
N
nanxiansen 已提交
159
        duration = (time2.tv_sec * 1000000 + time2.tv_nsec / 1000) - (time1.tv_sec * 1000000 + time1.tv_nsec / 1000);
M
mamingshuai 已提交
160 161 162 163 164
        LOG("testloop %d, actual sleep duration: %.1f s", i, duration);
        d += duration;
    }
    d = d / ACCURACY_TEST_LOOPS; // average
    LOG("average duration: %.2f", d);
N
nanxiansen 已提交
165
    ASSERT_NEAR(d, interval / 1000, SLEEP_ACCURACY) << "sleep accuracy check fail\n";
M
mamingshuai 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
}

/**
 * @tc.number SUB_KERNEL_TIME_API_CLOCK_NANOSLEEP_0200
 * @tc.name   clock_nanosleep fail test - non-support clock_id
 * @tc.desc   [C- SOFTWARE -0200]
 */
HWTEST_P(AllClockIDTest, testClockNanosleepInvalidID, Reliability | SmallTest | Level2)
{
    clockid_t cid = GetParam();
    const char* cname = ALL_CLOCKS_NAME[cid];
    LOG("test %s", cname);
    struct timespec req = {0, 100};
    struct timespec rem = {0};
    int rt = clock_nanosleep(cid, 0, &req, &rem);
N
nanxiansen 已提交
181
    if (cid == CLOCK_SGI_CYCLE) {
M
mamingshuai 已提交
182
        ASSERT_EQ(rt, EINVAL) << cname << " should not support.\n";
N
nanxiansen 已提交
183
    } 
M
mamingshuai 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
}
INSTANTIATE_TEST_CASE_P(SleepTest, AllClockIDTest, ALL_CLOCK_IDS);

/**
 * @tc.number SUB_KERNEL_TIME_API_CLOCK_NANOSLEEP_0300
 * @tc.name   clock_nanosleep fail test - invalid parameter
 * @tc.desc   [C- SOFTWARE -0200]
 */
HWTEST_F(SleepTest, testClockNanosleepInvalidPara, Reliability | SmallTest | Level2)
{
    struct timespec req = {0, 100};
    struct timespec rem = {0};
    int rt;

    // invlid clock_id
    int id = GetRandom(1000) + 12;
    LOG("check invlid clockid: %d...", id);
    rt = clock_nanosleep(id, 0, &req, &rem);
    EXPECT_EQ(rt, EINVAL);

    id = -GetRandom(1000) - 12;
    LOG("check invlid clockid: %d...", id);
    rt = clock_nanosleep(id, 0, &req, &rem);
    EXPECT_EQ(rt, EINVAL);

    // invlid flag
    int flag = TIMER_ABSTIME;
    LOG("check invlid flag: %d...", flag);
    rt = clock_nanosleep(CLOCK_REALTIME, flag, &req, &rem);
    EXPECT_EQ(rt, ENOTSUP);
    flag = GetRandom(100) + 1;
    LOG("check invlid flag: %d...", flag);
    rt = clock_nanosleep(CLOCK_REALTIME, flag, &req, &rem);
    EXPECT_EQ(rt, EINVAL);
    flag = -GetRandom(100) - 1;
    LOG("check invlid flag: %d...", flag);
    rt = clock_nanosleep(CLOCK_REALTIME, flag, &req, &rem);
    EXPECT_EQ(rt, EINVAL);

    // invlid timespec
N
nanxiansen 已提交
224
    req.tv_sec = -1;
M
mamingshuai 已提交
225 226 227 228
    req.tv_nsec = 1;
    LOG("check invlid timespec: tv_sec=-1 ...");
    rt = clock_nanosleep(CLOCK_REALTIME, 0, &req, &rem);
    EXPECT_EQ(rt, EINVAL);
N
nanxiansen 已提交
229
    req.tv_sec = 1;
M
mamingshuai 已提交
230 231 232 233
    req.tv_nsec = -1;
    LOG("check invlid timespec: tv_nsec=-1 ...");
    rt = clock_nanosleep(CLOCK_REALTIME, 0, &req, &rem);
    EXPECT_EQ(rt, EINVAL);
N
nanxiansen 已提交
234 235
    req.tv_sec = 1;
    req.tv_nsec = 1000 * 1000 * 1000 + 1;
M
mamingshuai 已提交
236 237 238 239 240 241 242
    LOG("check invlid timespec: tv_nsec overflow ...");
    rt = clock_nanosleep(CLOCK_REALTIME, 0, &req, &rem);
    EXPECT_EQ(rt, EINVAL);

    // invlid remain
    // para not used, so not tested.
}