aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/contrib/lite/profiling/profile_buffer_test.cc
blob: b8784cca455cfc301f2cc30c9c6d031b7174f829 (plain)
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
/* 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 <string>
#include <vector>

#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "tensorflow/contrib/lite/profiling/profile_buffer.h"
#include "tensorflow/contrib/lite/testing/util.h"

namespace tflite {
namespace profiling {

namespace {

std::vector<const ProfileEvent*> GetProfileEvents(const ProfileBuffer& buffer) {
  std::vector<const ProfileEvent*> events;
  for (auto i = 0; i < buffer.Size(); i++) {
    events.push_back(buffer.At(i));
  }
  return events;
}

TEST(ProfileBufferTest, Empty) {
  ProfileBuffer buffer(/*max_size*/ 0, /*enabled*/ true);
  EXPECT_EQ(0, buffer.Size());
}

TEST(ProfileBufferTest, AddEvent) {
  ProfileBuffer buffer(/*max_size*/ 10, /*enabled*/ true);
  EXPECT_EQ(0, buffer.Size());
  auto event_handle = buffer.BeginEvent(
      "hello", ProfileEvent::EventType::DEFAULT, /* event_metadata */ 42);

  EXPECT_GE(event_handle, 0);
  EXPECT_EQ(1, buffer.Size());

  auto event = GetProfileEvents(buffer)[0];
  EXPECT_EQ(event->tag, "hello");
  EXPECT_GT(event->begin_timestamp_us, 0);
  EXPECT_EQ(event->event_type, ProfileEvent::EventType::DEFAULT);
  EXPECT_EQ(event->event_metadata, 42);

  buffer.EndEvent(event_handle);
  EXPECT_EQ(1, buffer.Size());
  EXPECT_GE(event->end_timestamp_us, event->begin_timestamp_us);
}

TEST(ProfileBufferTest, OverFlow) {
  const int max_size = 4;
  ProfileBuffer buffer{max_size, true};
  std::vector<std::string> eventNames = {"first", "second", "third", "fourth"};
  for (int i = 0; i < 2 * max_size; i++) {
    buffer.BeginEvent(eventNames[i % 4].c_str(),
                      ProfileEvent::EventType::DEFAULT, i);
    size_t expected_size = std::min(i + 1, max_size);
    EXPECT_EQ(expected_size, buffer.Size());
  }
  EXPECT_EQ(max_size, buffer.Size());
  for (int j = 0; j < buffer.Size(); ++j) {
    auto event = buffer.At(j);
    EXPECT_EQ(eventNames[j % 4], event->tag);
    EXPECT_EQ(ProfileEvent::EventType::DEFAULT, event->event_type);
    EXPECT_EQ(4 + j, event->event_metadata);
  }
}

TEST(ProfileBufferTest, Enable) {
  ProfileBuffer buffer(/*max_size*/ 10, /*enabled*/ false);
  EXPECT_EQ(0, buffer.Size());
  auto event_handle = buffer.BeginEvent(
      "hello", ProfileEvent::EventType::DEFAULT, /* event_metadata */ 42);
  EXPECT_EQ(kInvalidEventHandle, event_handle);
  EXPECT_EQ(0, buffer.Size());
  buffer.SetEnabled(true);
  event_handle = buffer.BeginEvent("hello", ProfileEvent::EventType::DEFAULT,
                                   /* event_metadata */ 42);
  EXPECT_GE(event_handle, 0);
  EXPECT_EQ(1, buffer.Size());
}

}  // namespace
}  // namespace profiling
}  // namespace tflite

int main(int argc, char** argv) {
  ::tflite::LogToStderr();
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}