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

     1  package etime
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  )
     7  
     8  func TestNow(t *testing.T) {
     9  	if NowSecond() != uint32(time.Now().Unix()) {
    10  		t.FailNow()
    11  	}
    12  
    13  	ms1 := NowMillisecond()
    14  	ms2 := time.Now().UnixNano() / 1e6
    15  	if ms2 > ms1+1 || ms1 > ms2+1 {
    16  		t.FailNow()
    17  	}
    18  
    19  	micro1 := NowMicrosecond()
    20  	micro2 := time.Now().UnixNano() / 1e3
    21  	if micro1 > micro2+1 || micro2 > micro1+1 {
    22  		t.FailNow()
    23  	}
    24  
    25  	us1 := NowUs()
    26  	us2 := time.Now().UnixNano() / 1e3
    27  	if us1 > us2+1 || us2 > us1+1 {
    28  		t.FailNow()
    29  	}
    30  
    31  	ms1 = NowMs()
    32  	ms2 = time.Now().UnixNano() / 1e6
    33  	if ms2 > ms1+1 || ms1 > ms2+1 {
    34  		t.FailNow()
    35  	}
    36  
    37  	ns1 := NowNs()
    38  	ns2 := time.Now().UnixNano()
    39  	if ns1 > ns2 {
    40  		t.FailNow()
    41  	}
    42  }
    43  
    44  func TestUptimeMs(t *testing.T) {
    45  	us := UptimeUs()
    46  	ms := UptimeMs()
    47  	t.Log(us)
    48  	t.Log(ms)
    49  }
    50  
    51  func BenchmarkNowSecond(b *testing.B) {
    52  	b.ReportAllocs()
    53  	b.ResetTimer()
    54  	for i := 0; i < b.N; i++ {
    55  		_ = NowSecond()
    56  	}
    57  }
    58  
    59  func BenchmarkNowMillisecond(b *testing.B) {
    60  	b.ReportAllocs()
    61  	b.ResetTimer()
    62  	for i := 0; i < b.N; i++ {
    63  		_ = NowMillisecond()
    64  	}
    65  }
    66  
    67  func BenchmarkNowMicrosecond(b *testing.B) {
    68  	b.ReportAllocs()
    69  	b.ResetTimer()
    70  	for i := 0; i < b.N; i++ {
    71  		_ = NowMicrosecond()
    72  	}
    73  }
    74  
    75  func BenchmarkTimeNow(b *testing.B) {
    76  	b.ReportAllocs()
    77  	b.ResetTimer()
    78  	for i := 0; i < b.N; i++ {
    79  		_ = time.Now().Unix()
    80  	}
    81  }
    82  
    83  // BenchmarkNowSecond-12         	29296284	        39.8 ns/op	       0 B/op	       0 allocs/op
    84  // BenchmarkNowMillisecond-12    	29361312	        40.7 ns/op	       0 B/op	       0 allocs/op
    85  // BenchmarkNowMicrosecond-12    	29742286	        40.1 ns/op	       0 B/op	       0 allocs/op
    86  // BenchmarkTimeNow-12           	15010953	        70.0 ns/op	       0 B/op	       0 allocs/op