log_write_bench.cc 2.8 KB

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