/
analyst.go
140 lines (124 loc) · 2.34 KB
/
analyst.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"fmt"
"github.com/go-kid/ioc"
"github.com/go-kid/ioc/app"
"log"
"os"
"os/exec"
"runtime/pprof"
"strconv"
)
type A struct {
LIn *LI `wire:"LIn"`
ILIn iLI `wire:"ILIn"`
LI *LI `wire:""`
ILI iLI `wire:""`
List []*LI `wire:""`
IList []iLI `wire:""`
}
type iLI interface {
Item()
}
type iObj interface {
Field()
}
type Obj struct {
name string
LI *LI `wire:""`
ILI iLI `wire:""`
List []*LI `wire:""`
IList []iLI `wire:""`
}
func (o *Obj) Field() {}
func (o *Obj) Naming() string {
return o.name
}
type Obj1 struct {
Obj
}
type Obj2 struct {
Obj1
}
type Obj3 struct {
Obj2
}
type LI struct {
name string
IObj iObj `wire:""`
IList []iObj `wire:""`
}
func (l *LI) Naming() string {
return l.name
}
func (l *LI) Item() {}
func main() {
comps := []any{
&A{},
&LI{name: "LIn"},
&LI{name: "ILIn"},
&LI{name: ""},
&LI{name: "LI1"},
&LI{name: "LI2"},
&LI{name: "LI3"},
&Obj3{},
}
for i := 10; i < 1000; i++ {
comps = append(comps, &LI{name: "LI" + strconv.Itoa(i)})
comps = append(comps, &Obj3{Obj2{Obj1{Obj{name: "Obj" + strconv.Itoa(i)}}}})
}
fmt.Println("start")
err := Analyst(1, "cpu", func() error {
_, err := ioc.Run(
app.LogError,
app.SetRegistry(factory.NewRegistry()),
app.SetComponents(comps...),
)
//defer run.Close()
return err
})
if err != nil {
panic(err)
}
}
func Analyst(times int, cat string, f func() error) error {
switch cat {
case "cpu":
var fileName = "./performance_analyst/cpu.pprof"
cpuFile, err := os.Create(fileName)
if err != nil {
return err
}
defer os.Remove(fileName)
defer startPProf(fileName)
defer cpuFile.Close()
err = pprof.StartCPUProfile(cpuFile)
if err != nil {
return err
}
defer pprof.StopCPUProfile()
case "mem":
var fileName = "./performance_analyst/mem.pprof"
memFile, err := os.Create("./performance_analyst/mem.pprof")
if err != nil {
return err
}
defer os.Remove(fileName)
defer startPProf(fileName)
defer memFile.Close()
defer pprof.WriteHeapProfile(memFile)
}
for i := 0; i < times; i++ {
if err := f(); err != nil {
return err
}
}
return nil
}
func startPProf(file string) {
cmd := exec.Command("go", "tool", "pprof", "-web", file)
output, err := cmd.CombinedOutput()
if err != nil {
log.Fatalf("start pprof error: %v, %s", err, string(output))
}
}