Dmitriy Vyukov | 915784e | 2013-05-15 21:22:32 +0400 | [diff] [blame] | 1 | // 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 | |
| 5 | package runtime_test |
| 6 | |
| 7 | import ( |
Dmitriy Vyukov | a33ef8d | 2013-09-06 16:55:40 -0400 | [diff] [blame] | 8 | . "runtime" |
Dmitriy Vyukov | 915784e | 2013-05-15 21:22:32 +0400 | [diff] [blame] | 9 | "testing" |
| 10 | "unsafe" |
| 11 | ) |
| 12 | |
Dmitriy Vyukov | a33ef8d | 2013-09-06 16:55:40 -0400 | [diff] [blame] | 13 | func 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 Vyukov | 915784e | 2013-05-15 21:22:32 +0400 | [diff] [blame] | 27 | var mallocSink uintptr |
| 28 | |
| 29 | func 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 | |
| 38 | func 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 | |
| 47 | func 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 | |
| 58 | func 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 | } |