github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/store/metrics/histogram_test.go (about)

     1  // Copyright 2019 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // This file incorporates work covered by the following copyright and
    16  // permission notice:
    17  //
    18  // Copyright 2017 Attic Labs, Inc. All rights reserved.
    19  // Licensed under the Apache License, version 2.0:
    20  // http://www.apache.org/licenses/LICENSE-2.0
    21  
    22  package metrics
    23  
    24  import (
    25  	"testing"
    26  	"unsafe"
    27  
    28  	"github.com/stretchr/testify/assert"
    29  )
    30  
    31  func TestHistogramBucketValue(t *testing.T) {
    32  	assert := assert.New(t)
    33  
    34  	h := Histogram{}
    35  	assert.Equal(uint64(1<<0), h.bucketVal(0))
    36  	assert.Equal(uint64(1<<1), h.bucketVal(1))
    37  	assert.Equal(uint64(1<<2), h.bucketVal(2))
    38  	assert.Equal(uint64(1<<32), h.bucketVal(32))
    39  	assert.Equal(uint64(1<<40), h.bucketVal(40))
    40  }
    41  
    42  func TestHistogramBasic(t *testing.T) {
    43  	assert := assert.New(t)
    44  
    45  	h := Histogram{}
    46  
    47  	h.Sample(1)
    48  	h.Sample(1)
    49  	assert.Equal(uint64(2), h.buckets[0])
    50  
    51  	h.Sample(2)
    52  	h.Sample(3)
    53  	assert.Equal(uint64(2), h.buckets[1])
    54  
    55  	h.Sample(4)
    56  	h.Sample(5)
    57  	h.Sample(6)
    58  	assert.Equal(uint64(3), h.buckets[2])
    59  
    60  	h.Sample(256)
    61  	h.Sample(300)
    62  	h.Sample(500)
    63  	h.Sample(511)
    64  	assert.Equal(uint64(4), h.buckets[8])
    65  	assert.Equal(uint64(11), h.Samples())
    66  	assert.Equal(uint64(1589), h.Sum())
    67  	assert.Equal(uint64(144), h.Mean())
    68  }
    69  
    70  func TestHistogramLarge(t *testing.T) {
    71  	assert := assert.New(t)
    72  	h := Histogram{}
    73  	h.Sample(0xfffffffffffffe30)
    74  	assert.Equal(uint64(1), h.Samples())
    75  	assert.Equal(uint64(0xfffffffffffffe30), h.Sum())
    76  }
    77  
    78  func TestHistogramAdd(t *testing.T) {
    79  	assert := assert.New(t)
    80  
    81  	h := &Histogram{}
    82  	h.Sample(1)
    83  	h.Sample(2)
    84  	h.Sample(10)
    85  
    86  	h2 := &Histogram{}
    87  	h2.Sample(3)
    88  	h2.Sample(1073741854)
    89  
    90  	h.Add(h2)
    91  	assert.Equal(uint64(5), h.Samples())
    92  	assert.Equal(uint64(1073741870), h.Sum())
    93  	assert.Equal(uint64(1073741870)/uint64(5), h.Mean())
    94  }
    95  
    96  func TestHistogramString(t *testing.T) {
    97  	assert := assert.New(t)
    98  
    99  	h := &Histogram{}
   100  	h.Sample(1)
   101  	h.Sample(2)
   102  	h.Sample(10)
   103  	h.Sample(3034030343)
   104  
   105  	assert.Equal("Mean: 758507589, Sum: 3034030356, Samples: 4", h.String())
   106  
   107  	th := NewTimeHistogram()
   108  	th.Add(h)
   109  	assert.Equal("Mean: 758.507589ms, Sum: 3.034030356s, Samples: 4", th.String())
   110  
   111  	bh := NewByteHistogram()
   112  	bh.Add(h)
   113  	assert.Equal("Mean: 758 MB, Sum: 3.0 GB, Samples: 4", bh.String())
   114  }
   115  
   116  func TestHistogramStructSize(t *testing.T) {
   117  	size := unsafe.Sizeof(Histogram{})
   118  	assert.True(t, size%8 == 0)
   119  }