This repository has been archived by the owner on Oct 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 137
/
collector_danger_test.go
85 lines (75 loc) · 2.03 KB
/
collector_danger_test.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
// +build danger
// Run with:
//
// go test -tags=danger -memprofilerate=1 -memprofile=mem.out -bench=BenchmarkChunkedCollector1mil -run=NONE -v
//
// (danger tag is because it is very slow and eats a lot of memory!)
package appdash
import (
"fmt"
"os"
"runtime"
"runtime/debug"
"testing"
"time"
)
// BenchmarkChunkedCollector1mil performs 1 million collects with the same set
// of annotations and prints several runtime memory statistics during the
// process, invoking runtime.GC and runtime/debug.FreeOSMemory ten times as the
// final step.
func BenchmarkChunkedCollector1mil(b *testing.B) {
cc := &ChunkedCollector{
Collector: collectorFunc(func(span SpanID, anns ...Annotation) error {
return nil
}),
MinInterval: time.Millisecond * 1,
}
const (
nCollections = 1000000
nAnnotations = 50
)
anns := make([]Annotation, nAnnotations)
for i := range anns {
anns[i] = Annotation{Key: "k", Value: []byte{'v'}}
}
memStats := &memStats{
Collections: nCollections,
}
memStats.Log("pre")
var x ID
for i := 0; i < b.N; i++ {
for c := 0; c < nCollections; c++ {
x++
err := cc.Collect(SpanID{x, x + 1, x + 2}, anns...)
if err != nil {
b.Fatal(err)
}
}
}
memStats.Log("post")
// Perform N garbage collections (multiple are needed because the GC can at
// times need two phases to collect everything, we use an overly large
// amount just to be sure).
fmt.Println("[garbage collections]")
nGC := 10
for i := 0; i < nGC; i++ {
start := time.Now()
runtime.GC()
fmt.Printf(" %v. GC - %v\n", i, time.Since(start))
}
fmt.Println("")
memStats.Log("after GC")
// Perform N debug.FreeOSMemory() calls, again performing multiple just to
// besure.
fmt.Println("[free os memory]")
nFree := 10
for i := 0; i < nFree; i++ {
start := time.Now()
debug.FreeOSMemory()
fmt.Printf(" %v. FreeOSMemory - %v\n", i, time.Since(start))
}
fmt.Println("")
memStats.Log("after GC & FreeOSMemory")
fmt.Printf("sleeping 30s; check memory usage with 'top -p %v' now", os.Getpid())
time.Sleep(30 * time.Second)
}