github.com/choleraehyq/piD@v0.0.11/pid_go1.5_test.go (about) 1 // +build gc,go1.5 2 3 package goid 4 5 import ( 6 "fmt" 7 "testing" 8 "unsafe" 9 ) 10 11 var _ = unsafe.Sizeof(0) 12 13 //go:linkname procPin runtime.procPin 14 //go:nosplit 15 func procPin() int 16 17 //go:linkname procUnpin runtime.procUnpin 18 //go:nosplit 19 func procUnpin() 20 21 func getTID() int { 22 tid := procPin() 23 procUnpin() 24 return tid 25 } 26 27 func TestParallelGetPid(t *testing.T) { 28 ch := make(chan *string, 100) 29 for i := 0; i < cap(ch); i++ { 30 go func(i int) { 31 id := GetPid() 32 expected := getTID() 33 if id == expected { 34 ch <- nil 35 return 36 } 37 s := fmt.Sprintf("Expected %d, but got %d", expected, id) 38 ch <- &s 39 }(i) 40 } 41 42 for i := 0; i < cap(ch); i++ { 43 val := <-ch 44 if val != nil { 45 t.Fatal(*val) 46 } 47 } 48 } 49 50 func TestGetPid(t *testing.T) { 51 p1 := GetPid() 52 p2 := getTID() 53 if p1 != p2 { 54 t.Fatalf("The result of GetPid %d procPin %d are not equal!", p1, p2) 55 } 56 } 57 58 func BenchmarkGetPid(b *testing.B) { 59 for i := 0; i < b.N; i++ { 60 GetPid() 61 } 62 } 63 64 func BenchmarkParallelGetPid(b *testing.B) { 65 b.RunParallel(func(pb *testing.PB) { 66 for pb.Next() { 67 GetPid() 68 } 69 }) 70 }