~john-koepi/ubuntu/trusty/golang/default

« back to all changes in this revision

Viewing changes to src/pkg/sync/mutex_test.go

  • Committer: Bazaar Package Importer
  • Author(s): Ondřej Surý
  • Date: 2011-08-03 17:04:59 UTC
  • mfrom: (14.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20110803170459-wzd99m3567y80ila
Tags: 1:59-1
* Imported Upstream version 59
* Refresh patches to a new release
* Fix FTBFS on ARM (Closes: #634270)
* Update version.bash to work with Debian packaging and not hg
  repository

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
import (
10
10
        "runtime"
11
11
        . "sync"
 
12
        "sync/atomic"
12
13
        "testing"
13
14
)
14
15
 
43
44
        s := new(uint32)
44
45
        *s = 1
45
46
        c := make(chan bool)
46
 
        runtime.GOMAXPROCS(2)
 
47
        defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(2))
47
48
        b.StartTimer()
48
49
 
49
50
        go HammerSemaphore(s, b.N/2, c)
72
73
        }
73
74
}
74
75
 
75
 
func BenchmarkUncontendedMutex(b *testing.B) {
76
 
        m := new(Mutex)
77
 
        HammerMutex(m, b.N, make(chan bool, 2))
78
 
}
79
 
 
80
 
func BenchmarkContendedMutex(b *testing.B) {
81
 
        b.StopTimer()
82
 
        m := new(Mutex)
83
 
        c := make(chan bool)
84
 
        runtime.GOMAXPROCS(2)
85
 
        b.StartTimer()
86
 
 
87
 
        go HammerMutex(m, b.N/2, c)
88
 
        go HammerMutex(m, b.N/2, c)
89
 
        <-c
90
 
        <-c
91
 
}
92
 
 
93
76
func TestMutexPanic(t *testing.T) {
94
77
        defer func() {
95
78
                if recover() == nil {
102
85
        mu.Unlock()
103
86
        mu.Unlock()
104
87
}
 
88
 
 
89
func BenchmarkMutexUncontended(b *testing.B) {
 
90
        type PaddedMutex struct {
 
91
                Mutex
 
92
                pad [128]uint8
 
93
        }
 
94
        const CallsPerSched = 1000
 
95
        procs := runtime.GOMAXPROCS(-1)
 
96
        N := int32(b.N / CallsPerSched)
 
97
        c := make(chan bool, procs)
 
98
        for p := 0; p < procs; p++ {
 
99
                go func() {
 
100
                        var mu PaddedMutex
 
101
                        for atomic.AddInt32(&N, -1) >= 0 {
 
102
                                runtime.Gosched()
 
103
                                for g := 0; g < CallsPerSched; g++ {
 
104
                                        mu.Lock()
 
105
                                        mu.Unlock()
 
106
                                }
 
107
                        }
 
108
                        c <- true
 
109
                }()
 
110
        }
 
111
        for p := 0; p < procs; p++ {
 
112
                <-c
 
113
        }
 
114
}
 
115
 
 
116
func benchmarkMutex(b *testing.B, slack, work bool) {
 
117
        const (
 
118
                CallsPerSched  = 1000
 
119
                LocalWork      = 100
 
120
                GoroutineSlack = 10
 
121
        )
 
122
        procs := runtime.GOMAXPROCS(-1)
 
123
        if slack {
 
124
                procs *= GoroutineSlack
 
125
        }
 
126
        N := int32(b.N / CallsPerSched)
 
127
        c := make(chan bool, procs)
 
128
        var mu Mutex
 
129
        for p := 0; p < procs; p++ {
 
130
                go func() {
 
131
                        foo := 0
 
132
                        for atomic.AddInt32(&N, -1) >= 0 {
 
133
                                runtime.Gosched()
 
134
                                for g := 0; g < CallsPerSched; g++ {
 
135
                                        mu.Lock()
 
136
                                        mu.Unlock()
 
137
                                        if work {
 
138
                                                for i := 0; i < LocalWork; i++ {
 
139
                                                        foo *= 2
 
140
                                                        foo /= 2
 
141
                                                }
 
142
                                        }
 
143
                                }
 
144
                        }
 
145
                        c <- foo == 42
 
146
                }()
 
147
        }
 
148
        for p := 0; p < procs; p++ {
 
149
                <-c
 
150
        }
 
151
}
 
152
 
 
153
func BenchmarkMutex(b *testing.B) {
 
154
        benchmarkMutex(b, false, false)
 
155
}
 
156
 
 
157
func BenchmarkMutexSlack(b *testing.B) {
 
158
        benchmarkMutex(b, true, false)
 
159
}
 
160
 
 
161
func BenchmarkMutexWork(b *testing.B) {
 
162
        benchmarkMutex(b, false, true)
 
163
}
 
164
 
 
165
func BenchmarkMutexWorkSlack(b *testing.B) {
 
166
        benchmarkMutex(b, true, true)
 
167
}