-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyse.go
73 lines (62 loc) · 1.72 KB
/
analyse.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
package main
import (
"fmt"
"github.com/google/pprof/profile"
"os"
"sort"
)
var profiles = []string{
"cpu_profile_Bufio.prof",
"cpu_profile_IoCopy.prof",
"cpu_profile_IoCopyBuffer.prof",
"cpu_profile_IoPipe.prof",
"cpu_profile_IoCopyDirect.prof",
"cpu_profile_ReadvWritev.prof",
"cpu_profile_Sendfile.prof",
"cpu_profile_Splice.prof",
"cpu_profile_Syscall.prof",
"cpu_profile_UnixSyscall.prof",
}
func main() {
type ProfileInfo struct {
Name string
CPUTime int64
CPUTimeMs float64
}
var profileInfos []ProfileInfo
for _, profilePath := range profiles {
f, err := os.Open("results/" + profilePath)
if err != nil {
fmt.Printf("Error opening profile %s: %v\n", profilePath, err)
continue
}
defer f.Close()
prof, err := profile.Parse(f)
if err != nil {
fmt.Printf("Error parsing profile %s: %v\n", profilePath, err)
continue
}
var totalTime int64
for _, sample := range prof.Sample {
for _, value := range sample.Value {
totalTime += value
}
}
// Convert CPU time from nanoseconds to milliseconds
totalTimeMs := float64(totalTime) / 1e6
profileInfos = append(profileInfos, ProfileInfo{profilePath, totalTime, totalTimeMs})
}
// Sort profiles by CPU time
sort.Slice(profileInfos, func(i, j int) bool {
return profileInfos[i].CPUTime < profileInfos[j].CPUTime
})
// Print CPU times and top 3 profiles
fmt.Println("CPU time (in ms) for each profile:")
for _, info := range profileInfos {
fmt.Printf("Profile %s: %.2f ms\n", info.Name, info.CPUTimeMs)
}
fmt.Println("\nTop 3 profiles with the least CPU usage:")
for i := 0; i < 3 && i < len(profileInfos); i++ {
fmt.Printf("%d. Profile %s: %.2f ms\n", i+1, profileInfos[i].Name, profileInfos[i].CPUTimeMs)
}
}