gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/perf/linux/dup_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 <unistd.h>
    18  
    19  #include <memory>
    20  #include <string>
    21  #include <vector>
    22  
    23  #include "gtest/gtest.h"
    24  #include "benchmark/benchmark.h"
    25  #include "test/util/fs_util.h"
    26  #include "test/util/logging.h"
    27  #include "test/util/temp_path.h"
    28  
    29  namespace gvisor {
    30  
    31  namespace {
    32  
    33  void BM_Dup(benchmark::State& state) {
    34    const int size = state.range(0);
    35  
    36    for (auto _ : state) {
    37      std::vector<int> v;
    38      for (int i = 0; i < size; i++) {
    39        int fd = dup(2);
    40        TEST_CHECK(fd != -1);
    41        v.push_back(fd);
    42      }
    43      for (int i = 0; i < size; i++) {
    44        int fd = v[i];
    45        close(fd);
    46      }
    47    }
    48    state.SetItemsProcessed(state.iterations() * size);
    49  }
    50  
    51  BENCHMARK(BM_Dup)->Range(1, 1 << 15)->UseRealTime();
    52  
    53  }  // namespace
    54  
    55  }  // namespace gvisor