]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/util/log_write_bench.cc
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / rocksdb / util / log_write_bench.cc
1 // Copyright (c) 2011-present, 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 #ifndef GFLAGS
7 #include <cstdio>
8 int main() {
9 fprintf(stderr, "Please install gflags to run rocksdb tools\n");
10 return 1;
11 }
12 #else
13
14 #include <gflags/gflags.h>
15
16 #include "monitoring/histogram.h"
17 #include "rocksdb/env.h"
18 #include "util/file_reader_writer.h"
19 #include "util/testharness.h"
20 #include "util/testutil.h"
21
22 using GFLAGS::ParseCommandLineFlags;
23 using GFLAGS::SetUsageMessage;
24
25 // A simple benchmark to simulate transactional logs
26
27 DEFINE_int32(num_records, 6000, "Number of records.");
28 DEFINE_int32(record_size, 249, "Size of each record.");
29 DEFINE_int32(record_interval, 10000, "Interval between records (microSec)");
30 DEFINE_int32(bytes_per_sync, 0, "bytes_per_sync parameter in EnvOptions");
31 DEFINE_bool(enable_sync, false, "sync after each write.");
32
33 namespace rocksdb {
34 void RunBenchmark() {
35 std::string file_name = test::TmpDir() + "/log_write_benchmark.log";
36 Env* env = Env::Default();
37 EnvOptions env_options = env->OptimizeForLogWrite(EnvOptions());
38 env_options.bytes_per_sync = FLAGS_bytes_per_sync;
39 unique_ptr<WritableFile> file;
40 env->NewWritableFile(file_name, &file, env_options);
41 unique_ptr<WritableFileWriter> writer;
42 writer.reset(new WritableFileWriter(std::move(file), env_options));
43
44 std::string record;
45 record.assign(FLAGS_record_size, 'X');
46
47 HistogramImpl hist;
48
49 uint64_t start_time = env->NowMicros();
50 for (int i = 0; i < FLAGS_num_records; i++) {
51 uint64_t start_nanos = env->NowNanos();
52 writer->Append(record);
53 writer->Flush();
54 if (FLAGS_enable_sync) {
55 writer->Sync(false);
56 }
57 hist.Add(env->NowNanos() - start_nanos);
58
59 if (i % 1000 == 1) {
60 fprintf(stderr, "Wrote %d records...\n", i);
61 }
62
63 int time_to_sleep =
64 (i + 1) * FLAGS_record_interval - (env->NowMicros() - start_time);
65 if (time_to_sleep > 0) {
66 env->SleepForMicroseconds(time_to_sleep);
67 }
68 }
69
70 fprintf(stderr, "Distribution of latency of append+flush: \n%s",
71 hist.ToString().c_str());
72 }
73 } // namespace rocksdb
74
75 int main(int argc, char** argv) {
76 SetUsageMessage(std::string("\nUSAGE:\n") + std::string(argv[0]) +
77 " [OPTIONS]...");
78 ParseCommandLineFlags(&argc, &argv, true);
79
80 rocksdb::RunBenchmark();
81 return 0;
82 }
83
84 #endif // GFLAGS