2017-04-26 23:18:58 +02:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-16 01:03:42 +02:00
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
2017-04-26 23:18:58 +02:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "db/db_test_util.h"
|
|
|
|
#include "monitoring/thread_status_util.h"
|
|
|
|
#include "port/stack_trace.h"
|
|
|
|
#include "rocksdb/statistics.h"
|
2020-07-09 23:33:42 +02:00
|
|
|
#include "util/random.h"
|
2017-04-26 23:18:58 +02:00
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2017-04-26 23:18:58 +02:00
|
|
|
|
|
|
|
class DBStatisticsTest : public DBTestBase {
|
|
|
|
public:
|
2020-08-18 03:41:20 +02:00
|
|
|
DBStatisticsTest()
|
2021-07-23 17:37:27 +02:00
|
|
|
: DBTestBase("db_statistics_test", /*env_do_fsync=*/true) {}
|
2017-04-26 23:18:58 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(DBStatisticsTest, CompressionStatsTest) {
|
|
|
|
CompressionType type;
|
|
|
|
|
|
|
|
if (Snappy_Supported()) {
|
|
|
|
type = kSnappyCompression;
|
|
|
|
fprintf(stderr, "using snappy\n");
|
|
|
|
} else if (Zlib_Supported()) {
|
|
|
|
type = kZlibCompression;
|
|
|
|
fprintf(stderr, "using zlib\n");
|
|
|
|
} else if (BZip2_Supported()) {
|
|
|
|
type = kBZip2Compression;
|
|
|
|
fprintf(stderr, "using bzip2\n");
|
|
|
|
} else if (LZ4_Supported()) {
|
|
|
|
type = kLZ4Compression;
|
|
|
|
fprintf(stderr, "using lz4\n");
|
|
|
|
} else if (XPRESS_Supported()) {
|
|
|
|
type = kXpressCompression;
|
|
|
|
fprintf(stderr, "using xpress\n");
|
|
|
|
} else if (ZSTD_Supported()) {
|
|
|
|
type = kZSTD;
|
|
|
|
fprintf(stderr, "using ZSTD\n");
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "skipping test, compression disabled\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Options options = CurrentOptions();
|
|
|
|
options.compression = type;
|
2020-02-20 21:07:53 +01:00
|
|
|
options.statistics = ROCKSDB_NAMESPACE::CreateDBStatistics();
|
2019-03-01 19:39:00 +01:00
|
|
|
options.statistics->set_stats_level(StatsLevel::kExceptTimeForMutex);
|
2017-04-26 23:18:58 +02:00
|
|
|
DestroyAndReopen(options);
|
|
|
|
|
|
|
|
int kNumKeysWritten = 100000;
|
|
|
|
|
|
|
|
// Check that compressions occur and are counted when compression is turned on
|
|
|
|
Random rnd(301);
|
|
|
|
for (int i = 0; i < kNumKeysWritten; ++i) {
|
|
|
|
// compressible string
|
2020-07-09 23:33:42 +02:00
|
|
|
ASSERT_OK(Put(Key(i), rnd.RandomString(128) + std::string(128, 'a')));
|
2017-04-26 23:18:58 +02:00
|
|
|
}
|
|
|
|
ASSERT_OK(Flush());
|
|
|
|
ASSERT_GT(options.statistics->getTickerCount(NUMBER_BLOCK_COMPRESSED), 0);
|
|
|
|
|
|
|
|
for (int i = 0; i < kNumKeysWritten; ++i) {
|
|
|
|
auto r = Get(Key(i));
|
|
|
|
}
|
|
|
|
ASSERT_GT(options.statistics->getTickerCount(NUMBER_BLOCK_DECOMPRESSED), 0);
|
|
|
|
|
|
|
|
options.compression = kNoCompression;
|
|
|
|
DestroyAndReopen(options);
|
|
|
|
uint64_t currentCompressions =
|
|
|
|
options.statistics->getTickerCount(NUMBER_BLOCK_COMPRESSED);
|
|
|
|
uint64_t currentDecompressions =
|
|
|
|
options.statistics->getTickerCount(NUMBER_BLOCK_DECOMPRESSED);
|
|
|
|
|
|
|
|
// Check that compressions do not occur when turned off
|
|
|
|
for (int i = 0; i < kNumKeysWritten; ++i) {
|
|
|
|
// compressible string
|
2020-07-09 23:33:42 +02:00
|
|
|
ASSERT_OK(Put(Key(i), rnd.RandomString(128) + std::string(128, 'a')));
|
2017-04-26 23:18:58 +02:00
|
|
|
}
|
|
|
|
ASSERT_OK(Flush());
|
|
|
|
ASSERT_EQ(options.statistics->getTickerCount(NUMBER_BLOCK_COMPRESSED)
|
|
|
|
- currentCompressions, 0);
|
|
|
|
|
|
|
|
for (int i = 0; i < kNumKeysWritten; ++i) {
|
|
|
|
auto r = Get(Key(i));
|
|
|
|
}
|
|
|
|
ASSERT_EQ(options.statistics->getTickerCount(NUMBER_BLOCK_DECOMPRESSED)
|
|
|
|
- currentDecompressions, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DBStatisticsTest, MutexWaitStatsDisabledByDefault) {
|
|
|
|
Options options = CurrentOptions();
|
|
|
|
options.create_if_missing = true;
|
2020-02-20 21:07:53 +01:00
|
|
|
options.statistics = ROCKSDB_NAMESPACE::CreateDBStatistics();
|
2017-04-26 23:18:58 +02:00
|
|
|
CreateAndReopenWithCF({"pikachu"}, options);
|
|
|
|
const uint64_t kMutexWaitDelay = 100;
|
|
|
|
ThreadStatusUtil::TEST_SetStateDelay(ThreadStatus::STATE_MUTEX_WAIT,
|
|
|
|
kMutexWaitDelay);
|
|
|
|
ASSERT_OK(Put("hello", "rocksdb"));
|
|
|
|
ASSERT_EQ(TestGetTickerCount(options, DB_MUTEX_WAIT_MICROS), 0);
|
|
|
|
ThreadStatusUtil::TEST_SetStateDelay(ThreadStatus::STATE_MUTEX_WAIT, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DBStatisticsTest, MutexWaitStats) {
|
|
|
|
Options options = CurrentOptions();
|
|
|
|
options.create_if_missing = true;
|
2020-02-20 21:07:53 +01:00
|
|
|
options.statistics = ROCKSDB_NAMESPACE::CreateDBStatistics();
|
2019-03-01 19:39:00 +01:00
|
|
|
options.statistics->set_stats_level(StatsLevel::kAll);
|
2017-04-26 23:18:58 +02:00
|
|
|
CreateAndReopenWithCF({"pikachu"}, options);
|
|
|
|
const uint64_t kMutexWaitDelay = 100;
|
|
|
|
ThreadStatusUtil::TEST_SetStateDelay(ThreadStatus::STATE_MUTEX_WAIT,
|
|
|
|
kMutexWaitDelay);
|
|
|
|
ASSERT_OK(Put("hello", "rocksdb"));
|
|
|
|
ASSERT_GE(TestGetTickerCount(options, DB_MUTEX_WAIT_MICROS), kMutexWaitDelay);
|
|
|
|
ThreadStatusUtil::TEST_SetStateDelay(ThreadStatus::STATE_MUTEX_WAIT, 0);
|
|
|
|
}
|
|
|
|
|
2017-04-27 00:19:50 +02:00
|
|
|
TEST_F(DBStatisticsTest, ResetStats) {
|
|
|
|
Options options = CurrentOptions();
|
|
|
|
options.create_if_missing = true;
|
2020-02-20 21:07:53 +01:00
|
|
|
options.statistics = ROCKSDB_NAMESPACE::CreateDBStatistics();
|
2017-04-27 00:19:50 +02:00
|
|
|
DestroyAndReopen(options);
|
|
|
|
for (int i = 0; i < 2; ++i) {
|
|
|
|
// pick arbitrary ticker and histogram. On first iteration they're zero
|
|
|
|
// because db is unused. On second iteration they're zero due to Reset().
|
|
|
|
ASSERT_EQ(0, TestGetTickerCount(options, NUMBER_KEYS_WRITTEN));
|
|
|
|
HistogramData histogram_data;
|
|
|
|
options.statistics->histogramData(DB_WRITE, &histogram_data);
|
|
|
|
ASSERT_EQ(0.0, histogram_data.max);
|
|
|
|
|
|
|
|
if (i == 0) {
|
|
|
|
// The Put() makes some of the ticker/histogram stats nonzero until we
|
|
|
|
// Reset().
|
|
|
|
ASSERT_OK(Put("hello", "rocksdb"));
|
|
|
|
ASSERT_EQ(1, TestGetTickerCount(options, NUMBER_KEYS_WRITTEN));
|
|
|
|
options.statistics->histogramData(DB_WRITE, &histogram_data);
|
|
|
|
ASSERT_GT(histogram_data.max, 0.0);
|
2020-12-23 08:44:44 +01:00
|
|
|
ASSERT_OK(options.statistics->Reset());
|
2017-04-27 00:19:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-05 08:23:40 +02:00
|
|
|
TEST_F(DBStatisticsTest, ExcludeTickers) {
|
|
|
|
Options options = CurrentOptions();
|
|
|
|
options.statistics = ROCKSDB_NAMESPACE::CreateDBStatistics();
|
|
|
|
DestroyAndReopen(options);
|
|
|
|
options.statistics->set_stats_level(StatsLevel::kExceptTickers);
|
|
|
|
ASSERT_OK(Put("foo", "value"));
|
|
|
|
ASSERT_EQ(0, options.statistics->getTickerCount(BYTES_WRITTEN));
|
|
|
|
options.statistics->set_stats_level(StatsLevel::kExceptHistogramOrTimers);
|
|
|
|
Reopen(options);
|
|
|
|
ASSERT_EQ("value", Get("foo"));
|
|
|
|
ASSERT_GT(options.statistics->getTickerCount(BYTES_READ), 0);
|
|
|
|
}
|
|
|
|
|
2021-09-07 22:25:24 +02:00
|
|
|
#ifndef ROCKSDB_LITE
|
|
|
|
|
|
|
|
TEST_F(DBStatisticsTest, VerifyChecksumReadStat) {
|
|
|
|
Options options = CurrentOptions();
|
|
|
|
options.file_checksum_gen_factory = GetFileChecksumGenCrc32cFactory();
|
|
|
|
options.statistics = ROCKSDB_NAMESPACE::CreateDBStatistics();
|
|
|
|
Reopen(options);
|
|
|
|
|
|
|
|
// Expected to be populated regardless of `PerfLevel` in user thread
|
|
|
|
SetPerfLevel(kDisable);
|
|
|
|
|
|
|
|
{
|
|
|
|
// Scenario 0: only WAL data. Not verified so require ticker to be zero.
|
|
|
|
ASSERT_OK(Put("foo", "value"));
|
|
|
|
ASSERT_OK(db_->VerifyFileChecksums(ReadOptions()));
|
|
|
|
ASSERT_OK(db_->VerifyChecksum());
|
|
|
|
ASSERT_EQ(0,
|
|
|
|
options.statistics->getTickerCount(VERIFY_CHECKSUM_READ_BYTES));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create one SST.
|
|
|
|
ASSERT_OK(Flush());
|
|
|
|
std::unordered_map<std::string, uint64_t> table_files;
|
|
|
|
uint64_t table_files_size = 0;
|
|
|
|
GetAllDataFiles(kTableFile, &table_files, &table_files_size);
|
|
|
|
|
|
|
|
{
|
|
|
|
// Scenario 1: Table verified in `VerifyFileChecksums()`. This should read
|
|
|
|
// the whole file so we require the ticker stat exactly matches the file
|
|
|
|
// size.
|
|
|
|
ASSERT_OK(options.statistics->Reset());
|
|
|
|
ASSERT_OK(db_->VerifyFileChecksums(ReadOptions()));
|
|
|
|
ASSERT_EQ(table_files_size,
|
|
|
|
options.statistics->getTickerCount(VERIFY_CHECKSUM_READ_BYTES));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Scenario 2: Table verified in `VerifyChecksum()`. This opens a
|
|
|
|
// `TableReader` to verify each block. It can involve duplicate reads of the
|
|
|
|
// same data so we set a lower-bound only.
|
|
|
|
ASSERT_OK(options.statistics->Reset());
|
|
|
|
ASSERT_OK(db_->VerifyChecksum());
|
|
|
|
ASSERT_GE(options.statistics->getTickerCount(VERIFY_CHECKSUM_READ_BYTES),
|
|
|
|
table_files_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // !ROCKSDB_LITE
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|
2017-04-26 23:18:58 +02:00
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2020-02-20 21:07:53 +01:00
|
|
|
ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
|
2017-04-26 23:18:58 +02:00
|
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
|
|
return RUN_ALL_TESTS();
|
|
|
|
}
|