1 /**
2 * Copyright 2020 Huawei Technologies Co., Ltd
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include <iostream>
17 #include <string>
18 #include "common/common_test.h"
19
20 #ifndef ENABLE_PROFILE
21 #define ENABLE_PROFILE
22 #endif
23
24 #include "utils/profile.h"
25
26 namespace mindspore {
27 class TestProfile : public UT::Common {
28 public:
TestProfile()29 TestProfile() {}
~TestProfile()30 virtual ~TestProfile() {}
31
TearDown()32 virtual void TearDown() {}
33 };
34
test_lap(Profile * prof)35 static void test_lap(Profile* prof) {
36 int nums[] = {30, 20, 70};
37 int cnt = 0;
38 for (auto elem : nums) {
39 WITH(prof->Lap(cnt))[elem]()->void { usleep(elem); };
40 cnt += 1;
41 }
42 }
43
TEST_F(TestProfile,Test01)44 TEST_F(TestProfile, Test01) {
45 int step_cnt = 0;
46 Profile prof;
47 Profile* ptr_prof = &prof;
48 DumpTime::GetInstance().Record("Test01", GetTime(), true);
49 WITH(ptr_prof)[&ptr_prof, &step_cnt]()->void {
50 WITH(ptr_prof->Step("Step01"))[&step_cnt]()->void {
51 usleep(20);
52 step_cnt += 1;
53 };
54 WITH(ptr_prof->Step("Step02"))[&ptr_prof, &step_cnt]()->void {
55 usleep(10);
56 test_lap(ptr_prof);
57 step_cnt += 1;
58 };
59 };
60 DumpTime::GetInstance().Record("Test01", GetTime(), false);
61
62 prof.Print();
63
64 EXPECT_EQ(step_cnt, 2);
65 }
66
TEST_F(TestProfile,Test02)67 TEST_F(TestProfile, Test02) {
68 std::map<std::string, TimeStat> stat;
69 double t1 = GetTime();
70 usleep(20); // Step01.stage1
71 double t2 = GetTime();
72 usleep(30); // Step01.stage2
73 double t3 = GetTime();
74 usleep(10); // Step02.stage1
75 double t4 = GetTime();
76 usleep(10); // Step02.stage2
77 double t5 = GetTime();
78 usleep(10); // Step02.stage3
79 double t6 = GetTime();
80
81 MsProfile::StatTime("Step01.stage1", t2 - t1);
82 MsProfile::StatTime("Step01.stage2", t3 - t2);
83 MsProfile::StatTime("Step02.stage1", t4 - t3);
84 MsProfile::StatTime("Step02.stage2", t5 - t4);
85 MsProfile::StatTime("Step02.stage3", t6 - t5);
86
87 MsProfile::Print();
88 MsProfile::Reset();
89 EXPECT_GT(t6 - t1, 0);
90 }
91
92 } // namespace mindspore
93