summaryrefslogtreecommitdiffstats
path: root/src/test/common/test_perf_counters_key.cc
blob: 5a156c749b2079deff08f618dd42d4a871499d3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "common/perf_counters_key.h"
#include <gtest/gtest.h>

namespace ceph::perf_counters {

TEST(PerfCounters, key_create)
{
  EXPECT_EQ(key_create(""),
            std::string_view("\0", 1));

  EXPECT_EQ(key_create("perf"),
            std::string_view("perf\0", 5));

  EXPECT_EQ(key_create("perf", {{"",""}}),
            std::string_view("perf\0\0\0", 7));

  EXPECT_EQ(key_create("perf", {{"","a"}, {"",""}}),
            std::string_view("perf\0\0a\0", 8));

  EXPECT_EQ(key_create("perf", {{"a","b"}}),
            std::string_view("perf\0a\0b\0", 9));

  EXPECT_EQ(key_create("perf", {{"y","z"}, {"a","b"}}),
            std::string_view("perf\0a\0b\0y\0z\0", 13));

  EXPECT_EQ(key_create("perf", {{"a","b"}, {"a","c"}}),
            std::string_view("perf\0a\0b\0", 9));

  EXPECT_EQ(key_create("perf", {{"a","z"}, {"a","b"}}),
            std::string_view("perf\0a\0z\0", 9));

  EXPECT_EQ(key_create("perf", {{"d",""}, {"c",""}, {"b",""}, {"a",""}}),
            std::string_view("perf\0a\0\0b\0\0c\0\0d\0\0", 17));
}

TEST(PerfCounters, key_insert)
{
  EXPECT_EQ(key_insert("", {{"",""}}),
            std::string_view("\0\0\0", 3));

  EXPECT_EQ(key_insert("", {{"",""}, {"",""}}),
            std::string_view("\0\0\0", 3));

  EXPECT_EQ(key_insert(std::string_view{"\0\0\0", 3}, {{"",""}}),
            std::string_view("\0\0\0", 3));

  EXPECT_EQ(key_insert(std::string_view{"\0", 1}, {{"",""}}),
            std::string_view("\0\0\0", 3));

  EXPECT_EQ(key_insert("", {{"a","b"}}),
            std::string_view("\0a\0b\0", 5));

  EXPECT_EQ(key_insert(std::string_view{"\0", 1}, {{"a","b"}}),
            std::string_view("\0a\0b\0", 5));

  EXPECT_EQ(key_insert("a", {{"",""}}),
            std::string_view("a\0\0\0", 4));

  EXPECT_EQ(key_insert(std::string_view{"a\0", 2}, {{"",""}}),
            std::string_view("a\0\0\0", 4));

  EXPECT_EQ(key_insert(std::string_view{"p\0", 2}, {{"a","b"}}),
            std::string_view("p\0a\0b\0", 6));

  EXPECT_EQ(key_insert(std::string_view{"p\0a\0a\0", 6}, {{"a","b"}}),
            std::string_view("p\0a\0b\0", 6));

  EXPECT_EQ(key_insert(std::string_view{"p\0a\0z\0", 6}, {{"a","b"}}),
            std::string_view("p\0a\0b\0", 6));

  EXPECT_EQ(key_insert(std::string_view{"p\0z\0z\0", 6}, {{"a","b"}}),
            std::string_view("p\0a\0b\0z\0z\0", 10));

  EXPECT_EQ(key_insert(std::string_view{"p\0b\0b\0", 6},
                       {{"a","a"}, {"c","c"}}),
            std::string_view("p\0a\0a\0b\0b\0c\0c\0", 14));

  EXPECT_EQ(key_insert(std::string_view{"p\0a\0a\0b\0b\0c\0c\0", 14},
                       {{"z","z"}, {"b","z"}}),
            std::string_view("p\0a\0a\0b\0z\0c\0c\0z\0z\0", 18));
}

TEST(PerfCounters, key_name)
{
  EXPECT_EQ(key_name(""),
            "");
  EXPECT_EQ(key_name({"\0", 1}),
            "");
  EXPECT_EQ(key_name({"perf\0", 5}),
            "perf");
  EXPECT_EQ(key_name({"perf\0\0\0", 7}),
            "perf");
}

TEST(PerfCounters, key_labels)
{
  {
    auto labels = key_labels("");
    EXPECT_EQ(labels.begin(), labels.end());
  }
  {
    auto labels = key_labels({"\0", 1});
    EXPECT_EQ(labels.begin(), labels.end());
  }
  {
    auto labels = key_labels({"perf\0", 5});
    EXPECT_EQ(labels.begin(), labels.end());
  }
  {
    auto labels = key_labels({"\0\0\0", 3});
    ASSERT_EQ(1, std::distance(labels.begin(), labels.end()));
    EXPECT_EQ(label_pair("", ""), *labels.begin());
  }
  {
    auto labels = key_labels({"\0a\0b\0", 5});
    ASSERT_EQ(1, std::distance(labels.begin(), labels.end()));
    EXPECT_EQ(label_pair("a", "b"), *labels.begin());
    EXPECT_EQ(std::next(labels.begin()), labels.end());
  }
  {
    auto labels = key_labels({"\0a\0b\0c\0d\0", 9});
    ASSERT_EQ(2, std::distance(labels.begin(), labels.end()));
    EXPECT_EQ(label_pair("a", "b"), *labels.begin());
    EXPECT_EQ(label_pair("c", "d"), *std::next(labels.begin()));
    EXPECT_EQ(std::next(labels.begin(), 2), labels.end());
  }
}

} // namespace ceph::perf_counters