github.com/go-eden/common@v0.1.15-0.20210617133546-059099253264/esync/sync_atomic_float64_test.go (about)

     1  package esync
     2  
     3  import (
     4  	"github.com/stretchr/testify/assert"
     5  	"math/rand"
     6  	"testing"
     7  )
     8  
     9  func TestAtomicFloat64(t *testing.T) {
    10  	var a AtomicFloat64
    11  
    12  	for i := 0; i < 1000; i++ {
    13  		f := rand.Float64()
    14  		a.Set(f)
    15  		assert.True(t, a.Get() == f)
    16  		assert.True(t, a.Swap(0) == f, a.Get() == 0)
    17  	}
    18  	multiRun(10, func() {
    19  		for i := 0; i < 1000000; i++ {
    20  			a.Set(rand.Float64())
    21  			a.Get()
    22  		}
    23  	})
    24  	t.Log(a.String())
    25  }
    26  
    27  // BenchmarkAtomicFloat64_Set-12    	183620686	         6.208 ns/op
    28  func BenchmarkAtomicFloat64_Set(b *testing.B) {
    29  	var a AtomicFloat64
    30  	for i := 0; i < b.N; i++ {
    31  		a.Set(float64(i))
    32  	}
    33  }
    34  
    35  // BenchmarkAtomicFloat64_Get-12    	1000000000	         0.5715 ns/op
    36  func BenchmarkAtomicFloat64_Get(b *testing.B) {
    37  	var a AtomicFloat64
    38  	for i := 0; i < b.N; i++ {
    39  		_ = a.Get()
    40  	}
    41  }
    42  
    43  // BenchmarkAtomicFloat64_Swap-12    	187972417	         6.227 ns/op
    44  func BenchmarkAtomicFloat64_Swap(b *testing.B) {
    45  	var a AtomicFloat64
    46  	for i := 0; i < b.N; i++ {
    47  		_ = a.Swap(float64(i))
    48  	}
    49  }