mm_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package mem
  2. import (
  3. "encoding/json"
  4. "log"
  5. "math/rand"
  6. "os"
  7. "runtime"
  8. "strings"
  9. "testing"
  10. "time"
  11. )
  12. var MemBuffer []byte
  13. func init() {
  14. log.SetOutput(os.Stdout)
  15. log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds)
  16. }
  17. func MemTest() {
  18. x := rand.Intn(1024 * 1024 * 1024 * 4)
  19. log.Println("alloc str", x, "(", x/1024/1024, "MB)")
  20. str := strings.Repeat("-", x)
  21. log.Println("str buf")
  22. nbs := []byte(str)
  23. // log.Println("append")
  24. // MemBuffer = append(MemBuffer, nbs...)
  25. x = len(MemBuffer) + len(nbs)
  26. log.Println("make buf", x, "(", x/1024/1024, "MB)")
  27. tbs := make([]byte, x)
  28. x = len(MemBuffer)
  29. log.Println("copy old", x, "(", x/1024/1024, "MB)")
  30. copy(tbs, MemBuffer)
  31. x = len(nbs)
  32. log.Println("slice buf")
  33. xbs := tbs[len(MemBuffer):]
  34. MemBuffer = tbs
  35. log.Println("copy new", x, "(", x/1024/1024, "MB)")
  36. copy(xbs, nbs)
  37. x = len(MemBuffer)
  38. log.Println("buf size", x, "(", x/1024/1024/1024, "GB)")
  39. //MemBuffer = []byte{}
  40. MemStats()
  41. }
  42. func MemStats() map[string]interface{} {
  43. var m runtime.MemStats
  44. runtime.ReadMemStats(&m)
  45. //bs, _ := json.Marshal(m)
  46. mm := map[string]interface{}{
  47. "Alloc": m.Alloc,
  48. "HeapIdle": m.HeapIdle,
  49. "HeapInuse": m.HeapInuse,
  50. "HeapSys": m.HeapSys,
  51. "MCacheInuse": m.MCacheInuse,
  52. "MCacheSys": m.MCacheSys,
  53. "MSpanInuse": m.MSpanInuse,
  54. "MSpanSys": m.MSpanSys,
  55. "OtherSys": m.OtherSys,
  56. "StackInuse": m.StackInuse,
  57. "StackSys": m.StackSys,
  58. "Sys": m.Sys,
  59. }
  60. //json.Unmarshal(bs, &mm)
  61. // delete(mm, "PauseNs")
  62. // delete(mm, "PauseEnd")
  63. // delete(mm, "BySize")
  64. obs, _ := json.Marshal(mm)
  65. log.Println(string(obs))
  66. return mm
  67. }
  68. func HugeMemTest() {
  69. x := 1024 * 1024 * 1024 * 30
  70. log.Println("make buf", x, "(", x/1024/1024/1024, "GB)")
  71. tbs := make([]byte, x)
  72. log.Println("make buf ok", &tbs)
  73. MemStats()
  74. }
  75. func MapTest() map[string]interface{} {
  76. m := map[int][]byte{}
  77. n := 0
  78. t := 0
  79. for ; t < 1024*1024*1024*20; n += 1 {
  80. x := 1024 * 1024 * n
  81. bs := make([]byte, x)
  82. for i := 0; i < len(bs); i++ {
  83. bs[i] = byte(rand.Int())
  84. }
  85. m[n] = bs
  86. t += x
  87. log.Println("map size", len(m), "content size", t, "(", t/1024/1024/1024, "GB)")
  88. MemStats()
  89. time.Sleep(1 * time.Millisecond)
  90. }
  91. mmm := MemStats()
  92. for ; n > 0; n -= 1 {
  93. t -= len(m[n])
  94. delete(m, n)
  95. log.Println("map size", len(m), "content size", t, "(", t/1024/1024/1024, "GB)")
  96. MemStats()
  97. time.Sleep(1 * time.Millisecond)
  98. }
  99. return mmm
  100. }
  101. func Gogogo(n int, bsc []byte) []byte {
  102. x := 1024 * 1024 * n
  103. bs := make([]byte, x)
  104. log.Println(len(bs))
  105. if n > 0 {
  106. bs = Gogogo(n+1, bs)
  107. bs = append(bs, bsc...)
  108. }
  109. return bs
  110. }
  111. func Test_MemoryAlloc(t *testing.T) {
  112. // gogogo(1, nil)
  113. mmm := MapTest()
  114. wait := true
  115. n := 0
  116. for ; wait; n += 1 {
  117. //test()
  118. time.Sleep(1000 * time.Millisecond)
  119. if n%10 == 0 {
  120. bs, _ := json.Marshal(mmm)
  121. log.Println(string(bs))
  122. }
  123. MemStats()
  124. }
  125. }