gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/perf/linux/read_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 <fcntl.h>
    16  #include <stdlib.h>
    17  #include <sys/stat.h>
    18  #include <unistd.h>
    19  
    20  #include "gtest/gtest.h"
    21  #include "benchmark/benchmark.h"
    22  #include "test/util/fs_util.h"
    23  #include "test/util/logging.h"
    24  #include "test/util/temp_path.h"
    25  #include "test/util/test_util.h"
    26  
    27  namespace gvisor {
    28  namespace testing {
    29  
    30  namespace {
    31  
    32  void BM_Read(benchmark::State& state) {
    33    const int size = state.range(0);
    34    const std::string contents(size, 0);
    35    auto path = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith(
    36        GetAbsoluteTestTmpdir(), contents, TempPath::kDefaultFileMode));
    37    FileDescriptor fd = ASSERT_NO_ERRNO_AND_VALUE(Open(path.path(), O_RDONLY));
    38  
    39    std::vector<char> buf(size);
    40    for (auto _ : state) {
    41      TEST_CHECK(PreadFd(fd.get(), buf.data(), buf.size(), 0) == size);
    42    }
    43  
    44    state.SetBytesProcessed(static_cast<int64_t>(size) *
    45                            static_cast<int64_t>(state.iterations()));
    46  }
    47  
    48  BENCHMARK(BM_Read)->Range(1, 1 << 26)->UseRealTime();
    49  
    50  }  // namespace
    51  
    52  }  // namespace testing
    53  }  // namespace gvisor