aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/Xe/gopreload/sample/main.go
blob: b36bef26631e15a29096bb4da01a21b1d4dcb8b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main

import (
	"fmt"
	"math/rand"
	"net/http"
	"runtime"
	"time"

	_ "github.com/Xe/gopreload"
	"github.com/Xe/ln"
)

func main() {
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		spew()

		ln.Log(ln.F{"action": "gc_spew", "who": r.RemoteAddr})

		fmt.Fprintln(w, "done")
	})

	http.ListenAndServe(":9184", nil)
}

func makeBuffer() []byte {
	return make([]byte, rand.Intn(5000000)+5000000)
}

func spew() {
	pool := make([][]byte, 20)

	var m runtime.MemStats
	makes := 0
	for _ = range make([]struct{}, 50) {
		b := makeBuffer()
		makes += 1
		i := rand.Intn(len(pool))
		pool[i] = b

		time.Sleep(time.Millisecond * 250)

		bytes := 0

		for i := 0; i < len(pool); i++ {
			if pool[i] != nil {
				bytes += len(pool[i])
			}
		}

		runtime.ReadMemStats(&m)
		fmt.Printf("%d,%d,%d,%d,%d,%d\n", m.HeapSys, bytes, m.HeapAlloc,
			m.HeapIdle, m.HeapReleased, makes)
	}
}