1 // Copyright 2011 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.
17 func TestCPUProfile(t *testing.T) {
20 // see Apple Bug Report #9177434 (copied into change description)
30 buf := make([]byte, 100000)
32 if err := StartCPUProfile(&prof); err != nil {
35 // This loop takes about a quarter second on a 2 GHz laptop.
36 // We only need to get one 100 Hz clock tick, so we've got
37 // a 25x safety buffer.
38 for i := 0; i < 1000; i++ {
39 crc32.ChecksumIEEE(buf)
43 // Convert []byte to []uintptr.
45 val := *(*[]uintptr)(unsafe.Pointer(&bytes))
46 val = val[:len(bytes)/int(unsafe.Sizeof(uintptr(0)))]
49 t.Fatalf("profile too short: %#x", val)
51 if val[0] != 0 || val[1] != 3 || val[2] != 0 || val[3] != 1e6/100 || val[4] != 0 {
52 t.Fatalf("unexpected header %#x", val[:5])
55 // Check that profile is well formed and contains ChecksumIEEE.
59 if len(val) < 2 || val[0] < 1 || val[1] < 1 || uintptr(len(val)) < 2+val[1] {
60 t.Fatalf("malformed profile. leftover: %#x", val)
62 for _, pc := range val[2 : 2+val[1]] {
63 f := runtime.FuncForPC(pc)
67 if strings.Contains(f.Name(), "ChecksumIEEE") {
75 t.Fatal("did not find ChecksumIEEE in the profile")