-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
108 lines (96 loc) · 2.78 KB
/
Copy pathexample_test.go
File metadata and controls
108 lines (96 loc) · 2.78 KB
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
99
100
101
102
103
104
105
106
107
108
package massif_test
import (
"encoding/json"
"fmt"
"log"
"os"
"github.com/joshkunz/massif"
)
func Example() {
// Load the example massif file off disk.
f, err := os.Open("testdata/example.massif")
if err != nil {
log.Fatalf("failed to open testdata/example.massif: %v", err)
}
defer f.Close()
parsed, err := massif.Parse(f)
if err != nil {
log.Fatalf("failed to parse testdata/example.massif: %v", err)
}
// Convert the Massif struct into JSON for display purposes only. This is
// not required.
display, err := json.MarshalIndent(parsed, "", " ")
if err != nil {
log.Fatalf("failed to marshal Massif for display: %v", err)
}
fmt.Print(string(display))
// Output:
// {
// "Description": "--massif-out-file=./example.massif",
// "Binary": "example_binary",
// "Args": [
// "--option-one",
// "--option-two",
// "--flag=value"
// ],
// "TimeUnit": "i",
// "Snapshots": [
// {
// "Index": 0,
// "Time": "0",
// "MemoryHeap": 0,
// "MemoryHeapExtra": 0,
// "MemoryStack": 0,
// "HeapTree": ""
// },
// {
// "Index": 1,
// "Time": "103242501",
// "MemoryHeap": 8988696,
// "MemoryHeapExtra": 713576,
// "MemoryStack": 0,
// "HeapTree": "Note: This heap is snipped.\nn6: 1123587 (heap allocation functions) malloc/new/new[], --alloc-fns, etc.\n n0: 12558 in 28 places, all below massif's threshold (1.00%)"
// },
// {
// "Index": 2,
// "Time": "161677632",
// "MemoryHeap": 12931384,
// "MemoryHeapExtra": 1124552,
// "MemoryStack": 0,
// "HeapTree": "Note: This heap is snipped.\nn6: 1616423 (heap allocation functions) malloc/new/new[], --alloc-fns, etc.\n n0: 13839 in 28 places, all below massif's threshold (1.00%)"
// },
// {
// "Index": 3,
// "Time": "273947848",
// "MemoryHeap": 22066184,
// "MemoryHeapExtra": 1915064,
// "MemoryStack": 0,
// "HeapTree": ""
// }
// ]
// }
}
// ExamplePeakUsage prints the peak memory usage found in the example massif
// run.
func Example_peakUsage() {
// Load the example massif file off disk.
f, err := os.Open("testdata/example.massif")
if err != nil {
log.Fatalf("failed to open testdata/example.massif: %v", err)
}
defer f.Close()
parsed, err := massif.Parse(f)
if err != nil {
log.Fatalf("failed to parse testdata/example.massif: %v", err)
}
var max *massif.Snapshot
for _, snap := range parsed.Snapshots {
if max == nil || snap.MemoryHeap > max.MemoryHeap {
max = &snap
}
}
fmt.Printf("Max memory usage of %.2f MiB in snapshot %d at time %s%s",
max.MemoryHeap.Mebibytes(), max.Index, max.Time, parsed.TimeUnit)
// Output:
// Max memory usage of 2.63 MiB in snapshot 3 at time 273947848i
}