github.com/zerjioang/time32@v0.0.0-20211102104504-b756043b9843/time_test.go (about) 1 // 2 // Created by zerjioang 3 // https://github/zerjioang 4 // Copyright (c) 2020. All rights reserved. 5 // 6 // SPDX-License-Identifier: GPL-3.0 7 // 8 9 package time32 10 11 import ( 12 "encoding/binary" 13 "fmt" 14 "reflect" 15 "testing" 16 "time" 17 ) 18 19 func ExampleEpoch() { 20 fmt.Println(Epoch()) 21 } 22 23 func TestNow(t *testing.T) { 24 t.Run("custom-time", func(t *testing.T) { 25 tt := Now() 26 r := reflect.ValueOf(tt) 27 t.Log(r) 28 t.Log(binary.Size(tt)) 29 }) 30 t.Run("standard-go-time", func(t *testing.T) { 31 tt := time.Now() 32 r := reflect.ValueOf(tt) 33 t.Log(r) 34 t.Log(binary.Size(tt)) 35 }) 36 t.Run("custom-epoch", func(t *testing.T) { 37 tt := Epoch() 38 t.Log(binary.Size(tt)) 39 t.Log(tt) 40 }) 41 t.Run("standard-go-epoch", func(t *testing.T) { 42 tt := time.Now().Unix() 43 t.Log(binary.Size(tt)) 44 t.Log(tt) 45 }) 46 t.Run("both-epoch", func(t *testing.T) { 47 tt := time.Now().Unix() 48 t.Log(binary.Size(tt)) 49 t.Log(tt) 50 t2 := Epoch() 51 t.Log(binary.Size(t2)) 52 t.Log(t2) 53 }) 54 } 55 56 func BenchmarkNow(b *testing.B) { 57 // BenchmarkNow/epoch-custom-12 232 5111623 ns/op 0.00 MB/s 0 B/op 0 allocs/op 58 b.Run("epoch-custom", func(b *testing.B) { 59 b.ReportAllocs() 60 b.SetBytes(1) 61 b.ResetTimer() 62 var stamps [100000]Time32 63 for i := 0; i < b.N; i++ { 64 for i := 0; i < 100000; i++ { 65 stamps[i] = Epoch() 66 } 67 } 68 }) 69 //BenchmarkNow/epoch-standard-go-12 249 4805626 ns/op 0.00 MB/s 0 B/op 0 allocs/op 70 b.Run("epoch-standard-go", func(b *testing.B) { 71 b.ReportAllocs() 72 b.SetBytes(1) 73 b.ResetTimer() 74 var stamps [100000]int64 75 for i := 0; i < b.N; i++ { 76 for i := 0; i < 100000; i++ { 77 stamps[i] = time.Now().Unix() 78 } 79 } 80 }) 81 b.Run("custom", func(b *testing.B) { 82 b.ReportAllocs() 83 b.SetBytes(1) 84 b.ResetTimer() 85 for i := 0; i < b.N; i++ { 86 _ = Now() 87 } 88 }) 89 b.Run("standard-go-time", func(b *testing.B) { 90 b.ReportAllocs() 91 b.SetBytes(1) 92 b.ResetTimer() 93 for i := 0; i < b.N; i++ { 94 _ = Now() 95 } 96 }) 97 b.Run("custom-ref", func(b *testing.B) { 98 // make a benchmark in where compiler 99 // optimizations do not remove our variable 100 var tt Time 101 b.ReportAllocs() 102 b.SetBytes(1) 103 b.ResetTimer() 104 for i := 0; i < b.N; i++ { 105 tt = Now() 106 } 107 if tt.IsZero() { 108 b.Log("time is zero") 109 } 110 }) 111 b.Run("standard-go-time-ref", func(b *testing.B) { 112 // make a benchmark in where compiler 113 // optimizations do not remove our variable 114 var tt time.Time 115 b.ReportAllocs() 116 b.SetBytes(1) 117 b.ResetTimer() 118 for i := 0; i < b.N; i++ { 119 tt = time.Now() 120 } 121 if tt.IsZero() { 122 b.Log("time is zero") 123 } 124 }) 125 b.Run("reuse-time", func(b *testing.B) { 126 // make a benchmark in where compiler 127 // optimizations do not remove our variable 128 b.ReportAllocs() 129 b.SetBytes(1) 130 b.ResetTimer() 131 var ep time.Time 132 for i := 0; i < b.N; i++ { 133 ep = ReuseTime() 134 } 135 if ep.Unix() == 0 { 136 b.Log("time is zero") 137 } 138 }) 139 b.Run("reuse-unix", func(b *testing.B) { 140 // make a benchmark in where compiler 141 // optimizations do not remove our variable 142 b.ReportAllocs() 143 b.SetBytes(1) 144 b.ResetTimer() 145 var ep int64 146 for i := 0; i < b.N; i++ { 147 ep = ReuseUnix() 148 } 149 if ep == 0 { 150 b.Log("time is zero") 151 } 152 }) 153 b.Run("reuse-unixnano", func(b *testing.B) { 154 // make a benchmark in where compiler 155 // optimizations do not remove our variable 156 b.ReportAllocs() 157 b.SetBytes(1) 158 b.ResetTimer() 159 var ep int64 160 for i := 0; i < b.N; i++ { 161 ep = ReuseUnixNano() 162 } 163 if ep == 0 { 164 b.Log("time is zero") 165 } 166 }) 167 b.Run("reuse-time-unixnano", func(b *testing.B) { 168 // make a benchmark in where compiler 169 // optimizations do not remove our variable 170 b.ReportAllocs() 171 b.SetBytes(1) 172 b.ResetTimer() 173 var ep int64 174 for i := 0; i < b.N; i++ { 175 ep = ReuseTime().UnixNano() 176 } 177 if ep == 0 { 178 b.Log("time is zero") 179 } 180 }) 181 }