tzh
2024-08-22 c7d0944258c7d0943aa7b2211498fd612971ce27
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
 
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 <chrono>  // NOLINT(build/c++11)
#include <cmath>
#include <thread>  // NOLINT(build/c++11)
 
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "tensorflow/lite/profiling/profiler.h"
#include "tensorflow/lite/testing/util.h"
 
namespace tflite {
namespace profiling {
namespace {
 
double GetDurationOfEventMs(const ProfileEvent* event) {
  return (event->end_timestamp_us - event->begin_timestamp_us) / 1e3;
}
 
void SleepForQuarterSecond(Profiler* profiler) {
  ScopedProfile profile(profiler, "SleepForQuarter");
  std::this_thread::sleep_for(std::chrono::milliseconds(250));
}
 
void ChildFunction(Profiler* profiler) {
  ScopedProfile profile(profiler, "Child");
  SleepForQuarterSecond(profiler);
}
 
void ParentFunction(Profiler* profiler) {
  ScopedProfile profile(profiler, "Parent");
  for (int i = 0; i < 2; i++) {
    ChildFunction(profiler);
  }
}
 
TEST(ProfilerTest, NoProfilesAreCollectedWhenDisabled) {
  Profiler profiler;
  ParentFunction(&profiler);
  auto profile_events = profiler.GetProfileEvents();
  EXPECT_EQ(0, profile_events.size());
}
 
TEST(ProfilingTest, ProfilesAreCollected) {
  Profiler profiler;
  profiler.StartProfiling();
  ParentFunction(&profiler);
  profiler.StopProfiling();
  auto profile_events = profiler.GetProfileEvents();
  // ParentFunction calls the ChildFunction 2 times.
  // Each ChildFunction calls SleepForQuarterSecond once.
  // We expect 1 entry for ParentFunction, 2 for ChildFunction and 2 for
  // SleepForQuarterSecond: Total: 1+ 2 + 2 = 5
  //  Profiles should look like:
  //  Parent ~ 500 ms (due to 2 Child calls)
  //   - Child ~ 250 ms (due to SleepForQuarter calls)
  //       - SleepForQuarter ~ 250ms
  //   - Child ~ 250 ms (due to SleepForQuarter calls)
  //      - SleepForQuarter ~ 250ms
  //
  ASSERT_EQ(5, profile_events.size());
  EXPECT_EQ("Parent", profile_events[0]->tag);
  EXPECT_EQ("Child", profile_events[1]->tag);
  EXPECT_EQ("SleepForQuarter", profile_events[2]->tag);
  EXPECT_EQ("Child", profile_events[3]->tag);
  EXPECT_EQ("SleepForQuarter", profile_events[4]->tag);
 
#ifndef ADDRESS_SANITIZER
  // ASAN build is sometimes very slow. Set a large epsilon to avoid flakiness.
  // Due to flakiness, just verify relative values match.
  const int eps_ms = 50;
  auto parent_ms = GetDurationOfEventMs(profile_events[0]);
  double child_ms[2], sleep_for_quarter_ms[2];
  child_ms[0] = GetDurationOfEventMs(profile_events[1]);
  child_ms[1] = GetDurationOfEventMs(profile_events[3]);
  sleep_for_quarter_ms[0] = GetDurationOfEventMs(profile_events[2]);
  sleep_for_quarter_ms[1] = GetDurationOfEventMs(profile_events[4]);
  EXPECT_NEAR(parent_ms, child_ms[0] + child_ms[1], eps_ms);
  EXPECT_NEAR(child_ms[0], sleep_for_quarter_ms[0], eps_ms);
  EXPECT_NEAR(child_ms[1], sleep_for_quarter_ms[1], eps_ms);
#endif
}
 
TEST(ProfilingTest, NullProfiler) {
  Profiler* profiler = nullptr;
  { SCOPED_OPERATOR_PROFILE(profiler, 1); }
}
 
TEST(ProfilingTest, ScopedProfile) {
  Profiler profiler;
  profiler.StartProfiling();
  { SCOPED_OPERATOR_PROFILE(&profiler, 1); }
  profiler.StopProfiling();
  auto profile_events = profiler.GetProfileEvents();
  EXPECT_EQ(1, profile_events.size());
}
 
}  // namespace
}  // namespace profiling
}  // namespace tflite
 
int main(int argc, char** argv) {
  ::tflite::LogToStderr();
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}