github.com/nikandfor/hacked@v0.0.0-20231207014854-3b383967fdf4/htime/low_test.go (about)

     1  package htime
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  )
     7  
     8  func TestAfterFuncSync(t *testing.T) {
     9  	c := make(chan struct{})
    10  
    11  	setOk := func() {
    12  		close(c)
    13  	}
    14  
    15  	tt := NewTimerSync(time.Millisecond, setOk)
    16  	defer tt.Stop()
    17  
    18  	<-c
    19  }
    20  
    21  func TestMonotomicOf(t *testing.T) {
    22  	now := time.Now()
    23  
    24  	m := MonotonicOf(now)
    25  	if m == 0 {
    26  		t.Errorf("expected monotonic clock")
    27  	}
    28  
    29  	m = MonotonicOf(now.Truncate(-1))
    30  	if m != 0 {
    31  		t.Errorf("expected monotonic clock to be truncated")
    32  	}
    33  }
    34  
    35  func BenchmarkTimeNow(b *testing.B) {
    36  	for i := 0; i < b.N; i++ {
    37  		_ = time.Now()
    38  	}
    39  }
    40  
    41  func BenchmarkNow(b *testing.B) {
    42  	for i := 0; i < b.N; i++ {
    43  		_, _, _ = Now()
    44  	}
    45  }
    46  
    47  func BenchmarkTimeNowUnixNano(b *testing.B) {
    48  	for i := 0; i < b.N; i++ {
    49  		_ = time.Now().UnixNano()
    50  	}
    51  }
    52  
    53  func BenchmarkUnixNano(b *testing.B) {
    54  	for i := 0; i < b.N; i++ {
    55  		_ = UnixNano()
    56  	}
    57  }
    58  
    59  func BenchmarkDateAndClock(b *testing.B) {
    60  	t := time.Now()
    61  
    62  	for i := 0; i < b.N; i++ {
    63  		_, _, _ = t.Date()
    64  		_, _, _ = t.Clock()
    65  	}
    66  }
    67  
    68  func BenchmarkDateClock(b *testing.B) {
    69  	t := time.Now()
    70  
    71  	for i := 0; i < b.N; i++ {
    72  		_, _, _, _, _, _ = DateClock(t)
    73  	}
    74  }
    75  
    76  func BenchmarkMonotonicOf(b *testing.B) {
    77  	now := time.Now()
    78  
    79  	for i := 0; i < b.N; i++ {
    80  		_ = MonotonicOf(now)
    81  	}
    82  }
    83  
    84  func BenchmarkMono(b *testing.B) {
    85  	now := time.Now()
    86  
    87  	for i := 0; i < b.N; i++ {
    88  		_ = mono(&now)
    89  	}
    90  }