Skip to content

Commit 8f9c71a

Browse files
committed
rename file and add some test case
1 parent 78c3206 commit 8f9c71a

File tree

5 files changed

+68
-23
lines changed

5 files changed

+68
-23
lines changed

mysql/util.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
mrand "math/rand"
1414
"runtime"
1515
"strings"
16+
"time"
1617

1718
"github.com/Masterminds/semver"
1819
"github.com/go-mysql-org/go-mysql/utils"
@@ -132,7 +133,7 @@ func RandomBuf(size int) []byte {
132133
// When this project supports golang 1.20 as a minimum, then this mrand.New(...)
133134
// line can be eliminated and the random number can be generated by simply
134135
// calling mrand.Intn()
135-
random := mrand.New(mrand.NewSource(utils.Now().UTC().UnixNano()))
136+
random := mrand.New(mrand.NewSource(time.Now().UTC().UnixNano()))
136137
min, max := 30, 127
137138
for i := 0; i < size; i++ {
138139
buf[i] = byte(min + random.Intn(max-min))

utils/now.go

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,7 @@
1-
//go:build linux || darwin
1+
//go:build !unix
22

33
package utils
44

5-
import (
6-
"syscall"
7-
"time"
8-
)
5+
import "time"
96

10-
// Now is a faster method to get current time
11-
func Now() time.Time {
12-
var tv syscall.Timeval
13-
if err := syscall.Gettimeofday(&tv); nil != err {
14-
// If it failed at syscall, use time package instead
15-
return time.Now()
16-
}
17-
18-
return time.Unix(0, syscall.TimevalToNsec(tv))
19-
}
7+
var Now = time.Now

utils/now2.go

Lines changed: 0 additions & 7 deletions
This file was deleted.

utils/now_unix.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//go:build unix
2+
3+
package utils
4+
5+
import (
6+
"syscall"
7+
"time"
8+
)
9+
10+
// Now is a faster method to get current time
11+
func Now() time.Time {
12+
var tv syscall.Timeval
13+
if err := syscall.Gettimeofday(&tv); nil != err {
14+
// If it failed at syscall, use time package instead
15+
return time.Now()
16+
}
17+
18+
return time.Unix(0, syscall.TimevalToNsec(tv))
19+
}

utils/now_unix_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//go:build unix
2+
3+
package utils
4+
5+
import (
6+
"fmt"
7+
"os"
8+
"testing"
9+
"time"
10+
11+
"github.com/stretchr/testify/assert"
12+
)
13+
14+
func TestCustomTimeNow(t *testing.T) {
15+
precision = time.Millisecond
16+
17+
for i := 0; i < 1000; i++ {
18+
timestamp := time.Now().UnixNano()
19+
customTimestamp := Now().UnixNano()
20+
21+
// two timestamp should within 1 percistion
22+
assert.Equal(t, timestamp <= customTimestamp, true, fmt.Sprintf("Loop %d: timestamp <= customTimestamp should be true. timestamp: %d, customTimestamp: %d", i, timestamp, customTimestamp))
23+
assert.Equal(t, timestamp+int64(precision) >= customTimestamp, true, fmt.Sprintf("Loop: %d: customTimestamp should within %s. timestamp: %d, customTimestamp: %d", i, precision.String(), timestamp, customTimestamp))
24+
25+
os.Setenv("TZ", fmt.Sprintf("UTC%d", 14-i%27))
26+
time.Sleep(time.Nanosecond)
27+
}
28+
}
29+
30+
func BenchmarkGoTimeNow(t *testing.B) {
31+
t.ResetTimer()
32+
for n := 0; n < t.N; n++ {
33+
_ = time.Now()
34+
}
35+
t.StopTimer()
36+
}
37+
38+
func BenchmarkCustomTimeNow(t *testing.B) {
39+
t.ResetTimer()
40+
for n := 0; n < t.N; n++ {
41+
_ = Now()
42+
}
43+
t.StopTimer()
44+
}

0 commit comments

Comments
 (0)