Repository: zeromicro/go-zero
Pull request #1860 (2022-05-03 09:42:17)

chore: use time.Now() instead of timex.Time() because go optimized it


Patch:
diff --git a/core/breaker/breaker.go b/core/breaker/breaker.go
index 08acf8c7..3d5102ef 100644
--- a/core/breaker/breaker.go
+++ b/core/breaker/breaker.go
@@ -5,12 +5,12 @@ import (
 	"fmt"
 	"strings"
 	"sync"
+	"time"
 
 	"github.com/zeromicro/go-zero/core/mathx"
 	"github.com/zeromicro/go-zero/core/proc"
 	"github.com/zeromicro/go-zero/core/stat"
 	"github.com/zeromicro/go-zero/core/stringx"
-	"github.com/zeromicro/go-zero/core/timex"
 )
 
 const (
@@ -198,7 +198,7 @@ type errorWindow struct {
 
 func (ew *errorWindow) add(reason string) {
 	ew.lock.Lock()
-	ew.reasons[ew.index] = fmt.Sprintf("%s %s", timex.Time().Format(timeFormat), reason)
+	ew.reasons[ew.index] = fmt.Sprintf("%s %s", time.Now().Format(timeFormat), reason)
 	ew.index = (ew.index + 1) % numHistoryReasons
 	ew.count = mathx.MinInt(ew.count+1, numHistoryReasons)
 	ew.lock.Unlock()
diff --git a/core/logx/rotatelogger.go b/core/logx/rotatelogger.go
index ef86e507..a7b8ea72 100644
--- a/core/logx/rotatelogger.go
+++ b/core/logx/rotatelogger.go
@@ -15,7 +15,6 @@ import (
 
 	"github.com/zeromicro/go-zero/core/fs"
 	"github.com/zeromicro/go-zero/core/lang"
-	"github.com/zeromicro/go-zero/core/timex"
 )
 
 const (
@@ -290,12 +289,12 @@ func (l *RotateLogger) write(v []byte) {
 }
 
 func compressLogFile(file string) {
-	start := timex.Now()
+	start := time.Now()
 	Infof("compressing log file: %s", file)
 	if err := gzipFile(file); err != nil {
 		Errorf("compress error: %s", err)
 	} else {
-		Infof("compressed log file: %s, took %s", file, timex.Since(start))
+		Infof("compressed log file: %s, took %s", file, time.Since(start))
 	}
 }
 
diff --git a/core/logx/util.go b/core/logx/util.go
index 0adc5a38..2c89e9ff 100644
--- a/core/logx/util.go
+++ b/core/logx/util.go
@@ -4,8 +4,7 @@ import (
 	"fmt"
 	"runtime"
 	"strings"
-
-	"github.com/zeromicro/go-zero/core/timex"
+	"time"
 )
 
 func getCaller(callDepth int) string {
@@ -18,7 +17,7 @@ func getCaller(callDepth int) string {
 }
 
 func getTimestamp() string {
-	return timex.Time().Format(timeFormat)
+	return time.Now().Format(timeFormat)
 }
 
 func prettyCaller(file string, line int) string {
diff --git a/core/stat/alert.go b/core/stat/alert.go
index 9696bf83..db604dad 100644
--- a/core/stat/alert.go
+++ b/core/stat/alert.go
@@ -15,7 +15,6 @@ import (
 	"github.com/zeromicro/go-zero/core/logx"
 	"github.com/zeromicro/go-zero/core/proc"
 	"github.com/zeromicro/go-zero/core/sysx"
-	"github.com/zeromicro/go-zero/core/timex"
 )
 
 const (
@@ -47,7 +46,7 @@ func Report(msg string) {
 	if fn != nil {
 		reported := lessExecutor.DoOrDiscard(func() {
 			var builder strings.Builder
-			fmt.Fprintf(&builder, "%s\n", timex.Time().Format(timeFormat))
+			fmt.Fprintf(&builder, "%s\n", time.Now().Format(timeFormat))
 			if len(clusterName) > 0 {
 				fmt.Fprintf(&builder, "cluster: %s\n", clusterName)
 			}
diff --git a/core/timex/relativetime.go b/core/timex/relativetime.go
index 25a31d97..23375a06 100644
--- a/core/timex/relativetime.go
+++ b/core/timex/relativetime.go
@@ -15,8 +15,3 @@ func Now() time.Duration {
 func Since(d time.Duration) time.Duration {
 	return time.Since(initTime) - d
 }
-
-// Time returns current time, the same as time.Now().
-func Time() time.Time {
-	return initTime.Add(Now())
-}
diff --git a/core/timex/ticker.go b/core/timex/ticker.go
index 2691a030..1238229d 100644
--- a/core/timex/ticker.go
+++ b/core/timex/ticker.go
@@ -67,7 +67,7 @@ func (ft *fakeTicker) Stop() {
 }
 
 func (ft *fakeTicker) Tick() {
-	ft.c <- Time()
+	ft.c <- time.Now()
 }
 
 func (ft *fakeTicker) Wait(d time.Duration) error {


Test patch:
diff --git a/core/timex/relativetime_test.go b/core/timex/relativetime_test.go
index 386f6d2f..950dbc66 100644
--- a/core/timex/relativetime_test.go
+++ b/core/timex/relativetime_test.go
@@ -15,11 +15,18 @@ func TestRelativeTime(t *testing.T) {
 	assert.True(t, Since(now) > 0)
 }
 
-func TestRelativeTime_Time(t *testing.T) {
-	diff := time.Until(Time())
-	if diff > 0 {
-		assert.True(t, diff < time.Second)
-	} else {
-		assert.True(t, -diff < time.Second)
+func BenchmarkTimeSince(b *testing.B) {
+	b.ReportAllocs()
+
+	for i := 0; i < b.N; i++ {
+		_ = time.Since(time.Now())
+	}
+}
+
+func BenchmarkTimexSince(b *testing.B) {
+	b.ReportAllocs()
+
+	for i := 0; i < b.N; i++ {
+		_ = Since(Now())
 	}
 }