LCOV - code coverage report
Current view: top level - test/core/support - histogram_test.c (source / functions) Hit Total Coverage
Test: tmp.zDYK9MVh93 Lines: 101 101 100.0 %
Date: 2015-10-10 Functions: 6 6 100.0 %

          Line data    Source code
       1             : /*
       2             :  *
       3             :  * Copyright 2015, Google Inc.
       4             :  * All rights reserved.
       5             :  *
       6             :  * Redistribution and use in source and binary forms, with or without
       7             :  * modification, are permitted provided that the following conditions are
       8             :  * met:
       9             :  *
      10             :  *     * Redistributions of source code must retain the above copyright
      11             :  * notice, this list of conditions and the following disclaimer.
      12             :  *     * Redistributions in binary form must reproduce the above
      13             :  * copyright notice, this list of conditions and the following disclaimer
      14             :  * in the documentation and/or other materials provided with the
      15             :  * distribution.
      16             :  *     * Neither the name of Google Inc. nor the names of its
      17             :  * contributors may be used to endorse or promote products derived from
      18             :  * this software without specific prior written permission.
      19             :  *
      20             :  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
      21             :  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
      22             :  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
      23             :  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
      24             :  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
      25             :  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
      26             :  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
      27             :  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
      28             :  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
      29             :  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
      30             :  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      31             :  *
      32             :  */
      33             : 
      34             : #include <grpc/support/histogram.h>
      35             : #include <grpc/support/log.h>
      36             : 
      37             : #define LOG_TEST(x) gpr_log(GPR_INFO, "%s", x);
      38             : 
      39           1 : static void test_no_op(void) {
      40           1 :   gpr_histogram_destroy(gpr_histogram_create(0.01, 60e9));
      41           1 : }
      42             : 
      43          11 : static void expect_percentile(gpr_histogram *h, double percentile,
      44             :                               double min_expect, double max_expect) {
      45          11 :   double got = gpr_histogram_percentile(h, percentile);
      46          11 :   gpr_log(GPR_INFO, "@%f%%, expect %f <= %f <= %f", percentile, min_expect, got,
      47             :           max_expect);
      48          11 :   GPR_ASSERT(min_expect <= got);
      49          11 :   GPR_ASSERT(got <= max_expect);
      50          11 : }
      51             : 
      52           1 : static void test_simple(void) {
      53             :   gpr_histogram *h;
      54             : 
      55           1 :   LOG_TEST("test_simple");
      56             : 
      57           1 :   h = gpr_histogram_create(0.01, 60e9);
      58           1 :   gpr_histogram_add(h, 10000);
      59           1 :   gpr_histogram_add(h, 10000);
      60           1 :   gpr_histogram_add(h, 11000);
      61           1 :   gpr_histogram_add(h, 11000);
      62             : 
      63           1 :   expect_percentile(h, 50, 10001, 10999);
      64           1 :   GPR_ASSERT(gpr_histogram_mean(h) == 10500);
      65             : 
      66           1 :   gpr_histogram_destroy(h);
      67           1 : }
      68             : 
      69           1 : static void test_percentile(void) {
      70             :   gpr_histogram *h;
      71             :   double last;
      72             :   double i;
      73             :   double cur;
      74             : 
      75           1 :   LOG_TEST("test_percentile");
      76             : 
      77           1 :   h = gpr_histogram_create(0.05, 1e9);
      78           1 :   gpr_histogram_add(h, 2.5);
      79           1 :   gpr_histogram_add(h, 2.5);
      80           1 :   gpr_histogram_add(h, 8);
      81           1 :   gpr_histogram_add(h, 4);
      82             : 
      83           1 :   GPR_ASSERT(gpr_histogram_count(h) == 4);
      84           1 :   GPR_ASSERT(gpr_histogram_minimum(h) == 2.5);
      85           1 :   GPR_ASSERT(gpr_histogram_maximum(h) == 8);
      86           1 :   GPR_ASSERT(gpr_histogram_sum(h) == 17);
      87           1 :   GPR_ASSERT(gpr_histogram_sum_of_squares(h) == 92.5);
      88           1 :   GPR_ASSERT(gpr_histogram_mean(h) == 4.25);
      89           1 :   GPR_ASSERT(gpr_histogram_variance(h) == 5.0625);
      90           1 :   GPR_ASSERT(gpr_histogram_stddev(h) == 2.25);
      91             : 
      92           1 :   expect_percentile(h, -10, 2.5, 2.5);
      93           1 :   expect_percentile(h, 0, 2.5, 2.5);
      94           1 :   expect_percentile(h, 12.5, 2.5, 2.5);
      95           1 :   expect_percentile(h, 25, 2.5, 2.5);
      96           1 :   expect_percentile(h, 37.5, 2.5, 2.8);
      97           1 :   expect_percentile(h, 50, 3.0, 3.5);
      98           1 :   expect_percentile(h, 62.5, 3.5, 4.5);
      99           1 :   expect_percentile(h, 75, 5, 7.9);
     100           1 :   expect_percentile(h, 100, 8, 8);
     101           1 :   expect_percentile(h, 110, 8, 8);
     102             : 
     103             :   /* test monotonicity */
     104           1 :   last = 0.0;
     105       10001 :   for (i = 0; i < 100.0; i += 0.01) {
     106       10000 :     cur = gpr_histogram_percentile(h, i);
     107       10000 :     GPR_ASSERT(cur >= last);
     108       10000 :     last = cur;
     109             :   }
     110             : 
     111           1 :   gpr_histogram_destroy(h);
     112           1 : }
     113             : 
     114           1 : static void test_merge(void) {
     115             :   gpr_histogram *h1, *h2;
     116             :   double last;
     117             :   double i;
     118             :   double cur;
     119             : 
     120           1 :   LOG_TEST("test_merge");
     121             : 
     122           1 :   h1 = gpr_histogram_create(0.05, 1e9);
     123           1 :   gpr_histogram_add(h1, 2.5);
     124           1 :   gpr_histogram_add(h1, 2.5);
     125           1 :   gpr_histogram_add(h1, 8);
     126           1 :   gpr_histogram_add(h1, 4);
     127             : 
     128           1 :   h2 = gpr_histogram_create(0.01, 1e9);
     129           1 :   GPR_ASSERT(gpr_histogram_merge(h1, h2) == 0);
     130           1 :   gpr_histogram_destroy(h2);
     131             : 
     132           1 :   h2 = gpr_histogram_create(0.05, 1e10);
     133           1 :   GPR_ASSERT(gpr_histogram_merge(h1, h2) == 0);
     134           1 :   gpr_histogram_destroy(h2);
     135             : 
     136           1 :   h2 = gpr_histogram_create(0.05, 1e9);
     137           1 :   GPR_ASSERT(gpr_histogram_merge(h1, h2) == 1);
     138           1 :   GPR_ASSERT(gpr_histogram_count(h1) == 4);
     139           1 :   GPR_ASSERT(gpr_histogram_minimum(h1) == 2.5);
     140           1 :   GPR_ASSERT(gpr_histogram_maximum(h1) == 8);
     141           1 :   GPR_ASSERT(gpr_histogram_sum(h1) == 17);
     142           1 :   GPR_ASSERT(gpr_histogram_sum_of_squares(h1) == 92.5);
     143           1 :   GPR_ASSERT(gpr_histogram_mean(h1) == 4.25);
     144           1 :   GPR_ASSERT(gpr_histogram_variance(h1) == 5.0625);
     145           1 :   GPR_ASSERT(gpr_histogram_stddev(h1) == 2.25);
     146           1 :   gpr_histogram_destroy(h2);
     147             : 
     148           1 :   h2 = gpr_histogram_create(0.05, 1e9);
     149           1 :   gpr_histogram_add(h2, 7.0);
     150           1 :   gpr_histogram_add(h2, 17.0);
     151           1 :   gpr_histogram_add(h2, 1.0);
     152           1 :   GPR_ASSERT(gpr_histogram_merge(h1, h2) == 1);
     153           1 :   GPR_ASSERT(gpr_histogram_count(h1) == 7);
     154           1 :   GPR_ASSERT(gpr_histogram_minimum(h1) == 1.0);
     155           1 :   GPR_ASSERT(gpr_histogram_maximum(h1) == 17.0);
     156           1 :   GPR_ASSERT(gpr_histogram_sum(h1) == 42.0);
     157           1 :   GPR_ASSERT(gpr_histogram_sum_of_squares(h1) == 431.5);
     158           1 :   GPR_ASSERT(gpr_histogram_mean(h1) == 6.0);
     159             : 
     160             :   /* test monotonicity */
     161           1 :   last = 0.0;
     162       10001 :   for (i = 0; i < 100.0; i += 0.01) {
     163       10000 :     cur = gpr_histogram_percentile(h1, i);
     164       10000 :     GPR_ASSERT(cur >= last);
     165       10000 :     last = cur;
     166             :   }
     167             : 
     168           1 :   gpr_histogram_destroy(h1);
     169           1 :   gpr_histogram_destroy(h2);
     170           1 : }
     171             : 
     172           1 : int main(void) {
     173           1 :   test_no_op();
     174           1 :   test_simple();
     175           1 :   test_percentile();
     176           1 :   test_merge();
     177           1 :   return 0;
     178             : }

Generated by: LCOV version 1.10