statistics_test.cc 3.1 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. //
  6. #include "rocksdb/statistics.h"
  7. #include "port/stack_trace.h"
  8. #include "rocksdb/convenience.h"
  9. #include "rocksdb/utilities/options_type.h"
  10. #include "test_util/testharness.h"
  11. #include "test_util/testutil.h"
  12. namespace ROCKSDB_NAMESPACE {
  13. class StatisticsTest : public testing::Test {};
  14. // Sanity check to make sure that contents and order of TickersNameMap
  15. // match Tickers enum
  16. TEST_F(StatisticsTest, SanityTickers) {
  17. EXPECT_EQ(static_cast<size_t>(Tickers::TICKER_ENUM_MAX),
  18. TickersNameMap.size());
  19. for (uint32_t t = 0; t < Tickers::TICKER_ENUM_MAX; t++) {
  20. auto pair = TickersNameMap[static_cast<size_t>(t)];
  21. ASSERT_EQ(pair.first, t) << "Miss match at " << pair.second;
  22. }
  23. }
  24. // Sanity check to make sure that contents and order of HistogramsNameMap
  25. // match Tickers enum
  26. TEST_F(StatisticsTest, SanityHistograms) {
  27. EXPECT_EQ(static_cast<size_t>(Histograms::HISTOGRAM_ENUM_MAX),
  28. HistogramsNameMap.size());
  29. for (uint32_t h = 0; h < Histograms::HISTOGRAM_ENUM_MAX; h++) {
  30. auto pair = HistogramsNameMap[static_cast<size_t>(h)];
  31. ASSERT_EQ(pair.first, h) << "Miss match at " << pair.second;
  32. }
  33. }
  34. TEST_F(StatisticsTest, NoNameStats) {
  35. static std::unordered_map<std::string, OptionTypeInfo> no_name_opt_info = {
  36. {"inner",
  37. OptionTypeInfo::AsCustomSharedPtr<Statistics>(
  38. 0, OptionVerificationType::kByName,
  39. OptionTypeFlags::kAllowNull | OptionTypeFlags::kCompareNever)},
  40. };
  41. class DefaultNameStatistics : public Statistics {
  42. public:
  43. DefaultNameStatistics(const std::shared_ptr<Statistics>& stats = nullptr)
  44. : inner(stats) {
  45. RegisterOptions("", &inner, &no_name_opt_info);
  46. }
  47. uint64_t getTickerCount(uint32_t /*tickerType*/) const override {
  48. return 0;
  49. }
  50. void histogramData(uint32_t /*type*/,
  51. HistogramData* const /*data*/) const override {}
  52. void recordTick(uint32_t /*tickerType*/, uint64_t /*count*/) override {}
  53. void setTickerCount(uint32_t /*tickerType*/, uint64_t /*count*/) override {}
  54. uint64_t getAndResetTickerCount(uint32_t /*tickerType*/) override {
  55. return 0;
  56. }
  57. std::shared_ptr<Statistics> inner;
  58. };
  59. ConfigOptions options;
  60. options.ignore_unsupported_options = false;
  61. auto stats = std::make_shared<DefaultNameStatistics>();
  62. ASSERT_STREQ(stats->Name(), "");
  63. ASSERT_EQ("", stats->ToString(
  64. options)); // A stats with no name with have no options...
  65. ASSERT_OK(stats->ConfigureFromString(options, "inner="));
  66. ASSERT_EQ("", stats->ToString(
  67. options)); // A stats with no name with have no options...
  68. ASSERT_NE(stats->inner, nullptr);
  69. ASSERT_NE("", stats->inner->ToString(options)); // ... even if it does...
  70. }
  71. } // namespace ROCKSDB_NAMESPACE
  72. int main(int argc, char** argv) {
  73. ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
  74. ::testing::InitGoogleTest(&argc, argv);
  75. return RUN_ALL_TESTS();
  76. }