-
Notifications
You must be signed in to change notification settings - Fork 992
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use syscall.Gettimeofday to get current time (#961)
* use syscall.Gettimeofday to get current time * use Gettimeofday in just linux and darwin * rename file and add some test case * fix test file syntax * fix within check * fix code problem --------- Co-authored-by: lance6716 <lance6716@gmail.com>
- Loading branch information
Showing
13 changed files
with
100 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
//go:build !unix | ||
|
||
package utils | ||
|
||
import "time" | ||
|
||
var Now = time.Now |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
//go:build unix | ||
|
||
package utils | ||
|
||
import ( | ||
"syscall" | ||
"time" | ||
) | ||
|
||
// Now is a faster method to get current time | ||
func Now() time.Time { | ||
var tv syscall.Timeval | ||
if err := syscall.Gettimeofday(&tv); err != nil { | ||
// If it failed at syscall, use time package instead | ||
return time.Now() | ||
} | ||
|
||
return time.Unix(0, syscall.TimevalToNsec(tv)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
//go:build unix | ||
|
||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCustomTimeNow(t *testing.T) { | ||
precision := time.Millisecond | ||
|
||
for i := 0; i < 1000; i++ { | ||
timestamp := time.Now() | ||
customTimestamp := Now() | ||
|
||
// two timestamp should within 1 percistion | ||
assert.WithinDuration(t, timestamp, customTimestamp, precision, fmt.Sprintf("Loop: %d: customTimestamp should within %s. timestamp: %d, customTimestamp: %d", i, precision.String(), timestamp.UnixNano(), customTimestamp.UnixNano())) | ||
|
||
time.Sleep(time.Nanosecond) | ||
} | ||
} | ||
|
||
func BenchmarkGoTimeNow(t *testing.B) { | ||
t.ResetTimer() | ||
for n := 0; n < t.N; n++ { | ||
_ = time.Now() | ||
} | ||
t.StopTimer() | ||
} | ||
|
||
func BenchmarkCustomTimeNow(t *testing.B) { | ||
t.ResetTimer() | ||
for n := 0; n < t.N; n++ { | ||
_ = Now() | ||
} | ||
t.StopTimer() | ||
} |