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).
7 #include "rocksdb/statistics.h"
9 #include "port/stack_trace.h"
10 #include "rocksdb/convenience.h"
11 #include "rocksdb/utilities/options_type.h"
12 #include "test_util/testharness.h"
13 #include "test_util/testutil.h"
15 namespace ROCKSDB_NAMESPACE
{
17 class StatisticsTest
: public testing::Test
{};
19 // Sanity check to make sure that contents and order of TickersNameMap
21 TEST_F(StatisticsTest
, SanityTickers
) {
22 EXPECT_EQ(static_cast<size_t>(Tickers::TICKER_ENUM_MAX
),
23 TickersNameMap
.size());
25 for (uint32_t t
= 0; t
< Tickers::TICKER_ENUM_MAX
; t
++) {
26 auto pair
= TickersNameMap
[static_cast<size_t>(t
)];
27 ASSERT_EQ(pair
.first
, t
) << "Miss match at " << pair
.second
;
31 // Sanity check to make sure that contents and order of HistogramsNameMap
33 TEST_F(StatisticsTest
, SanityHistograms
) {
34 EXPECT_EQ(static_cast<size_t>(Histograms::HISTOGRAM_ENUM_MAX
),
35 HistogramsNameMap
.size());
37 for (uint32_t h
= 0; h
< Histograms::HISTOGRAM_ENUM_MAX
; h
++) {
38 auto pair
= HistogramsNameMap
[static_cast<size_t>(h
)];
39 ASSERT_EQ(pair
.first
, h
) << "Miss match at " << pair
.second
;
43 TEST_F(StatisticsTest
, NoNameStats
) {
44 static std::unordered_map
<std::string
, OptionTypeInfo
> no_name_opt_info
= {
47 OptionTypeInfo::AsCustomSharedPtr
<Statistics
>(
48 0, OptionVerificationType::kByName
,
49 OptionTypeFlags::kAllowNull
| OptionTypeFlags::kCompareNever
)},
50 #endif // ROCKSDB_LITE
53 class DefaultNameStatistics
: public Statistics
{
55 DefaultNameStatistics(const std::shared_ptr
<Statistics
>& stats
= nullptr)
57 RegisterOptions("", &inner
, &no_name_opt_info
);
60 uint64_t getTickerCount(uint32_t /*tickerType*/) const override
{
63 void histogramData(uint32_t /*type*/,
64 HistogramData
* const /*data*/) const override
{}
65 void recordTick(uint32_t /*tickerType*/, uint64_t /*count*/) override
{}
66 void setTickerCount(uint32_t /*tickerType*/, uint64_t /*count*/) override
{}
67 uint64_t getAndResetTickerCount(uint32_t /*tickerType*/) override
{
70 std::shared_ptr
<Statistics
> inner
;
72 ConfigOptions options
;
73 options
.ignore_unsupported_options
= false;
74 auto stats
= std::make_shared
<DefaultNameStatistics
>();
75 ASSERT_STREQ(stats
->Name(), "");
77 ASSERT_EQ("", stats
->ToString(
78 options
)); // A stats with no name with have no options...
79 ASSERT_OK(stats
->ConfigureFromString(options
, "inner="));
80 ASSERT_EQ("", stats
->ToString(
81 options
)); // A stats with no name with have no options...
82 ASSERT_NE(stats
->inner
, nullptr);
83 ASSERT_NE("", stats
->inner
->ToString(options
)); // ... even if it does...
84 #endif // ROCKSDB_LITE
86 } // namespace ROCKSDB_NAMESPACE
88 int main(int argc
, char** argv
) {
89 ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
90 ::testing::InitGoogleTest(&argc
, argv
);
91 return RUN_ALL_TESTS();