db_statistics_test.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. #include <string>
  6. #include "db/db_test_util.h"
  7. #include "monitoring/thread_status_util.h"
  8. #include "port/stack_trace.h"
  9. #include "rocksdb/statistics.h"
  10. namespace ROCKSDB_NAMESPACE {
  11. class DBStatisticsTest : public DBTestBase {
  12. public:
  13. DBStatisticsTest() : DBTestBase("/db_statistics_test") {}
  14. };
  15. TEST_F(DBStatisticsTest, CompressionStatsTest) {
  16. CompressionType type;
  17. if (Snappy_Supported()) {
  18. type = kSnappyCompression;
  19. fprintf(stderr, "using snappy\n");
  20. } else if (Zlib_Supported()) {
  21. type = kZlibCompression;
  22. fprintf(stderr, "using zlib\n");
  23. } else if (BZip2_Supported()) {
  24. type = kBZip2Compression;
  25. fprintf(stderr, "using bzip2\n");
  26. } else if (LZ4_Supported()) {
  27. type = kLZ4Compression;
  28. fprintf(stderr, "using lz4\n");
  29. } else if (XPRESS_Supported()) {
  30. type = kXpressCompression;
  31. fprintf(stderr, "using xpress\n");
  32. } else if (ZSTD_Supported()) {
  33. type = kZSTD;
  34. fprintf(stderr, "using ZSTD\n");
  35. } else {
  36. fprintf(stderr, "skipping test, compression disabled\n");
  37. return;
  38. }
  39. Options options = CurrentOptions();
  40. options.compression = type;
  41. options.statistics = ROCKSDB_NAMESPACE::CreateDBStatistics();
  42. options.statistics->set_stats_level(StatsLevel::kExceptTimeForMutex);
  43. DestroyAndReopen(options);
  44. int kNumKeysWritten = 100000;
  45. // Check that compressions occur and are counted when compression is turned on
  46. Random rnd(301);
  47. for (int i = 0; i < kNumKeysWritten; ++i) {
  48. // compressible string
  49. ASSERT_OK(Put(Key(i), RandomString(&rnd, 128) + std::string(128, 'a')));
  50. }
  51. ASSERT_OK(Flush());
  52. ASSERT_GT(options.statistics->getTickerCount(NUMBER_BLOCK_COMPRESSED), 0);
  53. for (int i = 0; i < kNumKeysWritten; ++i) {
  54. auto r = Get(Key(i));
  55. }
  56. ASSERT_GT(options.statistics->getTickerCount(NUMBER_BLOCK_DECOMPRESSED), 0);
  57. options.compression = kNoCompression;
  58. DestroyAndReopen(options);
  59. uint64_t currentCompressions =
  60. options.statistics->getTickerCount(NUMBER_BLOCK_COMPRESSED);
  61. uint64_t currentDecompressions =
  62. options.statistics->getTickerCount(NUMBER_BLOCK_DECOMPRESSED);
  63. // Check that compressions do not occur when turned off
  64. for (int i = 0; i < kNumKeysWritten; ++i) {
  65. // compressible string
  66. ASSERT_OK(Put(Key(i), RandomString(&rnd, 128) + std::string(128, 'a')));
  67. }
  68. ASSERT_OK(Flush());
  69. ASSERT_EQ(options.statistics->getTickerCount(NUMBER_BLOCK_COMPRESSED)
  70. - currentCompressions, 0);
  71. for (int i = 0; i < kNumKeysWritten; ++i) {
  72. auto r = Get(Key(i));
  73. }
  74. ASSERT_EQ(options.statistics->getTickerCount(NUMBER_BLOCK_DECOMPRESSED)
  75. - currentDecompressions, 0);
  76. }
  77. TEST_F(DBStatisticsTest, MutexWaitStatsDisabledByDefault) {
  78. Options options = CurrentOptions();
  79. options.create_if_missing = true;
  80. options.statistics = ROCKSDB_NAMESPACE::CreateDBStatistics();
  81. CreateAndReopenWithCF({"pikachu"}, options);
  82. const uint64_t kMutexWaitDelay = 100;
  83. ThreadStatusUtil::TEST_SetStateDelay(ThreadStatus::STATE_MUTEX_WAIT,
  84. kMutexWaitDelay);
  85. ASSERT_OK(Put("hello", "rocksdb"));
  86. ASSERT_EQ(TestGetTickerCount(options, DB_MUTEX_WAIT_MICROS), 0);
  87. ThreadStatusUtil::TEST_SetStateDelay(ThreadStatus::STATE_MUTEX_WAIT, 0);
  88. }
  89. TEST_F(DBStatisticsTest, MutexWaitStats) {
  90. Options options = CurrentOptions();
  91. options.create_if_missing = true;
  92. options.statistics = ROCKSDB_NAMESPACE::CreateDBStatistics();
  93. options.statistics->set_stats_level(StatsLevel::kAll);
  94. CreateAndReopenWithCF({"pikachu"}, options);
  95. const uint64_t kMutexWaitDelay = 100;
  96. ThreadStatusUtil::TEST_SetStateDelay(ThreadStatus::STATE_MUTEX_WAIT,
  97. kMutexWaitDelay);
  98. ASSERT_OK(Put("hello", "rocksdb"));
  99. ASSERT_GE(TestGetTickerCount(options, DB_MUTEX_WAIT_MICROS), kMutexWaitDelay);
  100. ThreadStatusUtil::TEST_SetStateDelay(ThreadStatus::STATE_MUTEX_WAIT, 0);
  101. }
  102. TEST_F(DBStatisticsTest, ResetStats) {
  103. Options options = CurrentOptions();
  104. options.create_if_missing = true;
  105. options.statistics = ROCKSDB_NAMESPACE::CreateDBStatistics();
  106. DestroyAndReopen(options);
  107. for (int i = 0; i < 2; ++i) {
  108. // pick arbitrary ticker and histogram. On first iteration they're zero
  109. // because db is unused. On second iteration they're zero due to Reset().
  110. ASSERT_EQ(0, TestGetTickerCount(options, NUMBER_KEYS_WRITTEN));
  111. HistogramData histogram_data;
  112. options.statistics->histogramData(DB_WRITE, &histogram_data);
  113. ASSERT_EQ(0.0, histogram_data.max);
  114. if (i == 0) {
  115. // The Put() makes some of the ticker/histogram stats nonzero until we
  116. // Reset().
  117. ASSERT_OK(Put("hello", "rocksdb"));
  118. ASSERT_EQ(1, TestGetTickerCount(options, NUMBER_KEYS_WRITTEN));
  119. options.statistics->histogramData(DB_WRITE, &histogram_data);
  120. ASSERT_GT(histogram_data.max, 0.0);
  121. options.statistics->Reset();
  122. }
  123. }
  124. }
  125. } // namespace ROCKSDB_NAMESPACE
  126. int main(int argc, char** argv) {
  127. ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
  128. ::testing::InitGoogleTest(&argc, argv);
  129. return RUN_ALL_TESTS();
  130. }