snapshot of https://github.com/grpc/grpc-go.git (master, 2026-09-15; blobs > 131072B omitted)
Some checks failed
codecov / upload (push) Failing after 13m59s
CodeQL / Analyze (push) Failing after 1s
Testing / vet-proto (push) Failing after 1s
Testing / tests (arm, latest) (push) Waiting to run
Testing / extras (latest) (push) Failing after 1s
Testing / static checks (latest-1) (push) Failing after 1s
Testing / tests (-race, latest) (push) Failing after 1s
Testing / tests (i386, latest) (push) Failing after 1s
Testing / tests (latest) (push) Failing after 1s
Testing / tests (latest-1) (push) Failing after 1s
Lock Threads / lock (push) Failing after 1s
govulncheck / govulncheck (.) (push) Failing after 1s
govulncheck / govulncheck (./cmd/protoc-gen-go-grpc) (push) Failing after 1s
govulncheck / govulncheck (./gcp/observability) (push) Failing after 1s
govulncheck / govulncheck (./security/advancedtls) (push) Failing after 1s
govulncheck / govulncheck (./stats/opencensus) (push) Failing after 1s
Stale bot / stale (push) Failing after 1s
Some checks failed
codecov / upload (push) Failing after 13m59s
CodeQL / Analyze (push) Failing after 1s
Testing / vet-proto (push) Failing after 1s
Testing / tests (arm, latest) (push) Waiting to run
Testing / extras (latest) (push) Failing after 1s
Testing / static checks (latest-1) (push) Failing after 1s
Testing / tests (-race, latest) (push) Failing after 1s
Testing / tests (i386, latest) (push) Failing after 1s
Testing / tests (latest) (push) Failing after 1s
Testing / tests (latest-1) (push) Failing after 1s
Lock Threads / lock (push) Failing after 1s
govulncheck / govulncheck (.) (push) Failing after 1s
govulncheck / govulncheck (./cmd/protoc-gen-go-grpc) (push) Failing after 1s
govulncheck / govulncheck (./gcp/observability) (push) Failing after 1s
govulncheck / govulncheck (./security/advancedtls) (push) Failing after 1s
govulncheck / govulncheck (./stats/opencensus) (push) Failing after 1s
Stale bot / stale (push) Failing after 1s
This commit is contained in:
125
benchmark/primitives/code_string_test.go
Normal file
125
benchmark/primitives/code_string_test.go
Normal file
@@ -0,0 +1,125 @@
|
||||
/*
|
||||
*
|
||||
* Copyright 2017 gRPC authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package primitives_test
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"google.golang.org/grpc/codes"
|
||||
)
|
||||
|
||||
type codeBench uint32
|
||||
|
||||
const (
|
||||
OK codeBench = iota
|
||||
Canceled
|
||||
Unknown
|
||||
InvalidArgument
|
||||
DeadlineExceeded
|
||||
NotFound
|
||||
AlreadyExists
|
||||
PermissionDenied
|
||||
ResourceExhausted
|
||||
FailedPrecondition
|
||||
Aborted
|
||||
OutOfRange
|
||||
Unimplemented
|
||||
Internal
|
||||
Unavailable
|
||||
DataLoss
|
||||
Unauthenticated
|
||||
)
|
||||
|
||||
// The following String() function was generated by stringer.
|
||||
const codeName = "OKCanceledUnknownInvalidArgumentDeadlineExceededNotFoundAlreadyExistsPermissionDeniedResourceExhaustedFailedPreconditionAbortedOutOfRangeUnimplementedInternalUnavailableDataLossUnauthenticated"
|
||||
|
||||
var codeIndex = [...]uint8{0, 2, 10, 17, 32, 48, 56, 69, 85, 102, 120, 127, 137, 150, 158, 169, 177, 192}
|
||||
|
||||
func (i codeBench) String() string {
|
||||
if i >= codeBench(len(codeIndex)-1) {
|
||||
return "Code(" + strconv.FormatInt(int64(i), 10) + ")"
|
||||
}
|
||||
return codeName[codeIndex[i]:codeIndex[i+1]]
|
||||
}
|
||||
|
||||
var nameMap = map[codeBench]string{
|
||||
OK: "OK",
|
||||
Canceled: "Canceled",
|
||||
Unknown: "Unknown",
|
||||
InvalidArgument: "InvalidArgument",
|
||||
DeadlineExceeded: "DeadlineExceeded",
|
||||
NotFound: "NotFound",
|
||||
AlreadyExists: "AlreadyExists",
|
||||
PermissionDenied: "PermissionDenied",
|
||||
ResourceExhausted: "ResourceExhausted",
|
||||
FailedPrecondition: "FailedPrecondition",
|
||||
Aborted: "Aborted",
|
||||
OutOfRange: "OutOfRange",
|
||||
Unimplemented: "Unimplemented",
|
||||
Internal: "Internal",
|
||||
Unavailable: "Unavailable",
|
||||
DataLoss: "DataLoss",
|
||||
Unauthenticated: "Unauthenticated",
|
||||
}
|
||||
|
||||
func (i codeBench) StringUsingMap() string {
|
||||
if s, ok := nameMap[i]; ok {
|
||||
return s
|
||||
}
|
||||
return "Code(" + strconv.FormatInt(int64(i), 10) + ")"
|
||||
}
|
||||
|
||||
func BenchmarkCodeStringStringer(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
c := codeBench(uint32(i % 17))
|
||||
_ = c.String()
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkCodeStringMap(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
c := codeBench(uint32(i % 17))
|
||||
_ = c.StringUsingMap()
|
||||
}
|
||||
}
|
||||
|
||||
// codes.Code.String() does a switch.
|
||||
func BenchmarkCodeStringSwitch(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
c := codes.Code(uint32(i % 17))
|
||||
_ = c.String()
|
||||
}
|
||||
}
|
||||
|
||||
// Testing all codes (0<=c<=16) and also one overflow (17).
|
||||
func BenchmarkCodeStringStringerWithOverflow(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
c := codeBench(uint32(i % 18))
|
||||
_ = c.String()
|
||||
}
|
||||
}
|
||||
|
||||
// Testing all codes (0<=c<=16) and also one overflow (17).
|
||||
func BenchmarkCodeStringSwitchWithOverflow(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
c := codes.Code(uint32(i % 18))
|
||||
_ = c.String()
|
||||
}
|
||||
}
|
||||
152
benchmark/primitives/context_test.go
Normal file
152
benchmark/primitives/context_test.go
Normal file
@@ -0,0 +1,152 @@
|
||||
/*
|
||||
*
|
||||
* Copyright 2017 gRPC authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package primitives_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
const defaultTestTimeout = 10 * time.Second
|
||||
|
||||
func BenchmarkCancelContextErrNoErr(b *testing.B) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
for i := 0; i < b.N; i++ {
|
||||
if err := ctx.Err(); err != nil {
|
||||
b.Fatal("error")
|
||||
}
|
||||
}
|
||||
cancel()
|
||||
}
|
||||
|
||||
func BenchmarkCancelContextErrGotErr(b *testing.B) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
for i := 0; i < b.N; i++ {
|
||||
if err := ctx.Err(); err == nil {
|
||||
b.Fatal("error")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkCancelContextChannelNoErr(b *testing.B) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
for i := 0; i < b.N; i++ {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
b.Fatal("error: ctx.Done():", ctx.Err())
|
||||
default:
|
||||
}
|
||||
}
|
||||
cancel()
|
||||
}
|
||||
|
||||
func BenchmarkCancelContextChannelGotErr(b *testing.B) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
for i := 0; i < b.N; i++ {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
if err := ctx.Err(); err == nil {
|
||||
b.Fatal("error")
|
||||
}
|
||||
default:
|
||||
b.Fatal("error: !ctx.Done()")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkTimerContextErrNoErr(b *testing.B) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
|
||||
for i := 0; i < b.N; i++ {
|
||||
if err := ctx.Err(); err != nil {
|
||||
b.Fatal("error")
|
||||
}
|
||||
}
|
||||
cancel()
|
||||
}
|
||||
|
||||
func BenchmarkTimerContextErrGotErr(b *testing.B) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Microsecond)
|
||||
cancel()
|
||||
for i := 0; i < b.N; i++ {
|
||||
if err := ctx.Err(); err == nil {
|
||||
b.Fatal("error")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkTimerContextChannelNoErr(b *testing.B) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
|
||||
for i := 0; i < b.N; i++ {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
b.Fatal("error: ctx.Done():", ctx.Err())
|
||||
default:
|
||||
}
|
||||
}
|
||||
cancel()
|
||||
}
|
||||
|
||||
func BenchmarkTimerContextChannelGotErr(b *testing.B) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Microsecond)
|
||||
cancel()
|
||||
for i := 0; i < b.N; i++ {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
if err := ctx.Err(); err == nil {
|
||||
b.Fatal("error")
|
||||
}
|
||||
default:
|
||||
b.Fatal("error: !ctx.Done()")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type ctxKey struct{}
|
||||
|
||||
func newContextWithLocalKey(parent context.Context) context.Context {
|
||||
return context.WithValue(parent, ctxKey{}, nil)
|
||||
}
|
||||
|
||||
var ck = ctxKey{}
|
||||
|
||||
func newContextWithGlobalKey(parent context.Context) context.Context {
|
||||
return context.WithValue(parent, ck, nil)
|
||||
}
|
||||
|
||||
func BenchmarkContextWithValue(b *testing.B) {
|
||||
benches := []struct {
|
||||
name string
|
||||
f func(context.Context) context.Context
|
||||
}{
|
||||
{"newContextWithLocalKey", newContextWithLocalKey},
|
||||
{"newContextWithGlobalKey", newContextWithGlobalKey},
|
||||
}
|
||||
|
||||
pCtx := context.Background()
|
||||
for _, bench := range benches {
|
||||
b.Run(bench.name, func(b *testing.B) {
|
||||
for j := 0; j < b.N; j++ {
|
||||
bench.f(pCtx)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
466
benchmark/primitives/primitives_test.go
Normal file
466
benchmark/primitives/primitives_test.go
Normal file
@@ -0,0 +1,466 @@
|
||||
/*
|
||||
*
|
||||
* Copyright 2017 gRPC authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
// Package primitives_test contains benchmarks for various synchronization primitives
|
||||
// available in Go.
|
||||
package primitives_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func BenchmarkSelectClosed(b *testing.B) {
|
||||
c := make(chan struct{})
|
||||
close(c)
|
||||
x := 0
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
select {
|
||||
case <-c:
|
||||
x++
|
||||
default:
|
||||
}
|
||||
}
|
||||
b.StopTimer()
|
||||
if x != b.N {
|
||||
b.Fatal("error")
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkSelectOpen(b *testing.B) {
|
||||
c := make(chan struct{})
|
||||
x := 0
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
select {
|
||||
case <-c:
|
||||
default:
|
||||
x++
|
||||
}
|
||||
}
|
||||
b.StopTimer()
|
||||
if x != b.N {
|
||||
b.Fatal("error")
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkAtomicBool(b *testing.B) {
|
||||
c := int32(0)
|
||||
x := 0
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
if atomic.LoadInt32(&c) == 0 {
|
||||
x++
|
||||
}
|
||||
}
|
||||
b.StopTimer()
|
||||
if x != b.N {
|
||||
b.Fatal("error")
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkAtomicValueLoad(b *testing.B) {
|
||||
c := atomic.Value{}
|
||||
c.Store(0)
|
||||
x := 0
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
if c.Load().(int) == 0 {
|
||||
x++
|
||||
}
|
||||
}
|
||||
b.StopTimer()
|
||||
if x != b.N {
|
||||
b.Fatal("error")
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkAtomicValueStore(b *testing.B) {
|
||||
c := atomic.Value{}
|
||||
v := 123
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
c.Store(v)
|
||||
}
|
||||
b.StopTimer()
|
||||
}
|
||||
|
||||
func BenchmarkMutex(b *testing.B) {
|
||||
c := sync.Mutex{}
|
||||
x := 0
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
c.Lock()
|
||||
x++
|
||||
c.Unlock()
|
||||
}
|
||||
b.StopTimer()
|
||||
if x != b.N {
|
||||
b.Fatal("error")
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkRWMutex(b *testing.B) {
|
||||
c := sync.RWMutex{}
|
||||
x := 0
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
c.RLock()
|
||||
x++
|
||||
c.RUnlock()
|
||||
}
|
||||
b.StopTimer()
|
||||
if x != b.N {
|
||||
b.Fatal("error")
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkRWMutexW(b *testing.B) {
|
||||
c := sync.RWMutex{}
|
||||
x := 0
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
c.Lock()
|
||||
x++
|
||||
c.Unlock()
|
||||
}
|
||||
b.StopTimer()
|
||||
if x != b.N {
|
||||
b.Fatal("error")
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkMutexWithDefer(b *testing.B) {
|
||||
c := sync.Mutex{}
|
||||
x := 0
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
func() {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
x++
|
||||
}()
|
||||
}
|
||||
b.StopTimer()
|
||||
if x != b.N {
|
||||
b.Fatal("error")
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkMutexWithClosureDefer(b *testing.B) {
|
||||
c := sync.Mutex{}
|
||||
x := 0
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
func() {
|
||||
c.Lock()
|
||||
defer func() { c.Unlock() }()
|
||||
x++
|
||||
}()
|
||||
}
|
||||
b.StopTimer()
|
||||
if x != b.N {
|
||||
b.Fatal("error")
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkMutexWithoutDefer(b *testing.B) {
|
||||
c := sync.Mutex{}
|
||||
x := 0
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
func() {
|
||||
c.Lock()
|
||||
x++
|
||||
c.Unlock()
|
||||
}()
|
||||
}
|
||||
b.StopTimer()
|
||||
if x != b.N {
|
||||
b.Fatal("error")
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkAtomicAddInt64(b *testing.B) {
|
||||
var c int64
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
atomic.AddInt64(&c, 1)
|
||||
}
|
||||
b.StopTimer()
|
||||
if c != int64(b.N) {
|
||||
b.Fatal("error")
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkAtomicTimeValueStore(b *testing.B) {
|
||||
var c atomic.Value
|
||||
t := time.Now()
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
c.Store(t)
|
||||
}
|
||||
b.StopTimer()
|
||||
}
|
||||
|
||||
func BenchmarkAtomic16BValueStore(b *testing.B) {
|
||||
var c atomic.Value
|
||||
t := struct {
|
||||
a int64
|
||||
b int64
|
||||
}{
|
||||
123, 123,
|
||||
}
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
c.Store(t)
|
||||
}
|
||||
b.StopTimer()
|
||||
}
|
||||
|
||||
func BenchmarkAtomic32BValueStore(b *testing.B) {
|
||||
var c atomic.Value
|
||||
t := struct {
|
||||
a int64
|
||||
b int64
|
||||
c int64
|
||||
d int64
|
||||
}{
|
||||
123, 123, 123, 123,
|
||||
}
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
c.Store(t)
|
||||
}
|
||||
b.StopTimer()
|
||||
}
|
||||
|
||||
func BenchmarkAtomicPointerStore(b *testing.B) {
|
||||
t := 123
|
||||
var up unsafe.Pointer
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
atomic.StorePointer(&up, unsafe.Pointer(&t))
|
||||
}
|
||||
b.StopTimer()
|
||||
}
|
||||
|
||||
func BenchmarkAtomicTimePointerStore(b *testing.B) {
|
||||
t := time.Now()
|
||||
var up unsafe.Pointer
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
atomic.StorePointer(&up, unsafe.Pointer(&t))
|
||||
}
|
||||
b.StopTimer()
|
||||
}
|
||||
|
||||
func BenchmarkStoreContentionWithAtomic(b *testing.B) {
|
||||
t := 123
|
||||
var c unsafe.Pointer
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
for pb.Next() {
|
||||
atomic.StorePointer(&c, unsafe.Pointer(&t))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func BenchmarkStoreContentionWithMutex(b *testing.B) {
|
||||
t := 123
|
||||
var mu sync.Mutex
|
||||
var c int
|
||||
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
for pb.Next() {
|
||||
mu.Lock()
|
||||
c = t
|
||||
mu.Unlock()
|
||||
}
|
||||
})
|
||||
_ = c
|
||||
}
|
||||
|
||||
type dummyStruct struct {
|
||||
a int64
|
||||
b time.Time
|
||||
}
|
||||
|
||||
func BenchmarkStructStoreContention(b *testing.B) {
|
||||
d := dummyStruct{}
|
||||
dp := unsafe.Pointer(&d)
|
||||
t := time.Now()
|
||||
for _, j := range []int{100000000, 10000, 0} {
|
||||
for _, i := range []int{100000, 10} {
|
||||
b.Run(fmt.Sprintf("CAS/%v/%v", j, i), func(b *testing.B) {
|
||||
b.SetParallelism(i)
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
n := &dummyStruct{
|
||||
b: t,
|
||||
}
|
||||
for pb.Next() {
|
||||
for y := 0; y < j; y++ {
|
||||
}
|
||||
for {
|
||||
v := (*dummyStruct)(atomic.LoadPointer(&dp))
|
||||
n.a = v.a + 1
|
||||
if atomic.CompareAndSwapPointer(&dp, unsafe.Pointer(v), unsafe.Pointer(n)) {
|
||||
n = v
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
var mu sync.Mutex
|
||||
for _, j := range []int{100000000, 10000, 0} {
|
||||
for _, i := range []int{100000, 10} {
|
||||
b.Run(fmt.Sprintf("Mutex/%v/%v", j, i), func(b *testing.B) {
|
||||
b.SetParallelism(i)
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
for pb.Next() {
|
||||
for y := 0; y < j; y++ {
|
||||
}
|
||||
mu.Lock()
|
||||
d.a++
|
||||
d.b = t
|
||||
mu.Unlock()
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type myFooer struct{}
|
||||
|
||||
func (myFooer) Foo() {}
|
||||
|
||||
type fooer interface {
|
||||
Foo()
|
||||
}
|
||||
|
||||
func BenchmarkInterfaceTypeAssertion(b *testing.B) {
|
||||
// Call a separate function to avoid compiler optimizations.
|
||||
runInterfaceTypeAssertion(b, myFooer{})
|
||||
}
|
||||
|
||||
func runInterfaceTypeAssertion(b *testing.B, fer any) {
|
||||
x := 0
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
if _, ok := fer.(fooer); ok {
|
||||
x++
|
||||
}
|
||||
}
|
||||
b.StopTimer()
|
||||
if x != b.N {
|
||||
b.Fatal("error")
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStructTypeAssertion(b *testing.B) {
|
||||
// Call a separate function to avoid compiler optimizations.
|
||||
runStructTypeAssertion(b, myFooer{})
|
||||
}
|
||||
|
||||
func runStructTypeAssertion(b *testing.B, fer any) {
|
||||
x := 0
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
if _, ok := fer.(myFooer); ok {
|
||||
x++
|
||||
}
|
||||
}
|
||||
b.StopTimer()
|
||||
if x != b.N {
|
||||
b.Fatal("error")
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkWaitGroupAddDone(b *testing.B) {
|
||||
wg := sync.WaitGroup{}
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
i := 0
|
||||
for ; pb.Next(); i++ {
|
||||
wg.Add(1)
|
||||
}
|
||||
for ; i > 0; i-- {
|
||||
wg.Done()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func BenchmarkRLockUnlock(b *testing.B) {
|
||||
mu := sync.RWMutex{}
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
i := 0
|
||||
for ; pb.Next(); i++ {
|
||||
mu.RLock()
|
||||
}
|
||||
for ; i > 0; i-- {
|
||||
mu.RUnlock()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
type ifNop interface {
|
||||
nop()
|
||||
}
|
||||
|
||||
type alwaysNop struct{}
|
||||
|
||||
func (alwaysNop) nop() {}
|
||||
|
||||
type concreteNop struct {
|
||||
isNop atomic.Bool
|
||||
i int
|
||||
}
|
||||
|
||||
func (c *concreteNop) nop() {
|
||||
if c.isNop.Load() {
|
||||
return
|
||||
}
|
||||
c.i++
|
||||
}
|
||||
|
||||
func BenchmarkInterfaceNop(b *testing.B) {
|
||||
n := ifNop(alwaysNop{})
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
for pb.Next() {
|
||||
n.nop()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func BenchmarkConcreteNop(b *testing.B) {
|
||||
n := &concreteNop{}
|
||||
n.isNop.Store(true)
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
for pb.Next() {
|
||||
n.nop()
|
||||
}
|
||||
})
|
||||
}
|
||||
114
benchmark/primitives/safe_config_selector_test.go
Normal file
114
benchmark/primitives/safe_config_selector_test.go
Normal file
@@ -0,0 +1,114 @@
|
||||
/*
|
||||
*
|
||||
* Copyright 2017 gRPC authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
// Benchmark options for safe config selector type.
|
||||
|
||||
package primitives_test
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
type safeUpdaterAtomicAndCounter struct {
|
||||
ptr unsafe.Pointer // *countingFunc
|
||||
}
|
||||
|
||||
type countingFunc struct {
|
||||
mu sync.RWMutex
|
||||
f func()
|
||||
}
|
||||
|
||||
func (s *safeUpdaterAtomicAndCounter) call() {
|
||||
cfPtr := atomic.LoadPointer(&s.ptr)
|
||||
var cf *countingFunc
|
||||
for {
|
||||
cf = (*countingFunc)(cfPtr)
|
||||
cf.mu.RLock()
|
||||
cfPtr2 := atomic.LoadPointer(&s.ptr)
|
||||
if cfPtr == cfPtr2 {
|
||||
// Use cf with confidence!
|
||||
break
|
||||
}
|
||||
// cf changed; try to use the new one instead, because the old one is
|
||||
// no longer valid to use.
|
||||
cf.mu.RUnlock()
|
||||
cfPtr = cfPtr2
|
||||
}
|
||||
defer cf.mu.RUnlock()
|
||||
cf.f()
|
||||
}
|
||||
|
||||
func (s *safeUpdaterAtomicAndCounter) update(f func()) {
|
||||
newCF := &countingFunc{f: f}
|
||||
oldCFPtr := atomic.SwapPointer(&s.ptr, unsafe.Pointer(newCF))
|
||||
if oldCFPtr == nil {
|
||||
return
|
||||
}
|
||||
(*countingFunc)(oldCFPtr).mu.Lock()
|
||||
(*countingFunc)(oldCFPtr).mu.Unlock() //lint:ignore SA2001 necessary to unlock after locking to unblock any RLocks
|
||||
}
|
||||
|
||||
type safeUpdaterRWMutex struct {
|
||||
mu sync.RWMutex
|
||||
f func()
|
||||
}
|
||||
|
||||
func (s *safeUpdaterRWMutex) call() {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
s.f()
|
||||
}
|
||||
|
||||
func (s *safeUpdaterRWMutex) update(f func()) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.f = f
|
||||
}
|
||||
|
||||
type updater interface {
|
||||
call()
|
||||
update(f func())
|
||||
}
|
||||
|
||||
func benchmarkSafeUpdater(b *testing.B, u updater) {
|
||||
t := time.NewTicker(time.Second)
|
||||
go func() {
|
||||
for range t.C {
|
||||
u.update(func() {})
|
||||
}
|
||||
}()
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
u.update(func() {})
|
||||
for pb.Next() {
|
||||
u.call()
|
||||
}
|
||||
})
|
||||
t.Stop()
|
||||
}
|
||||
|
||||
func BenchmarkSafeUpdaterAtomicAndCounter(b *testing.B) {
|
||||
benchmarkSafeUpdater(b, &safeUpdaterAtomicAndCounter{})
|
||||
}
|
||||
|
||||
func BenchmarkSafeUpdaterRWMutex(b *testing.B) {
|
||||
benchmarkSafeUpdater(b, &safeUpdaterRWMutex{})
|
||||
}
|
||||
165
benchmark/primitives/syncmap_test.go
Normal file
165
benchmark/primitives/syncmap_test.go
Normal file
@@ -0,0 +1,165 @@
|
||||
/*
|
||||
*
|
||||
* Copyright 2019 gRPC authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package primitives_test
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type incrementUint64Map interface {
|
||||
increment(string)
|
||||
result(string) uint64
|
||||
}
|
||||
|
||||
type mapWithLock struct {
|
||||
mu sync.Mutex
|
||||
m map[string]uint64
|
||||
}
|
||||
|
||||
func newMapWithLock() incrementUint64Map {
|
||||
return &mapWithLock{
|
||||
m: make(map[string]uint64),
|
||||
}
|
||||
}
|
||||
|
||||
func (mwl *mapWithLock) increment(c string) {
|
||||
mwl.mu.Lock()
|
||||
mwl.m[c]++
|
||||
mwl.mu.Unlock()
|
||||
}
|
||||
|
||||
func (mwl *mapWithLock) result(c string) uint64 {
|
||||
return mwl.m[c]
|
||||
}
|
||||
|
||||
type mapWithAtomicFastpath struct {
|
||||
mu sync.RWMutex
|
||||
m map[string]*uint64
|
||||
}
|
||||
|
||||
func newMapWithAtomicFastpath() incrementUint64Map {
|
||||
return &mapWithAtomicFastpath{
|
||||
m: make(map[string]*uint64),
|
||||
}
|
||||
}
|
||||
|
||||
func (mwaf *mapWithAtomicFastpath) increment(c string) {
|
||||
mwaf.mu.RLock()
|
||||
if p, ok := mwaf.m[c]; ok {
|
||||
atomic.AddUint64(p, 1)
|
||||
mwaf.mu.RUnlock()
|
||||
return
|
||||
}
|
||||
mwaf.mu.RUnlock()
|
||||
|
||||
mwaf.mu.Lock()
|
||||
if p, ok := mwaf.m[c]; ok {
|
||||
atomic.AddUint64(p, 1)
|
||||
mwaf.mu.Unlock()
|
||||
return
|
||||
}
|
||||
var temp uint64 = 1
|
||||
mwaf.m[c] = &temp
|
||||
mwaf.mu.Unlock()
|
||||
}
|
||||
|
||||
func (mwaf *mapWithAtomicFastpath) result(c string) uint64 {
|
||||
return atomic.LoadUint64(mwaf.m[c])
|
||||
}
|
||||
|
||||
type mapWithSyncMap struct {
|
||||
m sync.Map
|
||||
}
|
||||
|
||||
func newMapWithSyncMap() incrementUint64Map {
|
||||
return &mapWithSyncMap{}
|
||||
}
|
||||
|
||||
func (mwsm *mapWithSyncMap) increment(c string) {
|
||||
p, ok := mwsm.m.Load(c)
|
||||
if !ok {
|
||||
tp := new(uint64)
|
||||
p, _ = mwsm.m.LoadOrStore(c, tp)
|
||||
}
|
||||
atomic.AddUint64(p.(*uint64), 1)
|
||||
}
|
||||
|
||||
func (mwsm *mapWithSyncMap) result(c string) uint64 {
|
||||
p, _ := mwsm.m.Load(c)
|
||||
return atomic.LoadUint64(p.(*uint64))
|
||||
}
|
||||
|
||||
func benchmarkIncrementUint64Map(b *testing.B, f func() incrementUint64Map) {
|
||||
const cat = "cat"
|
||||
benches := []struct {
|
||||
name string
|
||||
goroutineCount int
|
||||
}{
|
||||
{
|
||||
name: " 1",
|
||||
goroutineCount: 1,
|
||||
},
|
||||
{
|
||||
name: " 10",
|
||||
goroutineCount: 10,
|
||||
},
|
||||
{
|
||||
name: " 100",
|
||||
goroutineCount: 100,
|
||||
},
|
||||
{
|
||||
name: "1000",
|
||||
goroutineCount: 1000,
|
||||
},
|
||||
}
|
||||
for _, bb := range benches {
|
||||
b.Run(bb.name, func(b *testing.B) {
|
||||
m := f()
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(bb.goroutineCount)
|
||||
b.ResetTimer()
|
||||
for i := 0; i < bb.goroutineCount; i++ {
|
||||
go func() {
|
||||
for j := 0; j < b.N; j++ {
|
||||
m.increment(cat)
|
||||
}
|
||||
wg.Done()
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
b.StopTimer()
|
||||
if m.result(cat) != uint64(bb.goroutineCount*b.N) {
|
||||
b.Fatalf("result is %d, want %d", m.result(cat), b.N)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkMapWithSyncMutexContention(b *testing.B) {
|
||||
benchmarkIncrementUint64Map(b, newMapWithLock)
|
||||
}
|
||||
|
||||
func BenchmarkMapWithAtomicFastpath(b *testing.B) {
|
||||
benchmarkIncrementUint64Map(b, newMapWithAtomicFastpath)
|
||||
}
|
||||
|
||||
func BenchmarkMapWithSyncMap(b *testing.B) {
|
||||
benchmarkIncrementUint64Map(b, newMapWithSyncMap)
|
||||
}
|
||||
Reference in New Issue
Block a user