log_write_bench.cc 2.7 KB

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