瀏覽代碼

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

Kevin Wan 3 年之前
父節點
當前提交
bab72b7630
共有 7 個文件被更改,包括 21 次插入22 次删除
  1. 2 2
      core/breaker/breaker.go
  2. 2 3
      core/logx/rotatelogger.go
  3. 2 3
      core/logx/util.go
  4. 1 2
      core/stat/alert.go
  5. 0 5
      core/timex/relativetime.go
  6. 13 6
      core/timex/relativetime_test.go
  7. 1 1
      core/timex/ticker.go

+ 2 - 2
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()

+ 2 - 3
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))
 	}
 }
 

+ 2 - 3
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 {

+ 1 - 2
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)
 			}

+ 0 - 5
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())
-}

+ 13 - 6
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())
 	}
 }

+ 1 - 1
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 {