mirror of
https://github.com/netbirdio/gvisor.git
synced 2026-05-22 17:12:49 -07:00
Introduce poll_benchmark
This benchmark performs similar measurements as epoll_benchmark. PiperOrigin-RevId: 571414062
This commit is contained in:
committed by
gVisor bot
parent
15cc3fcbbd
commit
07b9077bfd
+6
-1
@@ -1,5 +1,5 @@
|
||||
load("//tools:defs.bzl", "more_shards")
|
||||
load("//test/runner:defs.bzl", "syscall_test")
|
||||
load("//tools:defs.bzl", "more_shards")
|
||||
|
||||
package(
|
||||
default_applicable_licenses = ["//:license"],
|
||||
@@ -26,6 +26,11 @@ syscall_test(
|
||||
test = "//test/perf/linux:epoll_benchmark",
|
||||
)
|
||||
|
||||
syscall_test(
|
||||
debug = False,
|
||||
test = "//test/perf/linux:poll_benchmark",
|
||||
)
|
||||
|
||||
syscall_test(
|
||||
size = "large",
|
||||
debug = False,
|
||||
|
||||
+20
-1
@@ -260,10 +260,29 @@ cc_binary(
|
||||
gbenchmark,
|
||||
gtest,
|
||||
"//test/util:epoll_util",
|
||||
"//test/util:eventfd_util",
|
||||
"//test/util:file_descriptor",
|
||||
"//test/util:posix_error",
|
||||
"//test/util:test_main",
|
||||
"//test/util:test_util",
|
||||
"@com_google_absl//absl/time",
|
||||
],
|
||||
)
|
||||
|
||||
cc_binary(
|
||||
name = "poll_benchmark",
|
||||
testonly = 1,
|
||||
srcs = [
|
||||
"poll_benchmark.cc",
|
||||
],
|
||||
deps = [
|
||||
gbenchmark,
|
||||
gtest,
|
||||
"//test/util:eventfd_util",
|
||||
"//test/util:file_descriptor",
|
||||
"//test/util:posix_error",
|
||||
"//test/util:test_main",
|
||||
"//test/util:test_util",
|
||||
"//test/util:thread_util",
|
||||
"@com_google_absl//absl/time",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -13,38 +13,24 @@
|
||||
// limitations under the License.
|
||||
|
||||
#include <sys/epoll.h>
|
||||
#include <sys/eventfd.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <cerrno>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "gmock/gmock.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/time/time.h"
|
||||
#include "benchmark/benchmark.h"
|
||||
#include "test/util/epoll_util.h"
|
||||
#include "test/util/eventfd_util.h"
|
||||
#include "test/util/file_descriptor.h"
|
||||
#include "test/util/posix_error.h"
|
||||
#include "test/util/test_util.h"
|
||||
#include "test/util/thread_util.h"
|
||||
|
||||
namespace gvisor {
|
||||
namespace testing {
|
||||
|
||||
namespace {
|
||||
|
||||
// Returns a new eventfd.
|
||||
PosixErrorOr<FileDescriptor> NewEventFD() {
|
||||
int fd = eventfd(0, /* flags = */ 0);
|
||||
MaybeSave();
|
||||
if (fd < 0) {
|
||||
return PosixError(errno, "eventfd");
|
||||
}
|
||||
return FileDescriptor(fd);
|
||||
}
|
||||
|
||||
// Also stolen from epoll.cc unit tests.
|
||||
void BM_EpollTimeout(benchmark::State& state) {
|
||||
constexpr int kFDsPerEpoll = 3;
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
// Copyright 2023 The gVisor Authors.
|
||||
//
|
||||
// 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 <sys/poll.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "gmock/gmock.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "benchmark/benchmark.h"
|
||||
#include "test/util/eventfd_util.h"
|
||||
#include "test/util/file_descriptor.h"
|
||||
#include "test/util/posix_error.h"
|
||||
#include "test/util/test_util.h"
|
||||
|
||||
namespace gvisor {
|
||||
namespace testing {
|
||||
|
||||
namespace {
|
||||
|
||||
void Setup(int count, std::vector<FileDescriptor>& event_fds,
|
||||
std::vector<pollfd>& poll_fds) {
|
||||
for (int i = 0; i < count; ++i) {
|
||||
FileDescriptor fd = ASSERT_NO_ERRNO_AND_VALUE(NewEventFD());
|
||||
poll_fds.push_back(pollfd{.fd = fd.get(), .events = POLLIN});
|
||||
event_fds.push_back(std::move(fd));
|
||||
}
|
||||
}
|
||||
|
||||
void BM_PollTimeout(benchmark::State& state) {
|
||||
constexpr int kFDsPerPoll = 3;
|
||||
std::vector<FileDescriptor> event_fds;
|
||||
std::vector<pollfd> poll_fds;
|
||||
ASSERT_NO_FATAL_FAILURE(Setup(kFDsPerPoll, event_fds, poll_fds));
|
||||
|
||||
const int timeout_ms = state.range(0);
|
||||
for (auto _ : state) {
|
||||
EXPECT_EQ(poll(poll_fds.data(), poll_fds.size(), timeout_ms), 0);
|
||||
}
|
||||
}
|
||||
|
||||
BENCHMARK(BM_PollTimeout)->Range(/*start=*/0, /*limit=*/8);
|
||||
|
||||
void BM_PollAllEvents(benchmark::State& state) {
|
||||
const int fds_per_poll = state.range(0);
|
||||
std::vector<FileDescriptor> event_fds;
|
||||
std::vector<pollfd> poll_fds;
|
||||
ASSERT_NO_FATAL_FAILURE(Setup(fds_per_poll, event_fds, poll_fds));
|
||||
|
||||
constexpr uint64_t kEventVal = 5;
|
||||
for (const auto& eventfd : event_fds) {
|
||||
ASSERT_THAT(WriteFd(eventfd.get(), &kEventVal, sizeof(kEventVal)),
|
||||
SyscallSucceedsWithValue(sizeof(kEventVal)));
|
||||
}
|
||||
|
||||
constexpr int kTimeoutMs = 0;
|
||||
for (auto _ : state) {
|
||||
EXPECT_EQ(poll(poll_fds.data(), poll_fds.size(), kTimeoutMs), fds_per_poll);
|
||||
}
|
||||
}
|
||||
|
||||
BENCHMARK(BM_PollAllEvents)->Range(/*start=*/2, /*limit=*/1024);
|
||||
|
||||
} // namespace
|
||||
|
||||
} // namespace testing
|
||||
} // namespace gvisor
|
||||
Reference in New Issue
Block a user