1 /* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
2
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 #include "tensorflow/lite/profiling/profile_buffer.h"
16
17 #include <cstdint>
18 #include <string>
19 #include <vector>
20
21 #include <gmock/gmock.h>
22 #include <gtest/gtest.h>
23
24 namespace tflite {
25 namespace profiling {
26
27 namespace {
28
GetProfileEvents(const ProfileBuffer & buffer)29 std::vector<const ProfileEvent*> GetProfileEvents(const ProfileBuffer& buffer) {
30 std::vector<const ProfileEvent*> events;
31 for (size_t i = 0; i < buffer.Size(); i++) {
32 events.push_back(buffer.At(i));
33 }
34 return events;
35 }
36
TEST(ProfileBufferTest,Empty)37 TEST(ProfileBufferTest, Empty) {
38 ProfileBuffer buffer(/*max_size*/ 0, /*enabled*/ true);
39 EXPECT_EQ(0, buffer.Size());
40 }
41
TEST(ProfileBufferTest,AddEvent)42 TEST(ProfileBufferTest, AddEvent) {
43 ProfileBuffer buffer(/*max_size*/ 10, /*enabled*/ true);
44 EXPECT_EQ(0, buffer.Size());
45 auto event_handle =
46 buffer.BeginEvent("hello", ProfileEvent::EventType::DEFAULT,
47 /*event_metadata1*/ 42, /*event_metadata2*/ 0);
48
49 EXPECT_GE(event_handle, 0);
50 EXPECT_EQ(1, buffer.Size());
51
52 auto event = GetProfileEvents(buffer)[0];
53 EXPECT_EQ(event->tag, "hello");
54 EXPECT_GT(event->begin_timestamp_us, 0);
55 EXPECT_EQ(event->event_type, ProfileEvent::EventType::DEFAULT);
56 EXPECT_EQ(event->event_metadata, 42);
57
58 buffer.EndEvent(event_handle);
59 EXPECT_EQ(1, buffer.Size());
60 EXPECT_GE(event->end_timestamp_us, event->begin_timestamp_us);
61 }
62
TEST(ProfileBufferTest,EndEventWithMetadata)63 TEST(ProfileBufferTest, EndEventWithMetadata) {
64 ProfileBuffer buffer(/*max_size*/ 10, /*enabled*/ true);
65 EXPECT_EQ(0, buffer.Size());
66 auto event_handle =
67 buffer.BeginEvent("hello", ProfileEvent::EventType::DEFAULT,
68 /*event_metadata1*/ 42, /*event_metadata2*/ 0);
69 const int64_t kEventMetadata1 = 18;
70 const int64_t kEventMetadata2 = 36;
71 buffer.EndEvent(event_handle, &kEventMetadata1, &kEventMetadata2);
72
73 EXPECT_GE(event_handle, 0);
74 EXPECT_EQ(1, buffer.Size());
75 auto event = GetProfileEvents(buffer)[0];
76 EXPECT_EQ(event->tag, "hello");
77 EXPECT_GT(event->begin_timestamp_us, 0);
78 EXPECT_EQ(event->event_type, ProfileEvent::EventType::DEFAULT);
79 EXPECT_EQ(event->event_metadata, kEventMetadata1);
80 EXPECT_EQ(event->extra_event_metadata, kEventMetadata2);
81 EXPECT_EQ(1, buffer.Size());
82 EXPECT_GE(event->end_timestamp_us, event->begin_timestamp_us);
83 }
84
TEST(ProfileBufferTest,OverFlow)85 TEST(ProfileBufferTest, OverFlow) {
86 const int max_size = 4;
87 ProfileBuffer buffer{max_size, true};
88 std::vector<std::string> eventNames = {"first", "second", "third", "fourth"};
89 for (int i = 0; i < 2 * max_size; i++) {
90 buffer.BeginEvent(eventNames[i % 4].c_str(),
91 ProfileEvent::EventType::DEFAULT, i, 0);
92 size_t expected_size = std::min(i + 1, max_size);
93 EXPECT_EQ(expected_size, buffer.Size());
94 }
95 EXPECT_EQ(max_size, buffer.Size());
96 for (size_t j = 0; j < buffer.Size(); ++j) {
97 auto event = buffer.At(j);
98 EXPECT_EQ(eventNames[j % 4], event->tag);
99 EXPECT_EQ(ProfileEvent::EventType::DEFAULT, event->event_type);
100 EXPECT_EQ(j, event->event_metadata);
101 }
102 }
103
TEST(ProfileBufferTest,Enable)104 TEST(ProfileBufferTest, Enable) {
105 ProfileBuffer buffer(/*max_size*/ 10, /*enabled*/ false);
106 EXPECT_EQ(0, buffer.Size());
107 auto event_handle =
108 buffer.BeginEvent("hello", ProfileEvent::EventType::DEFAULT,
109 /*event_metadata1*/ 42, /*event_metadata2*/ 0);
110 EXPECT_EQ(kInvalidEventHandle, event_handle);
111 EXPECT_EQ(0, buffer.Size());
112 buffer.SetEnabled(true);
113 event_handle =
114 buffer.BeginEvent("hello", ProfileEvent::EventType::DEFAULT,
115 /*event_metadata1*/ 42, /*event_metadata2*/ 0);
116 EXPECT_GE(event_handle, 0);
117 EXPECT_EQ(1, buffer.Size());
118 }
119
120 } // namespace
121 } // namespace profiling
122 } // namespace tflite
123