• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
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  */
15 
16 #ifndef TEST_TIMER_H
17 #define TEST_TIMER_H
18 
19 #include "print_log.h"
20 
21 #include <hctest.h>
22 #include <time.h>
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 double CalcTimeSpecDiff(struct timespec before, struct timespec after);
29 
30 unsigned long long TimeConsumingOperation(void);
31 
32 #ifdef __cplusplus
33 }
34 #endif
35 
36 #define RUN_AND_PRINT_ELAPSED_TIME(out_result, operation)               \
37     do {                                                                \
38         struct timespec __before, __after;                              \
39         int __beforeRes = clock_gettime(CLOCK_MONOTONIC, &__before);    \
40         out_result = operation;                                         \
41         int __afterRes = clock_gettime(CLOCK_MONOTONIC, &__after);      \
42         TEST_ASSERT_TRUE(__beforeRes == 0 && __afterRes == 0);          \
43         double __mseconds = CalcTimeSpecDiff(__before, __after);        \
44         LOGI("operation %s took %f ms", #operation, __mseconds);        \
45     } while (0)
46 
47 #define RUN_AND_PRINT_ELAPSED_TIME_WITHOUT_RESULT(operation)            \
48     do {                                                                \
49         struct timespec __before, __after;                              \
50         int __beforeRes = clock_gettime(CLOCK_MONOTONIC, &__before);    \
51         (void)(operation);                                              \
52         int __afterRes = clock_gettime(CLOCK_MONOTONIC, &__after);      \
53         TEST_ASSERT_TRUE(__beforeRes == 0 && __afterRes == 0);          \
54         double __mseconds = CalcTimeSpecDiff(__before, __after);        \
55         LOGI("operation %s took %f ms", #operation, __mseconds);        \
56     } while (0)
57 
58 #endif // TEST_TIMER_H
59