• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved.
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 #include <iostream>
17 #include <cstdio>
18 #include <ctime>
19 #include <unistd.h>
20 
21 #include "ffrt.h"
22 
23 #define NSEC_PER_SEC 1000000000L
24 namespace {
25 constexpr int TWO_NUM = 2;
26 constexpr int VALUE_NUM = 100;
27 
GetNanos()28 static long GetNanos()
29 {
30     struct timespec ts;
31     clock_gettime(CLOCK_MONOTONIC, &ts);
32     return ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec;
33 }
34 } // namespace
35 
main(int argc,char * argv[])36 int main(int argc, char *argv[])
37 {
38     if (argc != TWO_NUM) {
39         return 0;
40     }
41     int i = 0;
42     for (; i < TWO_NUM; i++) {
43         std::cout << "hook" << std::endl;
44         sleep(TWO_NUM);
45         ffrt::submit([i] { std::cout << "num: " << i << std::endl; });
46     }
47     ffrt::wait();
48 
49     long startTime = 0;
50     long endTime = 0;
51     long elapsedTime = 0;
52     std::cout << "start" << std::endl;
53     startTime = GetNanos();
54     for (int j = 0; j < std::atoi(argv[1]); ++j) {
55         int x = 1;
56         ffrt::submit([&] { x = VALUE_NUM; }, {}, {&x});
57     }
58     ffrt::wait();
59     endTime = GetNanos();
60     elapsedTime = endTime - startTime;
61     printf("Elapsed time: %ld nanoseconds, mean time: %ld\n", elapsedTime, elapsedTime / std::atoi(argv[1]));
62     return 0;
63 }