blob: 2b686a6e7e5431413cbb751c41e24dabde6e1c58 [file] [log] [blame]
Dmitriy Vyukov915784e2013-05-15 21:22:32 +04001// Copyright 2013 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.
4
5package runtime_test
6
7import (
Dmitriy Vyukova33ef8d2013-09-06 16:55:40 -04008 . "runtime"
Dmitriy Vyukov915784e2013-05-15 21:22:32 +04009 "testing"
10 "unsafe"
11)
12
Dmitriy Vyukova33ef8d2013-09-06 16:55:40 -040013func TestMemStats(t *testing.T) {
14 // Test that MemStats has sane values.
15 st := new(MemStats)
16 ReadMemStats(st)
17 if st.HeapSys == 0 || st.StackSys == 0 || st.MSpanSys == 0 || st.MCacheSys == 0 ||
18 st.BuckHashSys == 0 || st.GCSys == 0 || st.OtherSys == 0 {
19 t.Fatalf("Zero sys value: %+v", *st)
20 }
21 if st.Sys != st.HeapSys+st.StackSys+st.MSpanSys+st.MCacheSys+
22 st.BuckHashSys+st.GCSys+st.OtherSys {
23 t.Fatalf("Bad sys value: %+v", *st)
24 }
25}
26
Dmitriy Vyukov915784e2013-05-15 21:22:32 +040027var mallocSink uintptr
28
29func BenchmarkMalloc8(b *testing.B) {
30 var x uintptr
31 for i := 0; i < b.N; i++ {
32 p := new(int64)
33 x ^= uintptr(unsafe.Pointer(p))
34 }
35 mallocSink = x
36}
37
38func BenchmarkMalloc16(b *testing.B) {
39 var x uintptr
40 for i := 0; i < b.N; i++ {
41 p := new([2]int64)
42 x ^= uintptr(unsafe.Pointer(p))
43 }
44 mallocSink = x
45}
46
47func BenchmarkMallocTypeInfo8(b *testing.B) {
48 var x uintptr
49 for i := 0; i < b.N; i++ {
50 p := new(struct {
51 p [8 / unsafe.Sizeof(uintptr(0))]*int
52 })
53 x ^= uintptr(unsafe.Pointer(p))
54 }
55 mallocSink = x
56}
57
58func BenchmarkMallocTypeInfo16(b *testing.B) {
59 var x uintptr
60 for i := 0; i < b.N; i++ {
61 p := new(struct {
62 p [16 / unsafe.Sizeof(uintptr(0))]*int
63 })
64 x ^= uintptr(unsafe.Pointer(p))
65 }
66 mallocSink = x
67}