github.com/GoWebProd/gip@v0.0.0-20230623090727-b60d41d5d320/fasttime/fasttime_test.go (about)

     1  package fasttime
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  	"unsafe"
     7  )
     8  
     9  func BenchmarkMyNow(b *testing.B) {
    10  	for i := 0; i < b.N; i++ {
    11  		_ = Now()
    12  	}
    13  }
    14  
    15  func BenchmarkTimeNow(b *testing.B) {
    16  	for i := 0; i < b.N; i++ {
    17  		_ = time.Now()
    18  	}
    19  }
    20  
    21  func BenchmarkMyRound(b *testing.B) {
    22  	t := Now()
    23  	p := int64(time.Hour / time.Second)
    24  	for i := 0; i < b.N; i++ {
    25  		_ = Round(t, p)
    26  	}
    27  }
    28  
    29  func BenchmarkTimeRound(b *testing.B) {
    30  	t := time.Now()
    31  	p := time.Hour
    32  	for i := 0; i < b.N; i++ {
    33  		_ = t.Round(p)
    34  	}
    35  }
    36  
    37  func BenchmarkMyNowRound(b *testing.B) {
    38  	p := int64(time.Hour / time.Second)
    39  	for i := 0; i < b.N; i++ {
    40  		_ = Round(Now(), p)
    41  	}
    42  }
    43  
    44  func BenchmarkTimeNowRound(b *testing.B) {
    45  	p := time.Hour
    46  	for i := 0; i < b.N; i++ {
    47  		_ = time.Now().Round(p)
    48  	}
    49  }
    50  
    51  func BenchmarkMyDate(b *testing.B) {
    52  	t := Now()
    53  	for i := 0; i < b.N; i++ {
    54  		_, _, _ = ParseDate(t)
    55  	}
    56  }
    57  
    58  func BenchmarkTimeDate(b *testing.B) {
    59  	t := time.Now()
    60  	for i := 0; i < b.N; i++ {
    61  		_, _, _ = t.Date()
    62  	}
    63  }
    64  
    65  func BenchmarkMyFormat(b *testing.B) {
    66  	for i := 0; i < b.N; i++ {
    67  		year, month, day := ParseDate(Now())
    68  		month += 1
    69  
    70  		_ = FormatDate(year, month, day)
    71  	}
    72  }
    73  
    74  func BenchmarkTimeFormat(b *testing.B) {
    75  	for i := 0; i < b.N; i++ {
    76  		_ = time.Now().Format("20060102")
    77  	}
    78  }
    79  
    80  func BenchmarkMyTotal(b *testing.B) {
    81  	p := int64(time.Hour / time.Second)
    82  	for i := 0; i < b.N; i++ {
    83  		year, month, day := ParseDate(Round(Now(), p))
    84  		month += 1
    85  
    86  		_ = FormatDate(year, month, day)
    87  	}
    88  }
    89  
    90  func BenchmarkTimeTotal(b *testing.B) {
    91  	p := time.Hour
    92  	for i := 0; i < b.N; i++ {
    93  		_ = time.Now().Round(p).Format("20060102")
    94  	}
    95  }
    96  
    97  func TestDate(t *testing.T) {
    98  	t1 := time.Now()
    99  	t2 := Now()
   100  
   101  	y1, m1, d1 := t1.Date()
   102  	y2, m2, d2 := ParseDate(t2)
   103  
   104  	if int64(y1) != y2 {
   105  		t.Fatal("year:", y1, y2)
   106  	}
   107  	if int64(m1)-1 != m2 {
   108  		t.Fatal("month:", int64(m1)-1, m2)
   109  	}
   110  	if int64(d1) != d2 {
   111  		t.Fatal("day:", d1, d2)
   112  	}
   113  }
   114  
   115  func TestTime(t *testing.T) {
   116  	t1 := time.Now().UnixNano()
   117  	now := NowNano()
   118  
   119  	type time struct {
   120  		wall uint64
   121  		ext  int64
   122  	}
   123  
   124  	t2 := (*time)(unsafe.Pointer(&t1))
   125  
   126  	if t2.ext-now > 1000 {
   127  		t.Fatalf("now: %d, wall: %d", now, t2.wall)
   128  	}
   129  }