gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/perf/linux/epoll_benchmark.cc (about)

     1  // Copyright 2020 The gVisor Authors.
     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 <sys/epoll.h>
    16  
    17  #include <cstdint>
    18  #include <vector>
    19  
    20  #include "gmock/gmock.h"
    21  #include "gtest/gtest.h"
    22  #include "benchmark/benchmark.h"
    23  #include "test/util/epoll_util.h"
    24  #include "test/util/eventfd_util.h"
    25  #include "test/util/file_descriptor.h"
    26  #include "test/util/posix_error.h"
    27  #include "test/util/test_util.h"
    28  
    29  namespace gvisor {
    30  namespace testing {
    31  
    32  namespace {
    33  
    34  // Also stolen from epoll.cc unit tests.
    35  void BM_EpollTimeout(benchmark::State& state) {
    36    constexpr int kFDsPerEpoll = 3;
    37    auto epollfd = ASSERT_NO_ERRNO_AND_VALUE(NewEpollFD());
    38  
    39    std::vector<FileDescriptor> eventfds;
    40    for (int i = 0; i < kFDsPerEpoll; i++) {
    41      eventfds.push_back(ASSERT_NO_ERRNO_AND_VALUE(NewEventFD()));
    42      ASSERT_NO_ERRNO(
    43          RegisterEpollFD(epollfd.get(), eventfds[i].get(), EPOLLIN, 0));
    44    }
    45  
    46    struct epoll_event result[kFDsPerEpoll];
    47    int timeout_ms = state.range(0);
    48  
    49    for (auto _ : state) {
    50      EXPECT_EQ(0, epoll_wait(epollfd.get(), result, kFDsPerEpoll, timeout_ms));
    51    }
    52  }
    53  
    54  BENCHMARK(BM_EpollTimeout)->Range(0, 8);
    55  
    56  // Also stolen from epoll.cc unit tests.
    57  void BM_EpollAllEvents(benchmark::State& state) {
    58    auto epollfd = ASSERT_NO_ERRNO_AND_VALUE(NewEpollFD());
    59    const int fds_per_epoll = state.range(0);
    60    constexpr uint64_t kEventVal = 5;
    61  
    62    std::vector<FileDescriptor> eventfds;
    63    for (int i = 0; i < fds_per_epoll; i++) {
    64      eventfds.push_back(ASSERT_NO_ERRNO_AND_VALUE(NewEventFD()));
    65      ASSERT_NO_ERRNO(
    66          RegisterEpollFD(epollfd.get(), eventfds[i].get(), EPOLLIN, 0));
    67  
    68      ASSERT_THAT(WriteFd(eventfds[i].get(), &kEventVal, sizeof(kEventVal)),
    69                  SyscallSucceedsWithValue(sizeof(kEventVal)));
    70    }
    71  
    72    std::vector<struct epoll_event> result(fds_per_epoll);
    73  
    74    for (auto _ : state) {
    75      EXPECT_EQ(fds_per_epoll,
    76                epoll_wait(epollfd.get(), result.data(), fds_per_epoll, 0));
    77    }
    78  }
    79  
    80  BENCHMARK(BM_EpollAllEvents)->Range(2, 1024);
    81  
    82  }  // namespace
    83  
    84  }  // namespace testing
    85  }  // namespace gvisor