|
| 1 | +// Copyright (c) 2013, Facebook, Inc. All rights reserved. |
| 2 | +// This source code is licensed under the BSD-style license found in the |
| 3 | +// LICENSE file in the root directory of this source tree. An additional grant |
| 4 | +// of patent rights can be found in the PATENTS file in the same directory. |
| 5 | + |
| 6 | +#include <gflags/gflags.h> |
| 7 | + |
| 8 | +#include "rocksdb/env.h" |
| 9 | +#include "util/histogram.h" |
| 10 | +#include "util/testharness.h" |
| 11 | +#include "util/testutil.h" |
| 12 | + |
| 13 | +// A simple benchmark to simulate transactional logs |
| 14 | + |
| 15 | +DEFINE_int32(num_records, 6000, "Size of each record."); |
| 16 | +DEFINE_int32(record_size, 249, "Size of each record."); |
| 17 | +DEFINE_int32(record_interval, 10000, "Interval between records (microSec)"); |
| 18 | +DEFINE_int32(bytes_per_sync, 0, "bytes_per_sync parameter in EnvOptions"); |
| 19 | +DEFINE_bool(enable_sync, false, "sync after each write."); |
| 20 | + |
| 21 | +namespace rocksdb { |
| 22 | +void RunBenchmark() { |
| 23 | + std::string file_name = test::TmpDir() + "/log_write_bench.log"; |
| 24 | + Env* env = Env::Default(); |
| 25 | + EnvOptions env_options; |
| 26 | + env_options.use_mmap_writes = false; |
| 27 | + env_options.bytes_per_sync = FLAGS_bytes_per_sync; |
| 28 | + unique_ptr<WritableFile> file; |
| 29 | + env->NewWritableFile(file_name, &file, env_options); |
| 30 | + |
| 31 | + std::string record; |
| 32 | + record.assign('X', FLAGS_record_size); |
| 33 | + |
| 34 | + HistogramImpl hist; |
| 35 | + |
| 36 | + uint64_t start_time = env->NowMicros(); |
| 37 | + for (int i = 0; i < FLAGS_num_records; i++) { |
| 38 | + uint64_t start_nanos = env->NowNanos(); |
| 39 | + file->Append(record); |
| 40 | + file->Flush(); |
| 41 | + if (FLAGS_enable_sync) { |
| 42 | + file->Sync(); |
| 43 | + } |
| 44 | + hist.Add(env->NowNanos() - start_nanos); |
| 45 | + |
| 46 | + if (i % 1000 == 1) { |
| 47 | + fprintf(stderr, "Wrote %d records...\n", i); |
| 48 | + } |
| 49 | + |
| 50 | + int time_to_sleep = |
| 51 | + (i + 1) * FLAGS_record_interval - (env->NowMicros() - start_time); |
| 52 | + if (time_to_sleep > 0) { |
| 53 | + env->SleepForMicroseconds(time_to_sleep); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + fprintf(stderr, "Distribution of latency of append+flush: \n%s", |
| 58 | + hist.ToString().c_str()); |
| 59 | +} |
| 60 | +} // namespace rocksdb |
| 61 | + |
| 62 | +int main(int argc, char** argv) { |
| 63 | + google::SetUsageMessage(std::string("\nUSAGE:\n") + std::string(argv[0]) + |
| 64 | + " [OPTIONS]..."); |
| 65 | + google::ParseCommandLineFlags(&argc, &argv, true); |
| 66 | + |
| 67 | + rocksdb::RunBenchmark(); |
| 68 | + return 0; |
| 69 | +} |
0 commit comments