mirror of https://github.com/grpc/grpc.git
commit
764ef8c797
35 changed files with 442 additions and 568 deletions
@ -1,64 +0,0 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2015 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. |
||||
* |
||||
*/ |
||||
|
||||
#ifndef GRPC_SUPPORT_HISTOGRAM_H |
||||
#define GRPC_SUPPORT_HISTOGRAM_H |
||||
|
||||
#include <grpc/support/port_platform.h> |
||||
#include <stddef.h> |
||||
|
||||
#ifdef __cplusplus |
||||
extern "C" { |
||||
#endif |
||||
|
||||
typedef struct gpr_histogram gpr_histogram; |
||||
|
||||
GPRAPI gpr_histogram* gpr_histogram_create(double resolution, |
||||
double max_bucket_start); |
||||
GPRAPI void gpr_histogram_destroy(gpr_histogram* h); |
||||
GPRAPI void gpr_histogram_add(gpr_histogram* h, double x); |
||||
|
||||
/** The following merges the second histogram into the first. It only works
|
||||
if they have the same buckets and resolution. Returns 0 on failure, 1 |
||||
on success */ |
||||
GPRAPI int gpr_histogram_merge(gpr_histogram* dst, const gpr_histogram* src); |
||||
|
||||
GPRAPI double gpr_histogram_percentile(gpr_histogram* histogram, |
||||
double percentile); |
||||
GPRAPI double gpr_histogram_mean(gpr_histogram* histogram); |
||||
GPRAPI double gpr_histogram_stddev(gpr_histogram* histogram); |
||||
GPRAPI double gpr_histogram_variance(gpr_histogram* histogram); |
||||
GPRAPI double gpr_histogram_maximum(gpr_histogram* histogram); |
||||
GPRAPI double gpr_histogram_minimum(gpr_histogram* histogram); |
||||
GPRAPI double gpr_histogram_count(gpr_histogram* histogram); |
||||
GPRAPI double gpr_histogram_sum(gpr_histogram* histogram); |
||||
GPRAPI double gpr_histogram_sum_of_squares(gpr_histogram* histogram); |
||||
|
||||
GPRAPI const uint32_t* gpr_histogram_get_contents(gpr_histogram* histogram, |
||||
size_t* count); |
||||
GPRAPI void gpr_histogram_merge_contents(gpr_histogram* histogram, |
||||
const uint32_t* data, |
||||
size_t data_count, double min_seen, |
||||
double max_seen, double sum, |
||||
double sum_of_squares, double count); |
||||
|
||||
#ifdef __cplusplus |
||||
} |
||||
#endif |
||||
|
||||
#endif /* GRPC_SUPPORT_HISTOGRAM_H */ |
@ -1,163 +0,0 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2015 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. |
||||
* |
||||
*/ |
||||
|
||||
#include <grpc/support/histogram.h> |
||||
#include <grpc/support/log.h> |
||||
|
||||
#define LOG_TEST(x) gpr_log(GPR_INFO, "%s", x); |
||||
|
||||
static void test_no_op(void) { |
||||
gpr_histogram_destroy(gpr_histogram_create(0.01, 60e9)); |
||||
} |
||||
|
||||
static void expect_percentile(gpr_histogram* h, double percentile, |
||||
double min_expect, double max_expect) { |
||||
double got = gpr_histogram_percentile(h, percentile); |
||||
gpr_log(GPR_INFO, "@%f%%, expect %f <= %f <= %f", percentile, min_expect, got, |
||||
max_expect); |
||||
GPR_ASSERT(min_expect <= got); |
||||
GPR_ASSERT(got <= max_expect); |
||||
} |
||||
|
||||
static void test_simple(void) { |
||||
gpr_histogram* h; |
||||
|
||||
LOG_TEST("test_simple"); |
||||
|
||||
h = gpr_histogram_create(0.01, 60e9); |
||||
gpr_histogram_add(h, 10000); |
||||
gpr_histogram_add(h, 10000); |
||||
gpr_histogram_add(h, 11000); |
||||
gpr_histogram_add(h, 11000); |
||||
|
||||
expect_percentile(h, 50, 10001, 10999); |
||||
GPR_ASSERT(gpr_histogram_mean(h) == 10500); |
||||
|
||||
gpr_histogram_destroy(h); |
||||
} |
||||
|
||||
static void test_percentile(void) { |
||||
gpr_histogram* h; |
||||
double last; |
||||
double i; |
||||
double cur; |
||||
|
||||
LOG_TEST("test_percentile"); |
||||
|
||||
h = gpr_histogram_create(0.05, 1e9); |
||||
gpr_histogram_add(h, 2.5); |
||||
gpr_histogram_add(h, 2.5); |
||||
gpr_histogram_add(h, 8); |
||||
gpr_histogram_add(h, 4); |
||||
|
||||
GPR_ASSERT(gpr_histogram_count(h) == 4); |
||||
GPR_ASSERT(gpr_histogram_minimum(h) == 2.5); |
||||
GPR_ASSERT(gpr_histogram_maximum(h) == 8); |
||||
GPR_ASSERT(gpr_histogram_sum(h) == 17); |
||||
GPR_ASSERT(gpr_histogram_sum_of_squares(h) == 92.5); |
||||
GPR_ASSERT(gpr_histogram_mean(h) == 4.25); |
||||
GPR_ASSERT(gpr_histogram_variance(h) == 5.0625); |
||||
GPR_ASSERT(gpr_histogram_stddev(h) == 2.25); |
||||
|
||||
expect_percentile(h, -10, 2.5, 2.5); |
||||
expect_percentile(h, 0, 2.5, 2.5); |
||||
expect_percentile(h, 12.5, 2.5, 2.5); |
||||
expect_percentile(h, 25, 2.5, 2.5); |
||||
expect_percentile(h, 37.5, 2.5, 2.8); |
||||
expect_percentile(h, 50, 3.0, 3.5); |
||||
expect_percentile(h, 62.5, 3.5, 4.5); |
||||
expect_percentile(h, 75, 5, 7.9); |
||||
expect_percentile(h, 100, 8, 8); |
||||
expect_percentile(h, 110, 8, 8); |
||||
|
||||
/* test monotonicity */ |
||||
last = 0.0; |
||||
for (i = 0; i < 100.0; i += 0.01) { |
||||
cur = gpr_histogram_percentile(h, i); |
||||
GPR_ASSERT(cur >= last); |
||||
last = cur; |
||||
} |
||||
|
||||
gpr_histogram_destroy(h); |
||||
} |
||||
|
||||
static void test_merge(void) { |
||||
gpr_histogram *h1, *h2; |
||||
double last; |
||||
double i; |
||||
double cur; |
||||
|
||||
LOG_TEST("test_merge"); |
||||
|
||||
h1 = gpr_histogram_create(0.05, 1e9); |
||||
gpr_histogram_add(h1, 2.5); |
||||
gpr_histogram_add(h1, 2.5); |
||||
gpr_histogram_add(h1, 8); |
||||
gpr_histogram_add(h1, 4); |
||||
|
||||
h2 = gpr_histogram_create(0.01, 1e9); |
||||
GPR_ASSERT(gpr_histogram_merge(h1, h2) == 0); |
||||
gpr_histogram_destroy(h2); |
||||
|
||||
h2 = gpr_histogram_create(0.05, 1e10); |
||||
GPR_ASSERT(gpr_histogram_merge(h1, h2) == 0); |
||||
gpr_histogram_destroy(h2); |
||||
|
||||
h2 = gpr_histogram_create(0.05, 1e9); |
||||
GPR_ASSERT(gpr_histogram_merge(h1, h2) == 1); |
||||
GPR_ASSERT(gpr_histogram_count(h1) == 4); |
||||
GPR_ASSERT(gpr_histogram_minimum(h1) == 2.5); |
||||
GPR_ASSERT(gpr_histogram_maximum(h1) == 8); |
||||
GPR_ASSERT(gpr_histogram_sum(h1) == 17); |
||||
GPR_ASSERT(gpr_histogram_sum_of_squares(h1) == 92.5); |
||||
GPR_ASSERT(gpr_histogram_mean(h1) == 4.25); |
||||
GPR_ASSERT(gpr_histogram_variance(h1) == 5.0625); |
||||
GPR_ASSERT(gpr_histogram_stddev(h1) == 2.25); |
||||
gpr_histogram_destroy(h2); |
||||
|
||||
h2 = gpr_histogram_create(0.05, 1e9); |
||||
gpr_histogram_add(h2, 7.0); |
||||
gpr_histogram_add(h2, 17.0); |
||||
gpr_histogram_add(h2, 1.0); |
||||
GPR_ASSERT(gpr_histogram_merge(h1, h2) == 1); |
||||
GPR_ASSERT(gpr_histogram_count(h1) == 7); |
||||
GPR_ASSERT(gpr_histogram_minimum(h1) == 1.0); |
||||
GPR_ASSERT(gpr_histogram_maximum(h1) == 17.0); |
||||
GPR_ASSERT(gpr_histogram_sum(h1) == 42.0); |
||||
GPR_ASSERT(gpr_histogram_sum_of_squares(h1) == 431.5); |
||||
GPR_ASSERT(gpr_histogram_mean(h1) == 6.0); |
||||
|
||||
/* test monotonicity */ |
||||
last = 0.0; |
||||
for (i = 0; i < 100.0; i += 0.01) { |
||||
cur = gpr_histogram_percentile(h1, i); |
||||
GPR_ASSERT(cur >= last); |
||||
last = cur; |
||||
} |
||||
|
||||
gpr_histogram_destroy(h1); |
||||
gpr_histogram_destroy(h2); |
||||
} |
||||
|
||||
int main(void) { |
||||
test_no_op(); |
||||
test_simple(); |
||||
test_percentile(); |
||||
test_merge(); |
||||
return 0; |
||||
} |
@ -0,0 +1,62 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2015 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. |
||||
* |
||||
*/ |
||||
|
||||
#ifndef GRPC_SUPPORT_HISTOGRAM_H |
||||
#define GRPC_SUPPORT_HISTOGRAM_H |
||||
|
||||
#include <grpc/support/port_platform.h> |
||||
#include <stddef.h> |
||||
|
||||
#ifdef __cplusplus |
||||
extern "C" { |
||||
#endif |
||||
|
||||
typedef struct grpc_histogram grpc_histogram; |
||||
|
||||
grpc_histogram* grpc_histogram_create(double resolution, |
||||
double max_bucket_start); |
||||
void grpc_histogram_destroy(grpc_histogram* h); |
||||
void grpc_histogram_add(grpc_histogram* h, double x); |
||||
|
||||
/** The following merges the second histogram into the first. It only works
|
||||
if they have the same buckets and resolution. Returns 0 on failure, 1 |
||||
on success */ |
||||
int grpc_histogram_merge(grpc_histogram* dst, const grpc_histogram* src); |
||||
|
||||
double grpc_histogram_percentile(grpc_histogram* histogram, double percentile); |
||||
double grpc_histogram_mean(grpc_histogram* histogram); |
||||
double grpc_histogram_stddev(grpc_histogram* histogram); |
||||
double grpc_histogram_variance(grpc_histogram* histogram); |
||||
double grpc_histogram_maximum(grpc_histogram* histogram); |
||||
double grpc_histogram_minimum(grpc_histogram* histogram); |
||||
double grpc_histogram_count(grpc_histogram* histogram); |
||||
double grpc_histogram_sum(grpc_histogram* histogram); |
||||
double grpc_histogram_sum_of_squares(grpc_histogram* histogram); |
||||
|
||||
const uint32_t* grpc_histogram_get_contents(grpc_histogram* histogram, |
||||
size_t* count); |
||||
void grpc_histogram_merge_contents(grpc_histogram* histogram, |
||||
const uint32_t* data, size_t data_count, |
||||
double min_seen, double max_seen, double sum, |
||||
double sum_of_squares, double count); |
||||
|
||||
#ifdef __cplusplus |
||||
} |
||||
#endif |
||||
|
||||
#endif /* GRPC_SUPPORT_HISTOGRAM_H */ |
@ -0,0 +1,163 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2015 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. |
||||
* |
||||
*/ |
||||
|
||||
#include "test/core/util/histogram.h" |
||||
#include <grpc/support/log.h> |
||||
|
||||
#define LOG_TEST(x) gpr_log(GPR_INFO, "%s", x); |
||||
|
||||
static void test_no_op(void) { |
||||
grpc_histogram_destroy(grpc_histogram_create(0.01, 60e9)); |
||||
} |
||||
|
||||
static void expect_percentile(grpc_histogram* h, double percentile, |
||||
double min_expect, double max_expect) { |
||||
double got = grpc_histogram_percentile(h, percentile); |
||||
gpr_log(GPR_INFO, "@%f%%, expect %f <= %f <= %f", percentile, min_expect, got, |
||||
max_expect); |
||||
GPR_ASSERT(min_expect <= got); |
||||
GPR_ASSERT(got <= max_expect); |
||||
} |
||||
|
||||
static void test_simple(void) { |
||||
grpc_histogram* h; |
||||
|
||||
LOG_TEST("test_simple"); |
||||
|
||||
h = grpc_histogram_create(0.01, 60e9); |
||||
grpc_histogram_add(h, 10000); |
||||
grpc_histogram_add(h, 10000); |
||||
grpc_histogram_add(h, 11000); |
||||
grpc_histogram_add(h, 11000); |
||||
|
||||
expect_percentile(h, 50, 10001, 10999); |
||||
GPR_ASSERT(grpc_histogram_mean(h) == 10500); |
||||
|
||||
grpc_histogram_destroy(h); |
||||
} |
||||
|
||||
static void test_percentile(void) { |
||||
grpc_histogram* h; |
||||
double last; |
||||
double i; |
||||
double cur; |
||||
|
||||
LOG_TEST("test_percentile"); |
||||
|
||||
h = grpc_histogram_create(0.05, 1e9); |
||||
grpc_histogram_add(h, 2.5); |
||||
grpc_histogram_add(h, 2.5); |
||||
grpc_histogram_add(h, 8); |
||||
grpc_histogram_add(h, 4); |
||||
|
||||
GPR_ASSERT(grpc_histogram_count(h) == 4); |
||||
GPR_ASSERT(grpc_histogram_minimum(h) == 2.5); |
||||
GPR_ASSERT(grpc_histogram_maximum(h) == 8); |
||||
GPR_ASSERT(grpc_histogram_sum(h) == 17); |
||||
GPR_ASSERT(grpc_histogram_sum_of_squares(h) == 92.5); |
||||
GPR_ASSERT(grpc_histogram_mean(h) == 4.25); |
||||
GPR_ASSERT(grpc_histogram_variance(h) == 5.0625); |
||||
GPR_ASSERT(grpc_histogram_stddev(h) == 2.25); |
||||
|
||||
expect_percentile(h, -10, 2.5, 2.5); |
||||
expect_percentile(h, 0, 2.5, 2.5); |
||||
expect_percentile(h, 12.5, 2.5, 2.5); |
||||
expect_percentile(h, 25, 2.5, 2.5); |
||||
expect_percentile(h, 37.5, 2.5, 2.8); |
||||
expect_percentile(h, 50, 3.0, 3.5); |
||||
expect_percentile(h, 62.5, 3.5, 4.5); |
||||
expect_percentile(h, 75, 5, 7.9); |
||||
expect_percentile(h, 100, 8, 8); |
||||
expect_percentile(h, 110, 8, 8); |
||||
|
||||
/* test monotonicity */ |
||||
last = 0.0; |
||||
for (i = 0; i < 100.0; i += 0.01) { |
||||
cur = grpc_histogram_percentile(h, i); |
||||
GPR_ASSERT(cur >= last); |
||||
last = cur; |
||||
} |
||||
|
||||
grpc_histogram_destroy(h); |
||||
} |
||||
|
||||
static void test_merge(void) { |
||||
grpc_histogram *h1, *h2; |
||||
double last; |
||||
double i; |
||||
double cur; |
||||
|
||||
LOG_TEST("test_merge"); |
||||
|
||||
h1 = grpc_histogram_create(0.05, 1e9); |
||||
grpc_histogram_add(h1, 2.5); |
||||
grpc_histogram_add(h1, 2.5); |
||||
grpc_histogram_add(h1, 8); |
||||
grpc_histogram_add(h1, 4); |
||||
|
||||
h2 = grpc_histogram_create(0.01, 1e9); |
||||
GPR_ASSERT(grpc_histogram_merge(h1, h2) == 0); |
||||
grpc_histogram_destroy(h2); |
||||
|
||||
h2 = grpc_histogram_create(0.05, 1e10); |
||||
GPR_ASSERT(grpc_histogram_merge(h1, h2) == 0); |
||||
grpc_histogram_destroy(h2); |
||||
|
||||
h2 = grpc_histogram_create(0.05, 1e9); |
||||
GPR_ASSERT(grpc_histogram_merge(h1, h2) == 1); |
||||
GPR_ASSERT(grpc_histogram_count(h1) == 4); |
||||
GPR_ASSERT(grpc_histogram_minimum(h1) == 2.5); |
||||
GPR_ASSERT(grpc_histogram_maximum(h1) == 8); |
||||
GPR_ASSERT(grpc_histogram_sum(h1) == 17); |
||||
GPR_ASSERT(grpc_histogram_sum_of_squares(h1) == 92.5); |
||||
GPR_ASSERT(grpc_histogram_mean(h1) == 4.25); |
||||
GPR_ASSERT(grpc_histogram_variance(h1) == 5.0625); |
||||
GPR_ASSERT(grpc_histogram_stddev(h1) == 2.25); |
||||
grpc_histogram_destroy(h2); |
||||
|
||||
h2 = grpc_histogram_create(0.05, 1e9); |
||||
grpc_histogram_add(h2, 7.0); |
||||
grpc_histogram_add(h2, 17.0); |
||||
grpc_histogram_add(h2, 1.0); |
||||
GPR_ASSERT(grpc_histogram_merge(h1, h2) == 1); |
||||
GPR_ASSERT(grpc_histogram_count(h1) == 7); |
||||
GPR_ASSERT(grpc_histogram_minimum(h1) == 1.0); |
||||
GPR_ASSERT(grpc_histogram_maximum(h1) == 17.0); |
||||
GPR_ASSERT(grpc_histogram_sum(h1) == 42.0); |
||||
GPR_ASSERT(grpc_histogram_sum_of_squares(h1) == 431.5); |
||||
GPR_ASSERT(grpc_histogram_mean(h1) == 6.0); |
||||
|
||||
/* test monotonicity */ |
||||
last = 0.0; |
||||
for (i = 0; i < 100.0; i += 0.01) { |
||||
cur = grpc_histogram_percentile(h1, i); |
||||
GPR_ASSERT(cur >= last); |
||||
last = cur; |
||||
} |
||||
|
||||
grpc_histogram_destroy(h1); |
||||
grpc_histogram_destroy(h2); |
||||
} |
||||
|
||||
int main(void) { |
||||
test_no_op(); |
||||
test_simple(); |
||||
test_percentile(); |
||||
test_merge(); |
||||
return 0; |
||||
} |
Loading…
Reference in new issue