-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathmain.go
76 lines (61 loc) · 1.41 KB
/
main.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
// Copyright (c) Efficient Go Authors
// Licensed under the Apache License 2.0.
package main
import (
"io"
"log"
"sync"
"github.com/efficientgo/examples/pkg/profile/fd"
)
// Example application instrumented with custom fd profile from Example 9-1.
// Read more in "Efficient Go"; Example 9-2.
type TestApp struct {
files []io.ReadCloser
}
func (a *TestApp) Close() {
for _, cl := range a.files {
_ = cl.Close() // TODO: Check error.
}
a.files = a.files[:0]
}
func (a *TestApp) open(name string) {
f, _ := fd.Open(name) // TODO: Check error.
a.files = append(a.files, f)
}
func (a *TestApp) OpenSingleFile(name string) {
a.open(name)
}
func (a *TestApp) OpenTenFiles(name string) {
for i := 0; i < 10; i++ {
a.open(name)
}
}
func (a *TestApp) Open100FilesConcurrently(name string) {
wg := sync.WaitGroup{}
wg.Add(10)
for i := 0; i < 10; i++ {
go func() {
a.OpenTenFiles(name)
wg.Done()
}()
}
wg.Wait()
}
func main() {
a := &TestApp{}
defer a.Close()
// No matter how many files we opened in the past...
for i := 0; i < 10; i++ {
a.OpenTenFiles("/dev/null")
a.Close()
}
// ...after last close, only files below will be used in profile.
f, _ := fd.Open("/dev/null") // TODO: Check error.
a.files = append(a.files, f)
a.OpenSingleFile("/dev/null")
a.OpenTenFiles("/dev/null")
a.Open100FilesConcurrently("/dev/null")
if err := fd.Write("fd.pprof"); err != nil {
log.Fatal(err)
}
}