profiler_test.cc 3.9 KB
Newer Older
D
dangqingqing 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

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 "paddle/platform/profiler.h"
#include "gtest/gtest.h"

TEST(Event, CpuElapsedTime) {
  using paddle::platform::Event;
  using paddle::platform::EventKind;

D
dangqingqing 已提交
22
  Event start_event(EventKind::kPushRange, "test", 0, nullptr);
D
dangqingqing 已提交
23 24 25 26 27
  EXPECT_TRUE(start_event.has_cuda() == false);
  int counter = 0;
  while (counter != 1000) {
    counter++;
  }
D
dangqingqing 已提交
28
  Event stop_event(EventKind::kPopRange, "test", 0, nullptr);
D
dangqingqing 已提交
29 30 31 32 33 34 35
  EXPECT_GT(start_event.CpuElapsedUs(stop_event), 0);
}

#ifdef PADDLE_WITH_CUDA
TEST(Event, CudaElapsedTime) {
  using paddle::platform::DeviceContext;
  using paddle::platform::CUDADeviceContext;
D
dangqingqing 已提交
36
  using paddle::platform::CUDAPlace;
D
dangqingqing 已提交
37 38 39
  using paddle::platform::Event;
  using paddle::platform::EventKind;

D
dangqingqing 已提交
40
  DeviceContext* dev_ctx = new CUDADeviceContext(CUDAPlace(0));
D
dangqingqing 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
  Event start_event(EventKind::kPushRange, "test", 0, dev_ctx);
  EXPECT_TRUE(start_event.has_cuda() == true);
  int counter = 0;
  while (counter != 1000) {
    counter++;
  }
  Event stop_event(EventKind::kPopRange, "test", 0, dev_ctx);
  EXPECT_GT(start_event.CudaElapsedUs(stop_event), 0);
}
#endif

TEST(RecordEvent, RecordEvent) {
  using paddle::platform::DeviceContext;
  using paddle::platform::Event;
  using paddle::platform::EventKind;
  using paddle::platform::RecordEvent;
  using paddle::platform::ProfilerState;
58
  using paddle::platform::EventSortingKey;
D
dangqingqing 已提交
59 60 61 62

  ProfilerState state = ProfilerState::kCPU;
  DeviceContext* dev_ctx = nullptr;
#ifdef PADDLE_WITH_CUDA
63
  using paddle::platform::CUDADeviceContext;
D
dangqingqing 已提交
64
  using paddle::platform::CUDAPlace;
D
dangqingqing 已提交
65 66
  state = ProfilerState::kCUDA;
  dev_ctx =
D
dangqingqing 已提交
67
      new paddle::platform::CUDADeviceContext(paddle::platform::CUDAPlace(0));
D
dangqingqing 已提交
68 69 70
#endif
  EnableProfiler(state);

71 72 73
  /* Usage 1:
  *  PushEvent(evt_name, dev_ctx);
  *  ...
74
  *  code to analyze
75 76 77
  *  ...
  * PopEvent(evt_name, dev_ctx);
  */
78 79 80 81 82 83 84 85
  for (int loop = 0; loop < 3; ++loop) {
    for (int i = 1; i < 5; ++i) {
      std::string name = "op_" + std::to_string(i);
      PushEvent(name, dev_ctx);
      int counter = 1;
      while (counter != i * 1000) counter++;
      PopEvent(name, dev_ctx);
    }
86 87 88 89 90 91
  }

  /* Usage 2:
   * {
   *   RecordEvent record_event(name, dev_ctx);
   *   ...
92 93
   *   code to analyze
   *   ...
94 95 96 97
   * }
   */
  for (int i = 1; i < 5; ++i) {
    std::string name = "evs_op_" + std::to_string(i);
D
dangqingqing 已提交
98
    RecordEvent record_event(name, dev_ctx);
99 100 101
    int counter = 1;
    while (counter != i * 1000) counter++;
  }
D
dangqingqing 已提交
102
  std::vector<std::vector<Event>> events = paddle::platform::DisableProfiler();
103 104 105
  // Will remove from test before merging
  ParseEvents(events, EventSortingKey::kTotal);

D
dangqingqing 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
  int cuda_startup_count = 0;
  int start_profiler_count = 0;
  int stop_profiler_count = 0;
  for (size_t i = 0; i < events.size(); ++i) {
    for (size_t j = 0; j < events[i].size(); ++j) {
      if (events[i][j].name() == "_cuda_startup_") ++cuda_startup_count;
      if (events[i][j].name() == "_start_profiler_") ++start_profiler_count;
      if (events[i][j].name() == "_stop_profiler_") ++stop_profiler_count;
      if (events[i][j].name() == "push") {
        EXPECT_EQ(events[i][j + 1].name(), "pop");
#ifdef PADDLE_WITH_CUDA
        EXPECT_GT(events[i][j].CudaElapsedUs(events[i][j + 1]), 0);
#else
        EXPECT_GT(events[i][j].CpuElapsedUs(events[i][j + 1]), 0);
#endif
      }
    }
  }
  EXPECT_EQ(cuda_startup_count % 5, 0);
  EXPECT_EQ(start_profiler_count, 1);
  EXPECT_EQ(stop_profiler_count, 1);
}