LCOV - code coverage report
Current view: top level - test/core/iomgr - time_averaged_stats_test.c (source / functions) Hit Total Coverage
Test: tmp.zDYK9MVh93 Lines: 120 120 100.0 %
Date: 2015-10-10 Functions: 9 9 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 "src/core/iomgr/time_averaged_stats.h"
      35             : 
      36             : #include <math.h>
      37             : 
      38             : #include <grpc/support/log.h>
      39             : #include "test/core/util/test_config.h"
      40             : 
      41             : #define EXPECT_EQ(a, b) GPR_ASSERT((a) == (b))
      42             : #define EXPECT_DOUBLE_EQ(a, b) GPR_ASSERT(fabs((a) - (b)) < 1e-9)
      43             : 
      44           1 : static void no_regress_no_persist_test_1(void) {
      45             :   grpc_time_averaged_stats tas;
      46           1 :   grpc_time_averaged_stats_init(&tas, 1000, 0, 0.0);
      47           1 :   EXPECT_DOUBLE_EQ(1000, tas.aggregate_weighted_avg);
      48           1 :   EXPECT_DOUBLE_EQ(0, tas.aggregate_total_weight);
      49             : 
      50             :   /* Should have no effect */
      51           1 :   grpc_time_averaged_stats_update_average(&tas);
      52           1 :   EXPECT_DOUBLE_EQ(1000, tas.aggregate_weighted_avg);
      53           1 :   EXPECT_DOUBLE_EQ(0, tas.aggregate_total_weight);
      54             : 
      55             :   /* Should replace old average */
      56           1 :   grpc_time_averaged_stats_add_sample(&tas, 2000);
      57           1 :   grpc_time_averaged_stats_update_average(&tas);
      58           1 :   EXPECT_DOUBLE_EQ(2000, tas.aggregate_weighted_avg);
      59           1 :   EXPECT_DOUBLE_EQ(1, tas.aggregate_total_weight);
      60           1 : }
      61             : 
      62           1 : static void no_regress_no_persist_test_2(void) {
      63             :   grpc_time_averaged_stats tas;
      64           1 :   grpc_time_averaged_stats_init(&tas, 1000, 0, 0.0);
      65           1 :   EXPECT_DOUBLE_EQ(1000, tas.aggregate_weighted_avg);
      66             :   /* Should replace init value */
      67           1 :   grpc_time_averaged_stats_add_sample(&tas, 2000);
      68           1 :   grpc_time_averaged_stats_update_average(&tas);
      69           1 :   EXPECT_DOUBLE_EQ(2000, tas.aggregate_weighted_avg);
      70           1 :   EXPECT_DOUBLE_EQ(1, tas.aggregate_total_weight);
      71             : 
      72           1 :   grpc_time_averaged_stats_add_sample(&tas, 3000);
      73           1 :   grpc_time_averaged_stats_update_average(&tas);
      74           1 :   EXPECT_DOUBLE_EQ(3000, tas.aggregate_weighted_avg);
      75           1 :   EXPECT_DOUBLE_EQ(1, tas.aggregate_total_weight);
      76           1 : }
      77             : 
      78           1 : static void no_regress_no_persist_test_3(void) {
      79             :   grpc_time_averaged_stats tas;
      80           1 :   grpc_time_averaged_stats_init(&tas, 1000, 0, 0.0);
      81           1 :   EXPECT_DOUBLE_EQ(1000, tas.aggregate_weighted_avg);
      82             :   /* Should replace init value */
      83           1 :   grpc_time_averaged_stats_add_sample(&tas, 2500);
      84           1 :   grpc_time_averaged_stats_update_average(&tas);
      85           1 :   EXPECT_DOUBLE_EQ(2500, tas.aggregate_weighted_avg);
      86           1 :   EXPECT_DOUBLE_EQ(1, tas.aggregate_total_weight);
      87             : 
      88           1 :   grpc_time_averaged_stats_add_sample(&tas, 3500);
      89           1 :   grpc_time_averaged_stats_add_sample(&tas, 4500);
      90           1 :   grpc_time_averaged_stats_update_average(&tas);
      91           1 :   EXPECT_DOUBLE_EQ(4000, tas.aggregate_weighted_avg);
      92           1 :   EXPECT_DOUBLE_EQ(2, tas.aggregate_total_weight);
      93           1 : }
      94             : 
      95           1 : static void some_regress_no_persist_test(void) {
      96             :   grpc_time_averaged_stats tas;
      97           1 :   grpc_time_averaged_stats_init(&tas, 1000, 0.5, 0.0);
      98           1 :   EXPECT_DOUBLE_EQ(1000, tas.aggregate_weighted_avg);
      99           1 :   EXPECT_DOUBLE_EQ(0, tas.aggregate_total_weight);
     100           1 :   grpc_time_averaged_stats_add_sample(&tas, 2000);
     101           1 :   grpc_time_averaged_stats_add_sample(&tas, 2000);
     102           1 :   grpc_time_averaged_stats_update_average(&tas);
     103             :   /* (2 * 2000 + 0.5 * 1000) / 2.5 */
     104           1 :   EXPECT_DOUBLE_EQ(1800, tas.aggregate_weighted_avg);
     105           1 :   EXPECT_DOUBLE_EQ(2.5, tas.aggregate_total_weight);
     106           1 : }
     107             : 
     108           1 : static void some_decay_test(void) {
     109             :   grpc_time_averaged_stats tas;
     110           1 :   grpc_time_averaged_stats_init(&tas, 1000, 1, 0.0);
     111           1 :   EXPECT_EQ(1000, tas.aggregate_weighted_avg);
     112             :   /* Should avg with init value */
     113           1 :   grpc_time_averaged_stats_add_sample(&tas, 2000);
     114           1 :   grpc_time_averaged_stats_update_average(&tas);
     115           1 :   EXPECT_DOUBLE_EQ(1500, tas.aggregate_weighted_avg);
     116           1 :   EXPECT_DOUBLE_EQ(2, tas.aggregate_total_weight);
     117             : 
     118           1 :   grpc_time_averaged_stats_add_sample(&tas, 2000);
     119           1 :   grpc_time_averaged_stats_update_average(&tas);
     120           1 :   EXPECT_DOUBLE_EQ(1500, tas.aggregate_weighted_avg);
     121           1 :   EXPECT_DOUBLE_EQ(2, tas.aggregate_total_weight);
     122             : 
     123           1 :   grpc_time_averaged_stats_add_sample(&tas, 2000);
     124           1 :   grpc_time_averaged_stats_update_average(&tas);
     125           1 :   EXPECT_DOUBLE_EQ(1500, tas.aggregate_weighted_avg);
     126           1 :   EXPECT_DOUBLE_EQ(2, tas.aggregate_total_weight);
     127           1 : }
     128             : 
     129           1 : static void no_regress_full_persist_test(void) {
     130             :   grpc_time_averaged_stats tas;
     131           1 :   grpc_time_averaged_stats_init(&tas, 1000, 0, 1.0);
     132           1 :   EXPECT_DOUBLE_EQ(1000, tas.aggregate_weighted_avg);
     133           1 :   EXPECT_DOUBLE_EQ(0, tas.aggregate_total_weight);
     134             : 
     135             :   /* Should replace init value */
     136           1 :   grpc_time_averaged_stats_add_sample(&tas, 2000);
     137           1 :   grpc_time_averaged_stats_update_average(&tas);
     138           1 :   EXPECT_EQ(2000, tas.aggregate_weighted_avg);
     139           1 :   EXPECT_EQ(1, tas.aggregate_total_weight);
     140             : 
     141             :   /* Will result in average of the 3 samples. */
     142           1 :   grpc_time_averaged_stats_add_sample(&tas, 2300);
     143           1 :   grpc_time_averaged_stats_add_sample(&tas, 2300);
     144           1 :   grpc_time_averaged_stats_update_average(&tas);
     145           1 :   EXPECT_DOUBLE_EQ(2200, tas.aggregate_weighted_avg);
     146           1 :   EXPECT_DOUBLE_EQ(3, tas.aggregate_total_weight);
     147           1 : }
     148             : 
     149           1 : static void no_regress_some_persist_test(void) {
     150             :   grpc_time_averaged_stats tas;
     151           1 :   grpc_time_averaged_stats_init(&tas, 1000, 0, 0.5);
     152             :   /* Should replace init value */
     153           1 :   grpc_time_averaged_stats_add_sample(&tas, 2000);
     154           1 :   grpc_time_averaged_stats_update_average(&tas);
     155           1 :   EXPECT_DOUBLE_EQ(2000, tas.aggregate_weighted_avg);
     156           1 :   EXPECT_DOUBLE_EQ(1, tas.aggregate_total_weight);
     157             : 
     158           1 :   grpc_time_averaged_stats_add_sample(&tas, 2500);
     159           1 :   grpc_time_averaged_stats_add_sample(&tas, 4000);
     160           1 :   grpc_time_averaged_stats_update_average(&tas);
     161           1 :   EXPECT_DOUBLE_EQ(3000, tas.aggregate_weighted_avg);
     162           1 :   EXPECT_DOUBLE_EQ(2.5, tas.aggregate_total_weight);
     163           1 : }
     164             : 
     165           1 : static void some_regress_some_persist_test(void) {
     166             :   grpc_time_averaged_stats tas;
     167           1 :   grpc_time_averaged_stats_init(&tas, 1000, 0.4, 0.6);
     168             :   /* Sample weight = 0 */
     169           1 :   EXPECT_EQ(1000, tas.aggregate_weighted_avg);
     170           1 :   EXPECT_EQ(0, tas.aggregate_total_weight);
     171             : 
     172           1 :   grpc_time_averaged_stats_update_average(&tas);
     173             :   /* (0.6 * 0 * 1000 + 0.4 * 1000 / 0.4) */
     174           1 :   EXPECT_DOUBLE_EQ(1000, tas.aggregate_weighted_avg);
     175           1 :   EXPECT_DOUBLE_EQ(0.4, tas.aggregate_total_weight);
     176             : 
     177           1 :   grpc_time_averaged_stats_add_sample(&tas, 2640);
     178           1 :   grpc_time_averaged_stats_update_average(&tas);
     179             :   /* (1 * 2640 + 0.6 * 0.4 * 1000 + 0.4 * 1000 / (1 + 0.6 * 0.4 + 0.4) */
     180           1 :   EXPECT_DOUBLE_EQ(2000, tas.aggregate_weighted_avg);
     181           1 :   EXPECT_DOUBLE_EQ(1.64, tas.aggregate_total_weight);
     182             : 
     183           1 :   grpc_time_averaged_stats_add_sample(&tas, 2876.8);
     184           1 :   grpc_time_averaged_stats_update_average(&tas);
     185             :   /* (1 * 2876.8 + 0.6 * 1.64 * 2000 + 0.4 * 1000 / (1 + 0.6 * 1.64 + 0.4) */
     186           1 :   EXPECT_DOUBLE_EQ(2200, tas.aggregate_weighted_avg);
     187           1 :   EXPECT_DOUBLE_EQ(2.384, tas.aggregate_total_weight);
     188             : 
     189           1 :   grpc_time_averaged_stats_add_sample(&tas, 4944.32);
     190           1 :   grpc_time_averaged_stats_update_average(&tas);
     191             :   /* (1 * 4944.32 + 0.6 * 2.384 * 2200 + 0.4 * 1000) /
     192             :      (1 + 0.6 * 2.384 + 0.4) */
     193           1 :   EXPECT_DOUBLE_EQ(3000, tas.aggregate_weighted_avg);
     194           1 :   EXPECT_DOUBLE_EQ(2.8304, tas.aggregate_total_weight);
     195           1 : }
     196             : 
     197           1 : int main(int argc, char **argv) {
     198           1 :   grpc_test_init(argc, argv);
     199           1 :   no_regress_no_persist_test_1();
     200           1 :   no_regress_no_persist_test_2();
     201           1 :   no_regress_no_persist_test_3();
     202           1 :   some_regress_no_persist_test();
     203           1 :   some_decay_test();
     204           1 :   no_regress_full_persist_test();
     205           1 :   no_regress_some_persist_test();
     206           1 :   some_regress_some_persist_test();
     207           1 :   return 0;
     208             : }

Generated by: LCOV version 1.10