github.com/matrixorigin/matrixone@v1.2.0/pkg/util/metric/stats/counter_test.go (about)

     1  // Copyright 2023 Matrix Origin
     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  package stats
    16  
    17  import (
    18  	"github.com/stretchr/testify/assert"
    19  	"sync/atomic"
    20  	"testing"
    21  )
    22  
    23  func TestCounterLoad(t *testing.T) {
    24  	c := Counter{}
    25  
    26  	c.Add(1)
    27  	assert.Equal(t, int64(1), c.Load())
    28  
    29  	c.Add(1)
    30  	assert.Equal(t, int64(2), c.Load())
    31  
    32  	c.Add(10)
    33  	assert.Equal(t, int64(12), c.Load())
    34  }
    35  
    36  func TestCounterSwapW(t *testing.T) {
    37  	c := Counter{}
    38  
    39  	c.Add(1)
    40  	assert.Equal(t, int64(1), c.Load())
    41  
    42  	c.Add(1)
    43  	assert.Equal(t, int64(2), c.Load())
    44  
    45  	c.Add(2)
    46  	assert.Equal(t, int64(4), c.SwapW(0))
    47  
    48  	assert.Equal(t, int64(0), c.SwapW(0))
    49  
    50  	c.Add(2)
    51  	assert.Equal(t, int64(6), c.Load())
    52  }
    53  
    54  func TestCounterLoadW(t *testing.T) {
    55  	c := Counter{}
    56  
    57  	c.Add(1)
    58  	assert.Equal(t, int64(1), c.Load())
    59  	assert.Equal(t, int64(1), c.LoadW())
    60  
    61  	c.Add(1)
    62  	assert.Equal(t, int64(2), c.Load())
    63  	assert.Equal(t, int64(2), c.LoadW())
    64  
    65  	assert.Equal(t, int64(2), c.SwapW(0))
    66  
    67  	assert.Equal(t, int64(2), c.Load())
    68  	assert.Equal(t, int64(0), c.LoadW())
    69  
    70  	c.Add(1)
    71  	assert.Equal(t, int64(3), c.Load())
    72  	assert.Equal(t, int64(1), c.LoadW())
    73  }
    74  
    75  func BenchmarkAdd(b *testing.B) {
    76  	b.Run("atomic.Int64", func(b *testing.B) {
    77  		c := atomic.Int64{}
    78  		for i := 0; i < b.N; i++ {
    79  			c.Add(int64(i))
    80  		}
    81  	})
    82  
    83  	b.Run("Counter", func(b *testing.B) {
    84  		c := Counter{}
    85  		for i := 0; i < b.N; i++ {
    86  			c.Add(int64(i))
    87  		}
    88  	})
    89  }
    90  
    91  func BenchmarkAddLoad(b *testing.B) {
    92  	b.Run("atomic.Int64", func(b *testing.B) {
    93  		c := atomic.Int64{}
    94  		for i := 0; i < b.N; i++ {
    95  			c.Add(1)
    96  			if int64(i+1) != c.Load() {
    97  				panic("Load() did not return global sum")
    98  			}
    99  		}
   100  	})
   101  
   102  	b.Run("Counter", func(b *testing.B) {
   103  		c := Counter{}
   104  		for i := 0; i < b.N; i++ {
   105  			c.Add(1)
   106  			if int64(i+1) != c.Load() {
   107  				panic("Load() did not return global sum")
   108  			}
   109  		}
   110  	})
   111  }