/
main_other.go
57 lines (51 loc) · 1.19 KB
/
main_other.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
//go:build !js || !wasm
// +build !js !wasm
package main
import (
"fmt"
"os"
"runtime/pprof"
"runtime/trace"
"github.com/evanw/esbuild/internal/logger"
)
func createTraceFile(osArgs []string, traceFile string) func() {
f, err := os.Create(traceFile)
if err != nil {
logger.PrintErrorToStderr(osArgs, fmt.Sprintf(
"Failed to create trace file: %s", err.Error()))
return nil
}
trace.Start(f)
return func() {
trace.Stop()
f.Close()
}
}
func createHeapFile(osArgs []string, heapFile string) func() {
f, err := os.Create(heapFile)
if err != nil {
logger.PrintErrorToStderr(osArgs, fmt.Sprintf(
"Failed to create heap file: %s", err.Error()))
return nil
}
return func() {
if err := pprof.WriteHeapProfile(f); err != nil {
logger.PrintErrorToStderr(osArgs, fmt.Sprintf(
"Failed to write heap profile: %s", err.Error()))
}
f.Close()
}
}
func createCpuprofileFile(osArgs []string, cpuprofileFile string) func() {
f, err := os.Create(cpuprofileFile)
if err != nil {
logger.PrintErrorToStderr(osArgs, fmt.Sprintf(
"Failed to create cpuprofile file: %s", err.Error()))
return nil
}
pprof.StartCPUProfile(f)
return func() {
pprof.StopCPUProfile()
f.Close()
}
}