github.com/thanos-io/thanos@v0.32.5/internal/cortex/querier/stats/stats_test.go (about)

     1  // Copyright (c) The Cortex Authors.
     2  // Licensed under the Apache License 2.0.
     3  
     4  package stats
     5  
     6  import (
     7  	"context"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestStats_WallTime(t *testing.T) {
    15  	t.Run("add and load wall time", func(t *testing.T) {
    16  		stats, _ := ContextWithEmptyStats(context.Background())
    17  		stats.AddWallTime(time.Second)
    18  		stats.AddWallTime(time.Second)
    19  
    20  		assert.Equal(t, 2*time.Second, stats.LoadWallTime())
    21  	})
    22  
    23  	t.Run("add and load wall time nil receiver", func(t *testing.T) {
    24  		var stats *Stats
    25  		stats.AddWallTime(time.Second)
    26  
    27  		assert.Equal(t, time.Duration(0), stats.LoadWallTime())
    28  	})
    29  }
    30  
    31  func TestStats_AddFetchedSeries(t *testing.T) {
    32  	t.Run("add and load series", func(t *testing.T) {
    33  		stats, _ := ContextWithEmptyStats(context.Background())
    34  		stats.AddFetchedSeries(100)
    35  		stats.AddFetchedSeries(50)
    36  
    37  		assert.Equal(t, uint64(150), stats.LoadFetchedSeries())
    38  	})
    39  
    40  	t.Run("add and load series nil receiver", func(t *testing.T) {
    41  		var stats *Stats
    42  		stats.AddFetchedSeries(50)
    43  
    44  		assert.Equal(t, uint64(0), stats.LoadFetchedSeries())
    45  	})
    46  }
    47  
    48  func TestStats_AddFetchedChunkBytes(t *testing.T) {
    49  	t.Run("add and load bytes", func(t *testing.T) {
    50  		stats, _ := ContextWithEmptyStats(context.Background())
    51  		stats.AddFetchedChunkBytes(4096)
    52  		stats.AddFetchedChunkBytes(4096)
    53  
    54  		assert.Equal(t, uint64(8192), stats.LoadFetchedChunkBytes())
    55  	})
    56  
    57  	t.Run("add and load bytes nil receiver", func(t *testing.T) {
    58  		var stats *Stats
    59  		stats.AddFetchedChunkBytes(1024)
    60  
    61  		assert.Equal(t, uint64(0), stats.LoadFetchedChunkBytes())
    62  	})
    63  }
    64  
    65  func TestStats_Merge(t *testing.T) {
    66  	t.Run("merge two stats objects", func(t *testing.T) {
    67  		stats1 := &Stats{}
    68  		stats1.AddWallTime(time.Millisecond)
    69  		stats1.AddFetchedSeries(50)
    70  		stats1.AddFetchedChunkBytes(42)
    71  
    72  		stats2 := &Stats{}
    73  		stats2.AddWallTime(time.Second)
    74  		stats2.AddFetchedSeries(60)
    75  		stats2.AddFetchedChunkBytes(100)
    76  
    77  		stats1.Merge(stats2)
    78  
    79  		assert.Equal(t, 1001*time.Millisecond, stats1.LoadWallTime())
    80  		assert.Equal(t, uint64(110), stats1.LoadFetchedSeries())
    81  		assert.Equal(t, uint64(142), stats1.LoadFetchedChunkBytes())
    82  	})
    83  
    84  	t.Run("merge two nil stats objects", func(t *testing.T) {
    85  		var stats1 *Stats
    86  		var stats2 *Stats
    87  
    88  		stats1.Merge(stats2)
    89  
    90  		assert.Equal(t, time.Duration(0), stats1.LoadWallTime())
    91  		assert.Equal(t, uint64(0), stats1.LoadFetchedSeries())
    92  		assert.Equal(t, uint64(0), stats1.LoadFetchedChunkBytes())
    93  	})
    94  }