github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/perf/linux/unlink_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/stat.h>
    16  #include <sys/types.h>
    17  #include <unistd.h>
    18  
    19  #include "gtest/gtest.h"
    20  #include "benchmark/benchmark.h"
    21  #include "test/util/fs_util.h"
    22  #include "test/util/temp_path.h"
    23  #include "test/util/test_util.h"
    24  
    25  namespace gvisor {
    26  namespace testing {
    27  
    28  namespace {
    29  
    30  // Creates a directory containing `files` files, and unlinks all the files.
    31  void BM_Unlink(benchmark::State& state) {
    32    // Create directory with given files.
    33    const int file_count = state.range(0);
    34  
    35    // We unlink all files on each iteration, but report this as a "batch"
    36    // iteration so that reported times are per file.
    37    TempPath dir;
    38    while (state.KeepRunningBatch(file_count)) {
    39      state.PauseTiming();
    40      // N.B. dir is declared outside the loop so that destruction of the previous
    41      // iteration's directory occurs here, inside of PauseTiming.
    42      dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
    43  
    44      std::vector<TempPath> files;
    45      for (int i = 0; i < file_count; i++) {
    46        TempPath file =
    47            ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileIn(dir.path()));
    48        files.push_back(std::move(file));
    49      }
    50      state.ResumeTiming();
    51  
    52      while (!files.empty()) {
    53        // Destructor unlinks.
    54        files.pop_back();
    55      }
    56    }
    57  
    58    state.SetItemsProcessed(state.iterations());
    59  }
    60  
    61  BENCHMARK(BM_Unlink)->Range(1, 100 * 1000)->UseRealTime();
    62  
    63  }  // namespace
    64  
    65  }  // namespace testing
    66  }  // namespace gvisor