/Users/deen/code/yugabyte-db/src/yb/util/mt-metrics-test.cc
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // |
18 | | // The following only applies to changes made to this file as part of YugaByte development. |
19 | | // |
20 | | // Portions Copyright (c) YugaByte, Inc. |
21 | | // |
22 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
23 | | // in compliance with the License. You may obtain a copy of the License at |
24 | | // |
25 | | // http://www.apache.org/licenses/LICENSE-2.0 |
26 | | // |
27 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
28 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
29 | | // or implied. See the License for the specific language governing permissions and limitations |
30 | | // under the License. |
31 | | // |
32 | | #include <functional> |
33 | | #include <vector> |
34 | | |
35 | | #include <gtest/gtest.h> |
36 | | |
37 | | #include "yb/gutil/atomicops.h" |
38 | | #include "yb/gutil/ref_counted.h" |
39 | | #include "yb/util/debug/leakcheck_disabler.h" |
40 | | #include "yb/util/metrics.h" |
41 | | #include "yb/util/monotime.h" |
42 | | #include "yb/util/status_log.h" |
43 | | #include "yb/util/test_macros.h" |
44 | | #include "yb/util/test_util.h" |
45 | | #include "yb/util/thread.h" |
46 | | |
47 | | DEFINE_int32(mt_metrics_test_num_threads, 4, |
48 | | "Number of threads to spawn in mt metrics tests"); |
49 | | |
50 | | METRIC_DEFINE_entity(test_entity); |
51 | | |
52 | | namespace yb { |
53 | | |
54 | | using debug::ScopedLeakCheckDisabler; |
55 | | using std::vector; |
56 | | |
57 | | class MultiThreadedMetricsTest : public YBTest { |
58 | | public: |
59 | | static void RegisterCounters(const scoped_refptr<MetricEntity>& metric_entity, |
60 | | const string& name_prefix, int num_counters); |
61 | | |
62 | | MetricRegistry registry_; |
63 | | }; |
64 | | |
65 | | // Call increment on a Counter a bunch of times. |
66 | 4 | static void CountWithCounter(scoped_refptr<Counter> counter, int num_increments) { |
67 | 4.00k | for (int i = 0; i < num_increments; i++) { |
68 | 4.00k | counter->Increment(); |
69 | 4.00k | } |
70 | 4 | } |
71 | | |
72 | | // Helper function that spawns and then joins a bunch of threads. |
73 | 2 | static void RunWithManyThreads(std::function<void()>* f, int num_threads) { |
74 | 2 | vector<scoped_refptr<yb::Thread> > threads; |
75 | 10 | for (int i = 0; i < num_threads; i++) { |
76 | 8 | scoped_refptr<yb::Thread> new_thread; |
77 | 8 | CHECK_OK(yb::Thread::Create("test", StringPrintf("thread%d", i), |
78 | 8 | *f, &new_thread)); |
79 | 8 | threads.push_back(new_thread); |
80 | 8 | } |
81 | 10 | for (int i = 0; i < num_threads; i++) { |
82 | 8 | ASSERT_OK(ThreadJoiner(threads[i].get()).Join()); |
83 | 8 | } |
84 | 2 | } |
85 | | |
86 | | METRIC_DEFINE_counter(test_entity, test_counter, "Test Counter", |
87 | | MetricUnit::kRequests, "Test counter"); |
88 | | |
89 | | // Ensure that incrementing a counter is thread-safe. |
90 | 1 | TEST_F(MultiThreadedMetricsTest, CounterIncrementTest) { |
91 | 1 | scoped_refptr<Counter> counter = new Counter(&METRIC_test_counter); |
92 | 1 | int num_threads = FLAGS_mt_metrics_test_num_threads; |
93 | 1 | int num_increments = 1000; |
94 | 1 | std::function<void()> f = std::bind(CountWithCounter, counter, num_increments); |
95 | 1 | RunWithManyThreads(&f, num_threads); |
96 | 1 | ASSERT_EQ(num_threads * num_increments, counter->value()); |
97 | 1 | } |
98 | | |
99 | | // Helper function to register a bunch of counters in a loop. |
100 | | void MultiThreadedMetricsTest::RegisterCounters( |
101 | | const scoped_refptr<MetricEntity>& metric_entity, |
102 | | const string& name_prefix, |
103 | 4 | int num_counters) { |
104 | 4 | uint64_t tid = Env::Default()->gettid(); |
105 | 4.00k | for (int i = 0; i < num_counters; i++) { |
106 | | // This loop purposefully leaks metrics prototypes, because the metrics system |
107 | | // expects the prototypes and their names to live forever. This is the only |
108 | | // place we dynamically generate them for the purposes of a test, so it's easier |
109 | | // to just leak them than to figure out a way to manage lifecycle of objects that |
110 | | // are typically static. |
111 | 4.00k | ScopedLeakCheckDisabler disabler; |
112 | | |
113 | 4.00k | string name = strings::Substitute("$0_$1_$2", name_prefix, tid, i); |
114 | 4.00k | auto proto = new CounterPrototype(MetricPrototype::CtorArgs( |
115 | 4.00k | "test_entity", strdup(name.c_str()), "Test Counter", |
116 | 4.00k | MetricUnit::kOperations, "test counter", |
117 | 4.00k | MetricLevel::kInfo)); |
118 | 4.00k | proto->Instantiate(metric_entity)->Increment(); |
119 | 4.00k | } |
120 | 4 | } |
121 | | |
122 | | // Ensure that adding a counter to a registry is thread-safe. |
123 | 1 | TEST_F(MultiThreadedMetricsTest, AddCounterToRegistryTest) { |
124 | 1 | scoped_refptr<MetricEntity> entity = METRIC_ENTITY_test_entity.Instantiate(®istry_, "my-test"); |
125 | 1 | int num_threads = FLAGS_mt_metrics_test_num_threads; |
126 | 1 | int num_counters = 1000; |
127 | 1 | std::function<void()> f = std::bind(RegisterCounters, entity, "prefix", num_counters); |
128 | 1 | RunWithManyThreads(&f, num_threads); |
129 | 1 | ASSERT_EQ(num_threads * num_counters, entity->UnsafeMetricsMapForTests().size()); |
130 | 1 | } |
131 | | |
132 | | } // namespace yb |