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