github.com/brownsys/tracing-framework-go@v0.0.0-20161210174012-0542a62412fe/other/internal/gls/bench_test.go (about)

     1  package gls
     2  
     3  import (
     4  	"runtime"
     5  	"sync"
     6  	"testing"
     7  )
     8  
     9  func BenchmarkCallers(b *testing.B) {
    10  	pc := make([]uintptr, 64)
    11  	b.ResetTimer()
    12  	benchmarkCallers(1, b.N, pc)
    13  }
    14  
    15  func BenchmarkCallers2(b *testing.B) {
    16  	pc := make([]uintptr, 64)
    17  	b.ResetTimer()
    18  	benchmarkCallers(2, b.N, pc)
    19  }
    20  
    21  func BenchmarkCallers4(b *testing.B) {
    22  	pc := make([]uintptr, 64)
    23  	b.ResetTimer()
    24  	benchmarkCallers(4, b.N, pc)
    25  }
    26  
    27  func BenchmarkCallers8(b *testing.B) {
    28  	pc := make([]uintptr, 64)
    29  	b.ResetTimer()
    30  	benchmarkCallers(8, b.N, pc)
    31  }
    32  
    33  func benchmarkCallers(depth, n int, pc []uintptr) {
    34  	if depth > 1 {
    35  		benchmarkCallers(depth-1, n, pc)
    36  		return
    37  	}
    38  	for i := 0; i < n; i++ {
    39  		runtime.Callers(0, pc)
    40  	}
    41  }
    42  
    43  func BenchmarkLockMapAccessReadOnly(b *testing.B) {
    44  	m := make(map[int]int)
    45  	m[1] = 1
    46  	var mtx sync.RWMutex
    47  
    48  	b.ResetTimer()
    49  	for i := 0; i < b.N; i++ {
    50  		mtx.RLock()
    51  		_ = m[1]
    52  		mtx.RUnlock()
    53  	}
    54  }
    55  
    56  func BenchmarkLockMapAccessReadWrite(b *testing.B) {
    57  	m := make(map[int]int)
    58  	m[1] = 1
    59  	var mtx sync.RWMutex
    60  
    61  	b.ResetTimer()
    62  	for i := 0; i < b.N; i++ {
    63  		mtx.Lock()
    64  		_ = m[1]
    65  		mtx.Unlock()
    66  	}
    67  }
    68  
    69  func BenchmarkSpawnNormal(b *testing.B) {
    70  	var wg sync.WaitGroup
    71  	wg.Add(b.N)
    72  	b.ResetTimer()
    73  	for i := 0; i < b.N; i++ {
    74  		go func() { wg.Done() }()
    75  	}
    76  	wg.Wait()
    77  }
    78  
    79  // func BenchmarkLockMapAccessReadOnlyContention(b *testing.B) {
    80  // 	m := make(map[int]int)
    81  // 	m[1] = 1
    82  // 	var mtx sync.RWMutex
    83  
    84  // 	var wg sync.WaitGroup
    85  
    86  // 	f := func() {
    87  // 		for i := 0; i < b.N; i++ {
    88  // 			mtx.RLock()
    89  // 			_ = m[1]
    90  // 			mtx.RUnlock()
    91  // 		}
    92  // 		wg.Done()
    93  // 	}
    94  
    95  // 	b.ResetTimer()
    96  // 	for i := 0; i < runtime.NumCPU(); i++ {
    97  // 		wg.Add(1)
    98  // 		// Use Go so there's an even amount of
    99  // 		// goroutine creation overhead
   100  // 		Go(f, 0)
   101  // 	}
   102  // 	wg.Wait()
   103  // }