-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
router, backend: replace time.Time with mono-time to optimize duratio…
…n calculation (#461)
- Loading branch information
Showing
10 changed files
with
109 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,5 +8,6 @@ vendor | |
work | ||
.vscode/ | ||
.cover* | ||
coverage.dat | ||
grafonnet-lib | ||
dist |
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,41 @@ | ||
// Copyright 2024 PingCAP, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package monotime | ||
|
||
import ( | ||
"time" | ||
_ "unsafe" | ||
) | ||
|
||
//go:noescape | ||
//go:linkname nanotime runtime.nanotime | ||
func nanotime() int64 | ||
|
||
// Time is a monotonic clock time which is used to calculate duration. | ||
// It's 2x faster than time.Time. | ||
type Time int64 | ||
|
||
func Now() Time { | ||
return Time(nanotime()) | ||
} | ||
|
||
func Since(t Time) time.Duration { | ||
return time.Duration(Time(nanotime()) - t) | ||
} | ||
|
||
func (t Time) Add(d time.Duration) Time { | ||
return t + Time(d) | ||
} | ||
|
||
func (t Time) Sub(d time.Duration) Time { | ||
return t - Time(d) | ||
} | ||
|
||
func (t Time) Before(u Time) bool { | ||
return t < u | ||
} | ||
|
||
func (t Time) After(u Time) bool { | ||
return t > u | ||
} |
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,46 @@ | ||
// Copyright 2024 PingCAP, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package monotime | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func BenchmarkGoNow(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
time.Now() | ||
} | ||
} | ||
|
||
func BenchmarkMonoNow(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
Now() | ||
} | ||
} | ||
|
||
func BenchmarkGoSince(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
time.Since(time.Now()) | ||
} | ||
} | ||
|
||
func BenchmarkMonoSince(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
Since(Now()) | ||
} | ||
} | ||
|
||
func TestAfter(t *testing.T) { | ||
t1 := Now() | ||
time.Sleep(100 * time.Millisecond) | ||
d := Since(t1) | ||
require.GreaterOrEqual(t, d, 100*time.Millisecond) | ||
require.True(t, Now().After(t1)) | ||
require.True(t, t1.Before(Now())) | ||
require.Greater(t, t1.Add(time.Millisecond), t1) | ||
require.Less(t, t1.Sub(time.Millisecond), t1) | ||
} |