cubefs/util/timeutil/time_test.go
slasher 5d5e45cb0a chore(util): store current time to atomic value
BenchmarkGetCurrentTime-4    100000000    11.60 ns/op            0 B/op          0 allocs/op
-->
BenchmarkGetCurrentTime-4    895091701     1.31 ns/op            0 B/op          0 allocs/op

Signed-off-by: slasher <mcq.sejust@gmail.com>
2024-01-31 19:13:25 +08:00

56 lines
1.3 KiB
Go

// Copyright 2018 The CubeFS Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package timeutil
import (
"testing"
"time"
)
func TestGetCurrentTimeUnix(t *testing.T) {
t1 := GetCurrentTimeUnix()
t2 := GetCurrentTime().Unix()
n := time.Now().Unix()
if n-t1 > 1 || n-t2 > 1 {
t.Errorf("wrong time: %d %d %d", n, t1, t2)
}
time.Sleep(time.Second * 2)
t1 = GetCurrentTimeUnix()
t2 = GetCurrentTime().Unix()
n = time.Now().Unix()
if n-t1 > 1 || n-t2 > 1 {
t.Errorf("wrong time: %d %d %d", n, t1, t2)
}
}
func BenchmarkGetCurrentTimeUnix(b *testing.B) {
for i := 0; i < b.N; i++ {
GetCurrentTimeUnix()
}
}
func BenchmarkGetCurrentTime(b *testing.B) {
for i := 0; i < b.N; i++ {
GetCurrentTime()
}
}
func BenchmarkGetNowTimeUnix(b *testing.B) {
for i := 0; i < b.N; i++ {
time.Now().Unix()
}
}