1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
14 func BenchmarkSemaUncontended(b
*testing
.B
) {
15 type PaddedSem
struct {
19 const CallsPerSched
= 1000
20 procs
:= runtime
.GOMAXPROCS(-1)
21 N
:= int32(b
.N
/ CallsPerSched
)
22 c
:= make(chan bool, procs
)
23 for p
:= 0; p
< procs
; p
++ {
26 for atomic
.AddInt32(&N
, -1) >= 0 {
28 for g
:= 0; g
< CallsPerSched
; g
++ {
29 Runtime_Semrelease(&sem
.sem
)
30 Runtime_Semacquire(&sem
.sem
)
36 for p
:= 0; p
< procs
; p
++ {
41 func benchmarkSema(b
*testing
.B
, block
, work
bool) {
42 const CallsPerSched
= 1000
44 procs
:= runtime
.GOMAXPROCS(-1)
45 N
:= int32(b
.N
/ CallsPerSched
)
46 c
:= make(chan bool, procs
)
47 c2
:= make(chan bool, procs
/2)
50 for p
:= 0; p
< procs
/2; p
++ {
52 Runtime_Semacquire(&sem
)
57 for p
:= 0; p
< procs
; p
++ {
60 for atomic
.AddInt32(&N
, -1) >= 0 {
62 for g
:= 0; g
< CallsPerSched
; g
++ {
63 Runtime_Semrelease(&sem
)
65 for i
:= 0; i
< LocalWork
; i
++ {
70 Runtime_Semacquire(&sem
)
74 Runtime_Semrelease(&sem
)
78 for p
:= 0; p
< procs
/2; p
++ {
82 for p
:= 0; p
< procs
; p
++ {
87 func BenchmarkSemaSyntNonblock(b
*testing
.B
) {
88 benchmarkSema(b
, false, false)
91 func BenchmarkSemaSyntBlock(b
*testing
.B
) {
92 benchmarkSema(b
, true, false)
95 func BenchmarkSemaWorkNonblock(b
*testing
.B
) {
96 benchmarkSema(b
, false, true)
99 func BenchmarkSemaWorkBlock(b
*testing
.B
) {
100 benchmarkSema(b
, true, true)