You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

profile_test.cc 2.4 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. #ifndef ENABLE_PROFILE
  20. #define ENABLE_PROFILE
  21. #endif
  22. #include "utils/profile.h"
  23. namespace mindspore {
  24. class TestProfile : public UT::Common {
  25. public:
  26. TestProfile() {}
  27. virtual ~TestProfile() {}
  28. virtual void TearDown() {}
  29. };
  30. static void test_lap(Profile* prof) {
  31. int nums[] = {30, 20, 70};
  32. int cnt = 0;
  33. for (auto elem : nums) {
  34. WITH(prof->Lap(cnt))[elem]()->void { usleep(elem); };
  35. cnt += 1;
  36. }
  37. }
  38. TEST_F(TestProfile, Test01) {
  39. int step_cnt = 0;
  40. Profile prof;
  41. Profile* ptr_prof = &prof;
  42. DumpTime::GetInstance().Record("Test01", GetTime(), true);
  43. WITH(ptr_prof)[&ptr_prof, &step_cnt]()->void {
  44. WITH(ptr_prof->Step("Step01"))[&step_cnt]()->void {
  45. usleep(20);
  46. step_cnt += 1;
  47. };
  48. WITH(ptr_prof->Step("Step02"))[&ptr_prof, &step_cnt]()->void {
  49. usleep(10);
  50. test_lap(ptr_prof);
  51. step_cnt += 1;
  52. };
  53. };
  54. DumpTime::GetInstance().Record("Test01", GetTime(), false);
  55. prof.Print();
  56. EXPECT_EQ(step_cnt, 2);
  57. }
  58. TEST_F(TestProfile, Test02) {
  59. std::map<std::string, TimeStat> stat;
  60. double t1 = GetTime();
  61. usleep(20); // Step01.stage1
  62. double t2 = GetTime();
  63. usleep(30); // Step01.stage2
  64. double t3 = GetTime();
  65. usleep(10); // Step02.stage1
  66. double t4 = GetTime();
  67. usleep(10); // Step02.stage2
  68. double t5 = GetTime();
  69. usleep(10); // Step02.stage3
  70. double t6 = GetTime();
  71. MsProfile::StatTime("Step01.stage1", t2 - t1);
  72. MsProfile::StatTime("Step01.stage2", t3 - t2);
  73. MsProfile::StatTime("Step02.stage1", t4 - t3);
  74. MsProfile::StatTime("Step02.stage2", t5 - t4);
  75. MsProfile::StatTime("Step02.stage3", t6 - t5);
  76. MsProfile::Print();
  77. MsProfile::Reset();
  78. EXPECT_GT(t6 - t1, 0);
  79. }
  80. } // namespace mindspore