This repository was archived by the owner on Feb 14, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 10 files changed +198
-0
lines changed Expand file tree Collapse file tree 10 files changed +198
-0
lines changed Original file line number Diff line number Diff line change
1
+ sudo : false
2
+
3
+ language : go
4
+ go :
5
+ - " 1.11.x"
6
+ - " 1.12.x"
7
+ - " 1.x"
8
+ env :
9
+ - GO111MODULE=on
10
+
11
+ before_install :
12
+ - go mod download
13
+ - go get -u golang.org/x/lint/golint
14
+ - go get github.com/mattn/goveralls
15
+
16
+ script :
17
+ - golint ./...
18
+ - go vet ./...
19
+ - go test -covermode=count -coverprofile=profile.cov ./...
20
+ - go test -bench=.
21
+ - goveralls -coverprofile=profile.cov -service=travis-ci
22
+
23
+ notifications :
24
+ email :
25
+ on_success : never
26
+ on_failure : always
Original file line number Diff line number Diff line change
1
+ package timex_test
2
+
3
+ import (
4
+ "testing"
5
+ "time"
6
+
7
+ "github.com/hamba/timex"
8
+ "github.com/hamba/timex/mono"
9
+ )
10
+
11
+ func BenchmarkMonoNow (b * testing.B ) {
12
+ b .ReportAllocs ()
13
+ for i := 0 ; i < b .N ; i ++ {
14
+ _ = mono .Now ()
15
+ }
16
+ }
17
+
18
+ func BenchmarkMonoSince (b * testing.B ) {
19
+ start := mono .Now ()
20
+
21
+ b .ReportAllocs ()
22
+ b .ResetTimer ()
23
+ for i := 0 ; i < b .N ; i ++ {
24
+ _ = mono .Since (start )
25
+ }
26
+ }
27
+
28
+ func BenchmarkTimexNow (b * testing.B ) {
29
+ b .ReportAllocs ()
30
+ for i := 0 ; i < b .N ; i ++ {
31
+ _ = timex .Now ()
32
+ }
33
+ }
34
+
35
+ func BenchmarkTimexSince (b * testing.B ) {
36
+ start := timex .Now ()
37
+
38
+ b .ReportAllocs ()
39
+ b .ResetTimer ()
40
+ for i := 0 ; i < b .N ; i ++ {
41
+ _ = timex .Since (start )
42
+ }
43
+ }
44
+
45
+ func BenchmarkTimexUnix (b * testing.B ) {
46
+ b .ReportAllocs ()
47
+ for i := 0 ; i < b .N ; i ++ {
48
+ _ = timex .Unix ()
49
+ }
50
+ }
51
+
52
+ func BenchmarkWallTimeNow (b * testing.B ) {
53
+ b .ReportAllocs ()
54
+ for i := 0 ; i < b .N ; i ++ {
55
+ _ = time .Now ().UnixNano ()
56
+ }
57
+ }
58
+
59
+ func BenchmarkWallTimeSince (b * testing.B ) {
60
+ start := time .Now ()
61
+
62
+ b .ReportAllocs ()
63
+ b .ResetTimer ()
64
+ for i := 0 ; i < b .N ; i ++ {
65
+ _ = time .Since (start )
66
+ }
67
+ }
Original file line number Diff line number Diff line change
1
+ module github.com/hamba/timex
2
+
3
+ go 1.12
4
+
5
+ require github.com/stretchr/testify v1.3.0
Original file line number Diff line number Diff line change
1
+ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8 =
2
+ github.com/davecgh/go-spew v1.1.0 /go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38 =
3
+ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM =
4
+ github.com/pmezard/go-difflib v1.0.0 /go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4 =
5
+ github.com/stretchr/objx v0.1.0 /go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME =
6
+ github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q =
7
+ github.com/stretchr/testify v1.3.0 /go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI =
Original file line number Diff line number Diff line change
1
+ // Package mono provides a fast monotonic clock source.
2
+ package mono
3
+
4
+ import (
5
+ "time"
6
+ _ "unsafe" // Required in order to import nanotime
7
+ )
8
+
9
+ //go:linkname nanotime runtime.nanotime
10
+ func nanotime () int64
11
+
12
+ // Now returns the current time in nanoseconds from a monotonic clock.
13
+ //
14
+ // The time returned is guaranteed to increase monotonically at a
15
+ // constant rate, unlike timex.Now() which is influenced by NTP
16
+ // changes.
17
+ func Now () int64 {
18
+ return nanotime ()
19
+ }
20
+
21
+ // Since is analogous to https://golang.org/pkg/time/#Since.
22
+ func Since (s int64 ) time.Duration {
23
+ return time .Duration (Now () - s )
24
+ }
Original file line number Diff line number Diff line change
1
+ package mono_test
2
+
3
+ import (
4
+ "testing"
5
+ "time"
6
+
7
+ "github.com/hamba/timex/mono"
8
+ "github.com/stretchr/testify/assert"
9
+ )
10
+
11
+ func TestSince (t * testing.T ) {
12
+ then := mono .Now () - 1000
13
+
14
+ d := mono .Since (then )
15
+
16
+ assert .True (t , time .Duration (1000 ) <= d )
17
+ }
Original file line number Diff line number Diff line change
1
+ // Package timex provides a fast wall clock source.
2
+ package timex
3
+
4
+ import (
5
+ "time"
6
+ _ "unsafe" // Required in order to import walltime
7
+ )
8
+
9
+ //go:linkname walltime runtime.walltime
10
+ func walltime () (int64 , int32 )
11
+
12
+ // Now returns the current time in nanoseconds.
13
+ func Now () int64 {
14
+ sec , nsec := walltime ()
15
+ return sec * 1000000000 + int64 (nsec )
16
+ }
17
+
18
+ // Since is analogous to https://golang.org/pkg/time/#Since.
19
+ func Since (s int64 ) time.Duration {
20
+ return time .Duration (Now () - s )
21
+ }
22
+
23
+ // Unix returns the current time in seconds.
24
+ func Unix () int64 {
25
+ sec , _ := walltime ()
26
+ return sec
27
+ }
Original file line number Diff line number Diff line change
1
+ package timex_test
2
+
3
+ import (
4
+ "testing"
5
+ "time"
6
+
7
+ "github.com/hamba/timex"
8
+ "github.com/stretchr/testify/assert"
9
+ )
10
+
11
+ func TestSince (t * testing.T ) {
12
+ then := timex .Now () - 1000
13
+
14
+ d := timex .Since (then )
15
+
16
+ assert .True (t , time .Duration (1000 ) <= d )
17
+ }
18
+
19
+ func TestUnix (t * testing.T ) {
20
+ want := time .Now ().Unix ()
21
+
22
+ got := timex .Unix ()
23
+
24
+ assert .Equal (t , want , got )
25
+ }
You can’t perform that action at this time.
0 commit comments