/
report.go
87 lines (71 loc) · 2.58 KB
/
report.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
// Copyright 2014 Rafael Dantas Justo. All rights reserved.
// Use of this source code is governed by a GPL
// license that can be found in the LICENSE file.
// utils add features to make the test life easier
package utils
import (
"io/ioutil"
"os"
"runtime"
"runtime/pprof"
"time"
)
// If we found a report file in the current path, rename it so we don't lose the old data.
// We are going to use the modification date from the file. We also don't check the errors
// because we really don't care
func WriteReport(reportFile string, report string) {
if file, err := os.Open(reportFile); err == nil {
newFilename := reportFile + ".old-"
if fileStatus, err := file.Stat(); err == nil {
newFilename += fileStatus.ModTime().Format("20060102150405")
} else {
// Did not find the modification date, so lets use now
newFilename += time.Now().Format("20060102150405")
}
// We don't use defer because we want to rename it before the end of scope
file.Close()
os.Rename(reportFile, newFilename)
}
ioutil.WriteFile(reportFile, []byte(report), 0444)
}
// Initialize CPU profile. This function will return a function that MUST be defered to the end of
// the report test. If something goes wrong, the funciton will abort the program
func StartCPUProfile(name string) func() {
profileFile, err := os.Create(name)
if err != nil {
Fatalln("Error creating CPU profile file", err)
}
if err := pprof.StartCPUProfile(profileFile); err != nil {
Fatalln("Error starting CPU profile file", err)
}
return pprof.StopCPUProfile
}
// Initialize Memory profile. This function will return a function that MUST be defered to the end
// of the report test. If something goes wrong, the funciton will abort the program
func StartMemoryProfile(name string) func() {
return func() {
runtime.GC()
profileFile, err := os.Create(name)
if err != nil {
Fatalln("Error creating memory profile file", err)
}
if err := pprof.Lookup("heap").WriteTo(profileFile, 1); err != nil {
Fatalln("Error writing to memory profile file", err)
}
profileFile.Close()
}
}
// Initialize Go Routines profile. This function will return a function that MUST be defered to the
// end of the report test. If something goes wrong, the funciton will abort the program
func StartGoRoutinesProfile(name string) func() {
return func() {
profileFile, err := os.Create(name)
if err != nil {
Fatalln("Error creating Go routines profile file", err)
}
if err := pprof.Lookup("goroutine").WriteTo(profileFile, 2); err != nil {
Fatalln("Error writing to Go routines profile file", err)
}
profileFile.Close()
}
}