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

master
Kevin Wan 3 years ago committed by GitHub
parent ac321fc146
commit bab72b7630
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -5,12 +5,12 @@ import (
"fmt" "fmt"
"strings" "strings"
"sync" "sync"
"time"
"github.com/zeromicro/go-zero/core/mathx" "github.com/zeromicro/go-zero/core/mathx"
"github.com/zeromicro/go-zero/core/proc" "github.com/zeromicro/go-zero/core/proc"
"github.com/zeromicro/go-zero/core/stat" "github.com/zeromicro/go-zero/core/stat"
"github.com/zeromicro/go-zero/core/stringx" "github.com/zeromicro/go-zero/core/stringx"
"github.com/zeromicro/go-zero/core/timex"
) )
const ( const (
@ -198,7 +198,7 @@ type errorWindow struct {
func (ew *errorWindow) add(reason string) { func (ew *errorWindow) add(reason string) {
ew.lock.Lock() 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.index = (ew.index + 1) % numHistoryReasons
ew.count = mathx.MinInt(ew.count+1, numHistoryReasons) ew.count = mathx.MinInt(ew.count+1, numHistoryReasons)
ew.lock.Unlock() ew.lock.Unlock()

@ -15,7 +15,6 @@ import (
"github.com/zeromicro/go-zero/core/fs" "github.com/zeromicro/go-zero/core/fs"
"github.com/zeromicro/go-zero/core/lang" "github.com/zeromicro/go-zero/core/lang"
"github.com/zeromicro/go-zero/core/timex"
) )
const ( const (
@ -290,12 +289,12 @@ func (l *RotateLogger) write(v []byte) {
} }
func compressLogFile(file string) { func compressLogFile(file string) {
start := timex.Now() start := time.Now()
Infof("compressing log file: %s", file) Infof("compressing log file: %s", file)
if err := gzipFile(file); err != nil { if err := gzipFile(file); err != nil {
Errorf("compress error: %s", err) Errorf("compress error: %s", err)
} else { } else {
Infof("compressed log file: %s, took %s", file, timex.Since(start)) Infof("compressed log file: %s, took %s", file, time.Since(start))
} }
} }

@ -4,8 +4,7 @@ import (
"fmt" "fmt"
"runtime" "runtime"
"strings" "strings"
"time"
"github.com/zeromicro/go-zero/core/timex"
) )
func getCaller(callDepth int) string { func getCaller(callDepth int) string {
@ -18,7 +17,7 @@ func getCaller(callDepth int) string {
} }
func getTimestamp() string { func getTimestamp() string {
return timex.Time().Format(timeFormat) return time.Now().Format(timeFormat)
} }
func prettyCaller(file string, line int) string { func prettyCaller(file string, line int) string {

@ -15,7 +15,6 @@ import (
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/proc" "github.com/zeromicro/go-zero/core/proc"
"github.com/zeromicro/go-zero/core/sysx" "github.com/zeromicro/go-zero/core/sysx"
"github.com/zeromicro/go-zero/core/timex"
) )
const ( const (
@ -47,7 +46,7 @@ func Report(msg string) {
if fn != nil { if fn != nil {
reported := lessExecutor.DoOrDiscard(func() { reported := lessExecutor.DoOrDiscard(func() {
var builder strings.Builder 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 { if len(clusterName) > 0 {
fmt.Fprintf(&builder, "cluster: %s\n", clusterName) fmt.Fprintf(&builder, "cluster: %s\n", clusterName)
} }

@ -15,8 +15,3 @@ func Now() time.Duration {
func Since(d time.Duration) time.Duration { func Since(d time.Duration) time.Duration {
return time.Since(initTime) - d return time.Since(initTime) - d
} }
// Time returns current time, the same as time.Now().
func Time() time.Time {
return initTime.Add(Now())
}

@ -15,11 +15,18 @@ func TestRelativeTime(t *testing.T) {
assert.True(t, Since(now) > 0) assert.True(t, Since(now) > 0)
} }
func TestRelativeTime_Time(t *testing.T) { func BenchmarkTimeSince(b *testing.B) {
diff := time.Until(Time()) b.ReportAllocs()
if diff > 0 {
assert.True(t, diff < time.Second) for i := 0; i < b.N; i++ {
} else { _ = time.Since(time.Now())
assert.True(t, -diff < time.Second) }
}
func BenchmarkTimexSince(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = Since(Now())
} }
} }

@ -67,7 +67,7 @@ func (ft *fakeTicker) Stop() {
} }
func (ft *fakeTicker) Tick() { func (ft *fakeTicker) Tick() {
ft.c <- Time() ft.c <- time.Now()
} }
func (ft *fakeTicker) Wait(d time.Duration) error { func (ft *fakeTicker) Wait(d time.Duration) error {

Loading…
Cancel
Save