summaryrefslogtreecommitdiff
path: root/util/fasttime_test.go
blob: c8ff0e13561fab7099942bc14f17be412831e650 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package util

import (
	"github.com/stretchr/testify/assert"
	"testing"
	"time"
)

func TestFTime_UnixNano(t *testing.T) {
	ft := NewFastTime(time.Millisecond)
	defer ft.Stop()

	var d int64

	d = time.Now().UnixNano() - ft.UnixNano()

	assert.True(t, d >= 0)
	assert.True(t, d <= int64(time.Millisecond*2))

	time.Sleep(time.Millisecond * 100)
	d = time.Now().UnixNano() - ft.UnixNano()

	assert.True(t, d >= 0)
	assert.True(t, d <= int64(time.Millisecond*2))
}

func Benchmark_FastTime(b *testing.B) {
	ft := NewFastTime(time.Microsecond)
	defer ft.Stop()

	b.ReportAllocs()

	for n := 0; n < b.N; n++ {
		_ = ft.UnixNano()
	}
}

func Benchmark_Time(b *testing.B) {
	ft := NewFastTime(time.Microsecond)
	defer ft.Stop()

	b.ReportAllocs()

	for n := 0; n < b.N; n++ {
		_ = time.Now().UnixNano()
	}
}