mirror of https://github.com/grpc/grpc.git
The C based gRPC (C++, Python, Ruby, Objective-C, PHP, C#)
https://grpc.io/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
103 lines
2.8 KiB
103 lines
2.8 KiB
2 years ago
|
//
|
||
|
//
|
||
|
// Copyright 2017 gRPC authors.
|
||
|
//
|
||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
// you may not use this file except in compliance with the License.
|
||
|
// You may obtain a copy of the License at
|
||
|
//
|
||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||
|
//
|
||
|
// Unless required by applicable law or agreed to in writing, software
|
||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
// See the License for the specific language governing permissions and
|
||
|
// limitations under the License.
|
||
|
//
|
||
|
//
|
||
7 years ago
|
|
||
6 months ago
|
#include "src/core/telemetry/stats.h"
|
||
7 years ago
|
|
||
2 years ago
|
#include <algorithm>
|
||
|
#include <memory>
|
||
7 years ago
|
|
||
2 years ago
|
#include "gtest/gtest.h"
|
||
3 years ago
|
|
||
7 years ago
|
#include <grpc/grpc.h>
|
||
|
|
||
2 years ago
|
#include "src/core/lib/iomgr/exec_ctx.h"
|
||
6 months ago
|
#include "src/core/telemetry/stats_data.h"
|
||
7 months ago
|
#include "test/core/test_util/test_config.h"
|
||
4 years ago
|
|
||
2 years ago
|
namespace grpc_core {
|
||
7 years ago
|
namespace testing {
|
||
|
|
||
|
class Snapshot {
|
||
|
public:
|
||
2 years ago
|
std::unique_ptr<GlobalStats> delta() {
|
||
|
auto now = global_stats().Collect();
|
||
|
return now->Diff(*begin_);
|
||
7 years ago
|
}
|
||
|
|
||
|
private:
|
||
2 years ago
|
std::unique_ptr<GlobalStats> begin_ = global_stats().Collect();
|
||
7 years ago
|
};
|
||
|
|
||
|
TEST(StatsTest, IncSpecificCounter) {
|
||
7 years ago
|
std::unique_ptr<Snapshot> snapshot(new Snapshot);
|
||
7 years ago
|
|
||
2 years ago
|
ExecCtx exec_ctx;
|
||
|
global_stats().IncrementClientCallsCreated();
|
||
7 years ago
|
|
||
2 years ago
|
EXPECT_EQ(snapshot->delta()->client_calls_created, 1);
|
||
7 years ago
|
}
|
||
|
|
||
2 years ago
|
TEST(StatsTest, IncrementHttp2MetadataSize) {
|
||
|
ExecCtx exec_ctx;
|
||
|
global_stats().IncrementHttp2MetadataSize(0);
|
||
|
}
|
||
|
|
||
2 years ago
|
static int FindExpectedBucket(const HistogramView& h, int value) {
|
||
|
if (value < 0) {
|
||
7 years ago
|
return 0;
|
||
|
}
|
||
2 years ago
|
if (value >= h.bucket_boundaries[h.num_buckets]) {
|
||
|
return h.num_buckets - 1;
|
||
7 years ago
|
}
|
||
2 years ago
|
return std::upper_bound(h.bucket_boundaries,
|
||
|
h.bucket_boundaries + h.num_buckets, value) -
|
||
|
h.bucket_boundaries - 1;
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
class HistogramTest : public ::testing::TestWithParam<int> {};
|
||
|
|
||
2 years ago
|
TEST_P(HistogramTest, CheckBucket) {
|
||
2 years ago
|
const GlobalStats::Histogram kHistogram =
|
||
|
static_cast<GlobalStats::Histogram>(GetParam());
|
||
|
auto some_stats = std::make_unique<GlobalStats>();
|
||
|
auto view = some_stats->histogram(kHistogram);
|
||
|
const int max_bucket_boundary = view.bucket_boundaries[view.num_buckets];
|
||
2 years ago
|
for (int i = -1000; i < max_bucket_boundary + 1000; i++) {
|
||
2 years ago
|
ASSERT_EQ(FindExpectedBucket(view, i), view.bucket_for(i))
|
||
2 years ago
|
<< "i=" << i << " expect_bucket="
|
||
2 years ago
|
<< view.bucket_boundaries[FindExpectedBucket(view, i)]
|
||
|
<< " actual_bucket=" << view.bucket_boundaries[view.bucket_for(i)];
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
INSTANTIATE_TEST_SUITE_P(
|
||
|
HistogramTestCases, HistogramTest,
|
||
|
::testing::Range<int>(0, static_cast<int>(GlobalStats::Histogram::COUNT)));
|
||
7 years ago
|
|
||
7 years ago
|
} // namespace testing
|
||
2 years ago
|
} // namespace grpc_core
|
||
7 years ago
|
|
||
|
int main(int argc, char** argv) {
|
||
3 years ago
|
grpc::testing::TestEnvironment env(&argc, argv);
|
||
7 years ago
|
::testing::InitGoogleTest(&argc, argv);
|
||
|
grpc_init();
|
||
|
int ret = RUN_ALL_TESTS();
|
||
|
grpc_shutdown();
|
||
|
return ret;
|
||
|
}
|