-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench.go
98 lines (84 loc) · 1.97 KB
/
bench.go
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"runtime"
"time"
"github.com/julienschmidt/httprouter"
"github.com/segmentio/analytics-go"
"github.com/sirupsen/logrus"
"github.com/urfave/negroni"
)
func main() {
wk := os.Getenv("WRITE_KEY")
if wk == "" {
wk = "foo"
}
api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
out, _ := ioutil.ReadAll(r.Body)
fmt.Printf("%+v\n\t%s", r, out)
}))
defer api.Close()
n := negroni.New()
segmentMiddleware := metrics.NewMetricsManagerWithConfig(
"foo",
true,
wk,
[]string{},
logrus.New(),
"metrics-middleware",
1.0,
analytics.Config{
Interval: time.Second,
BatchSize: 1,
Endpoint: "https://ory-metrics-server.herokuapp.com",
},
)
go segmentMiddleware.RegisterSegment("1.0.0", "c1b", time.Now().String())
go segmentMiddleware.CommitMemoryStatistics()
n.Use(segmentMiddleware)
r := httprouter.New()
r.GET("/", func(writer http.ResponseWriter, request *http.Request, params httprouter.Params) {
writer.WriteHeader(http.StatusNoContent)
})
n.UseHandler(r)
ts := httptest.NewServer(n)
defer ts.Close()
printMemUsage()
go func() {
for {
printMemUsage()
time.Sleep(time.Second)
}
}()
c := ts.Client()
for i := 0; i <= 5000; i++ {
func() {
resp, err := c.Get(ts.URL)
if err != nil {
logrus.WithError(err).Fatalf("Unable to get")
}
defer resp.Body.Close()
if http.StatusNoContent != resp.StatusCode {
logrus.WithError(err).Fatalf("Unable to get")
}
time.Sleep(time.Millisecond * 10)
}()
}
printMemUsage()
}
func printMemUsage() {
var m runtime.MemStats
runtime.ReadMemStats(&m)
// For info on each, see: https://golang.org/pkg/runtime/#MemStats
fmt.Printf("Alloc = %v MiB", bToMb(m.Alloc))
fmt.Printf("\tTotalAlloc = %v MiB", bToMb(m.TotalAlloc))
fmt.Printf("\tSys = %v MiB", bToMb(m.Sys))
fmt.Printf("\tNumGC = %v\n", m.NumGC)
}
func bToMb(b uint64) uint64 {
return b / 1024 / 1024
}