-
Notifications
You must be signed in to change notification settings - Fork 2
/
cgobench_test.go
50 lines (41 loc) · 1015 Bytes
/
cgobench_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package cgobench
import (
"bytes"
"fmt"
"runtime"
"sync"
"testing"
)
// Benchmark results on my machine:
//
// BenchmarkCGO-8 10000000 179 ns/op
// BenchmarkGo-8 2000000000 1.82 ns/op
func BenchmarkCGO(b *testing.B) {
CallCgo(b.N)
}
// BenchmarkGo must be called with `-gcflags -l` to avoid inlining.
func BenchmarkGo(b *testing.B) {
CallGo(b.N)
}
// Example_cgo_sleep demonstrates that each blocking Cgo call consumes a
// new thread.
func Example_cgo_sleep() {
const maxThreads = 50
var wg sync.WaitGroup
wg.Add(2 * maxThreads)
for i := 0; i < 2*maxThreads; i++ {
startSleeper(&wg)
}
// Make sure they're all running.
wg.Wait()
// Extract a rough count of threads from the stack.
// You might as well run your variant of
// `ps -M $(pgrep cgobench)`.
buf := make([]byte, 1<<16)
buf = buf[:runtime.Stack(buf, true)]
if bytes.Count(buf, []byte("locked to thread")) > maxThreads {
fmt.Printf("> %d threads are running", maxThreads)
}
// Output:
// > 50 threads are running
}