malloc_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. package runtime_test
  5. import (
  6. "flag"
  7. . "runtime"
  8. "testing"
  9. "time"
  10. "unsafe"
  11. )
  12. func TestMemStats(t *testing.T) {
  13. // Test that MemStats has sane values.
  14. st := new(MemStats)
  15. ReadMemStats(st)
  16. if st.HeapSys == 0 || /* st.StackSys == 0 || */ st.MSpanSys == 0 || st.MCacheSys == 0 ||
  17. st.BuckHashSys == 0 || st.GCSys == 0 || st.OtherSys == 0 {
  18. t.Fatalf("Zero sys value: %+v", *st)
  19. }
  20. if st.Sys != st.HeapSys+st.StackSys+st.MSpanSys+st.MCacheSys+
  21. st.BuckHashSys+st.GCSys+st.OtherSys {
  22. t.Fatalf("Bad sys value: %+v", *st)
  23. }
  24. }
  25. var mallocSink uintptr
  26. func BenchmarkMalloc8(b *testing.B) {
  27. var x uintptr
  28. for i := 0; i < b.N; i++ {
  29. p := new(int64)
  30. x ^= uintptr(unsafe.Pointer(p))
  31. }
  32. mallocSink = x
  33. }
  34. func BenchmarkMalloc16(b *testing.B) {
  35. var x uintptr
  36. for i := 0; i < b.N; i++ {
  37. p := new([2]int64)
  38. x ^= uintptr(unsafe.Pointer(p))
  39. }
  40. mallocSink = x
  41. }
  42. func BenchmarkMallocTypeInfo8(b *testing.B) {
  43. var x uintptr
  44. for i := 0; i < b.N; i++ {
  45. p := new(struct {
  46. p [8 / unsafe.Sizeof(uintptr(0))]*int
  47. })
  48. x ^= uintptr(unsafe.Pointer(p))
  49. }
  50. mallocSink = x
  51. }
  52. func BenchmarkMallocTypeInfo16(b *testing.B) {
  53. var x uintptr
  54. for i := 0; i < b.N; i++ {
  55. p := new(struct {
  56. p [16 / unsafe.Sizeof(uintptr(0))]*int
  57. })
  58. x ^= uintptr(unsafe.Pointer(p))
  59. }
  60. mallocSink = x
  61. }
  62. var n = flag.Int("n", 1000, "number of goroutines")
  63. func BenchmarkGoroutineSelect(b *testing.B) {
  64. quit := make(chan struct{})
  65. read := func(ch chan struct{}) {
  66. for {
  67. select {
  68. case _, ok := <-ch:
  69. if !ok {
  70. return
  71. }
  72. case <-quit:
  73. return
  74. }
  75. }
  76. }
  77. benchHelper(b, *n, read)
  78. }
  79. func BenchmarkGoroutineBlocking(b *testing.B) {
  80. read := func(ch chan struct{}) {
  81. for {
  82. if _, ok := <-ch; !ok {
  83. return
  84. }
  85. }
  86. }
  87. benchHelper(b, *n, read)
  88. }
  89. func BenchmarkGoroutineForRange(b *testing.B) {
  90. read := func(ch chan struct{}) {
  91. for _ = range ch {
  92. }
  93. }
  94. benchHelper(b, *n, read)
  95. }
  96. func benchHelper(b *testing.B, n int, read func(chan struct{})) {
  97. m := make([]chan struct{}, n)
  98. for i := range m {
  99. m[i] = make(chan struct{}, 1)
  100. go read(m[i])
  101. }
  102. b.StopTimer()
  103. b.ResetTimer()
  104. GC()
  105. for i := 0; i < b.N; i++ {
  106. for _, ch := range m {
  107. if ch != nil {
  108. ch <- struct{}{}
  109. }
  110. }
  111. time.Sleep(10 * time.Millisecond)
  112. b.StartTimer()
  113. GC()
  114. b.StopTimer()
  115. }
  116. for _, ch := range m {
  117. close(ch)
  118. }
  119. time.Sleep(10 * time.Millisecond)
  120. }
  121. func BenchmarkGoroutineIdle(b *testing.B) {
  122. quit := make(chan struct{})
  123. fn := func() {
  124. <-quit
  125. }
  126. for i := 0; i < *n; i++ {
  127. go fn()
  128. }
  129. GC()
  130. b.ResetTimer()
  131. for i := 0; i < b.N; i++ {
  132. GC()
  133. }
  134. b.StopTimer()
  135. close(quit)
  136. time.Sleep(10 * time.Millisecond)
  137. }