github.com/coyove/common@v0.0.0-20240403014525-f70e643f9de8/clock/clock_test.go (about)

     1  package clock
     2  
     3  import (
     4  	"sync"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  func TestNow(t *testing.T) {
    10  	for {
    11  		if time.Now().Nanosecond()/1e6 > 900 {
    12  			break
    13  		}
    14  	}
    15  
    16  	wg := sync.WaitGroup{}
    17  	m := sync.Map{}
    18  
    19  	for i := 0; i < 1e3; i++ {
    20  		wg.Add(1)
    21  		go func() {
    22  			for i := 0; i < 1e3; i++ {
    23  				n := Timestamp()
    24  				if _, ok := m.Load(n); ok {
    25  					t.Fatal(n)
    26  				}
    27  				m.Store(n, true)
    28  			}
    29  			wg.Done()
    30  		}()
    31  	}
    32  	wg.Wait()
    33  }
    34  
    35  func BenchmarkThumb(b *testing.B) {
    36  	for i := 0; i < b.N; i++ {
    37  		timeNow()
    38  	}
    39  }
    40  
    41  func BenchmarkTime(b *testing.B) {
    42  	for i := 0; i < b.N; i++ {
    43  		time.Now().UnixNano()
    44  	}
    45  }