github.com/aristanetworks/goarista@v0.0.0-20240514173732-cca2755bbd44/monitor/stats/histogram_test.go (about)

     1  // Copyright (c) 2017 Arista Networks, Inc.
     2  // Use of this source code is governed by the Apache License 2.0
     3  // that can be found in the COPYING file.
     4  
     5  package stats_test
     6  
     7  import (
     8  	"bytes"
     9  	"encoding/json"
    10  	"testing"
    11  
    12  	"github.com/aristanetworks/goarista/monitor/stats"
    13  )
    14  
    15  var expected0 = []byte(`{
    16    "stats": {
    17      "count": 0,
    18      "min": 0,
    19      "max": 0,
    20      "avg": 0.00
    21    },
    22    "buckets": [
    23      {
    24        "range": "[0,20)",
    25        "count": 0,
    26        "percentage": 0.0
    27      },
    28      {
    29        "range": "[20,40)",
    30        "count": 0,
    31        "percentage": 0.0
    32      },
    33      {
    34        "range": "[40,60)",
    35        "count": 0,
    36        "percentage": 0.0
    37      },
    38      {
    39        "range": "[60,80)",
    40        "count": 0,
    41        "percentage": 0.0
    42      },
    43      {
    44        "range": "[80,100)",
    45        "count": 0,
    46        "percentage": 0.0
    47      },
    48      {
    49        "range": "[100,120)",
    50        "count": 0,
    51        "percentage": 0.0
    52      },
    53      {
    54        "range": "[120,140)",
    55        "count": 0,
    56        "percentage": 0.0
    57      },
    58      {
    59        "range": "[140,160)",
    60        "count": 0,
    61        "percentage": 0.0
    62      },
    63      {
    64        "range": "[160,180)",
    65        "count": 0,
    66        "percentage": 0.0
    67      },
    68      {
    69        "range": "[180,inf)",
    70        "count": 0,
    71        "percentage": 0.0
    72      }
    73    ]
    74  }
    75  `)
    76  
    77  var expected42 = []byte(`{
    78    "stats": {
    79      "count": 1,
    80      "min": 42,
    81      "max": 42,
    82      "avg": 42.00
    83    },
    84    "buckets": [
    85      {
    86        "range": "[0,20)",
    87        "count": 0,
    88        "percentage": 0.0
    89      },
    90      {
    91        "range": "[20,40)",
    92        "count": 0,
    93        "percentage": 0.0
    94      },
    95      {
    96        "range": "[40,60)",
    97        "count": 1,
    98        "percentage": 100.0
    99      },
   100      {
   101        "range": "[60,80)",
   102        "count": 0,
   103        "percentage": 0.0
   104      },
   105      {
   106        "range": "[80,100)",
   107        "count": 0,
   108        "percentage": 0.0
   109      },
   110      {
   111        "range": "[100,120)",
   112        "count": 0,
   113        "percentage": 0.0
   114      },
   115      {
   116        "range": "[120,140)",
   117        "count": 0,
   118        "percentage": 0.0
   119      },
   120      {
   121        "range": "[140,160)",
   122        "count": 0,
   123        "percentage": 0.0
   124      },
   125      {
   126        "range": "[160,180)",
   127        "count": 0,
   128        "percentage": 0.0
   129      },
   130      {
   131        "range": "[180,inf)",
   132        "count": 0,
   133        "percentage": 0.0
   134      }
   135    ]
   136  }
   137  `)
   138  
   139  func testJSON(t *testing.T, h *stats.Histogram, exp []byte) {
   140  	var buf bytes.Buffer
   141  	enc := json.NewEncoder(&buf)
   142  	enc.SetIndent("", "  ")
   143  	err := enc.Encode(h.Value())
   144  	if err != nil {
   145  		t.Fatal(err)
   146  	}
   147  	if !bytes.Equal(buf.Bytes(), exp) {
   148  		t.Error("unexpected json")
   149  		t.Errorf("Expected: %s", exp)
   150  		t.Errorf("Got: %s", buf.Bytes())
   151  	}
   152  	var v interface{}
   153  	err = json.Unmarshal(buf.Bytes(), &v)
   154  	if err != nil {
   155  		t.Errorf("Failed to parse JSON: %s\nJSON was: %s", err, buf.Bytes())
   156  	}
   157  }
   158  
   159  // Ensure we can JSONify the histogram into valid JSON.
   160  func TestJSON(t *testing.T) {
   161  	h := stats.NewHistogram(
   162  		stats.HistogramOptions{NumBuckets: 10, GrowthFactor: 0,
   163  			SmallestBucketSize: 20, MinValue: 0})
   164  	testJSON(t, h, expected0)
   165  	if err := h.Add(42); err != nil {
   166  		t.Fatal(err)
   167  	}
   168  	testJSON(t, h, expected42)
   169  }