mirror of
https://github.com/tailscale/tailscale.git
synced 2025-08-27 07:38:49 +00:00
tstime/mono: new package
Package mono provides a fast monotonic time. Its primary advantage is that it is fast: It is approximately twice as fast as time.Now. This is because time.Now uses two clock calls, one for wall time and one for monotonic time. We ask for the current time 4-6 times per network packet. At ~50ns per call to time.Now, that's enough to show up in CPU profiles. Package mono is a first step towards addressing that. It is designed to be a near drop-in replacement for package time. Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
This commit is contained in:

committed by
Josh Bleecher Snyder

parent
881bb8bcdc
commit
142670b8c2
30
tstime/mono/mono_test.go
Normal file
30
tstime/mono/mono_test.go
Normal file
@@ -0,0 +1,30 @@
|
||||
// Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package mono
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestNow(t *testing.T) {
|
||||
start := Now()
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
if elapsed := Since(start); elapsed < 100*time.Millisecond {
|
||||
t.Errorf("short sleep: %v elapsed, want min %v", elapsed, 100*time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkMonoNow(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
Now()
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkTimeNow(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
time.Now()
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user