drop 0.4mb "runtime/pprof" and "runtime/trace" (#836)

This commit is contained in:
Evan Wallace
2021-02-19 14:37:02 -08:00
parent 187feec684
commit a0d9dc9ce4
4 changed files with 91 additions and 27 deletions

View File

@@ -4,8 +4,6 @@ import (
"fmt"
"os"
"runtime/debug"
"runtime/pprof"
"runtime/trace"
"strings"
"time"
@@ -184,34 +182,22 @@ func main() {
// To view a CPU trace, use "go tool trace [file]". Note that the trace
// viewer doesn't work under Windows Subsystem for Linux for some reason.
if traceFile != "" {
f, err := os.Create(traceFile)
if err != nil {
logger.PrintErrorToStderr(osArgs, fmt.Sprintf(
"Failed to create trace file: %s", err.Error()))
if done := createTraceFile(osArgs, traceFile); done == nil {
return
} else {
defer done()
}
defer f.Close()
trace.Start(f)
defer trace.Stop()
}
// To view a heap trace, use "go tool pprof [file]" and type "top". You can
// also drop it into https://speedscope.app and use the "left heavy" or
// "sandwich" view modes.
if heapFile != "" {
f, err := os.Create(heapFile)
if err != nil {
logger.PrintErrorToStderr(osArgs, fmt.Sprintf(
"Failed to create heap file: %s", err.Error()))
if done := createHeapFile(osArgs, heapFile); done == nil {
return
} else {
defer done()
}
defer func() {
if err := pprof.WriteHeapProfile(f); err != nil {
logger.PrintErrorToStderr(osArgs, fmt.Sprintf(
"Failed to write heap profile: %s", err.Error()))
}
f.Close()
}()
}
// To view a CPU profile, drop the file into https://speedscope.app.
@@ -219,15 +205,11 @@ func main() {
// Linux. The profiler has to be built for native Windows and run using the
// command prompt instead.
if cpuprofileFile != "" {
f, err := os.Create(cpuprofileFile)
if err != nil {
logger.PrintErrorToStderr(osArgs, fmt.Sprintf(
"Failed to create cpuprofile file: %s", err.Error()))
if done := createCpuprofileFile(osArgs, cpuprofileFile); done == nil {
return
} else {
defer done()
}
defer f.Close()
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
if cpuprofileFile != "" {

56
cmd/esbuild/main_other.go Normal file
View File

@@ -0,0 +1,56 @@
// +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()
}
}

24
cmd/esbuild/main_wasm.go Normal file
View File

@@ -0,0 +1,24 @@
// +build js,wasm
package main
import (
"github.com/evanw/esbuild/internal/logger"
)
// Remove this code from the WebAssembly binary to reduce size. This only removes 0.4mb of stuff.
func createTraceFile(osArgs []string, traceFile string) func() {
logger.PrintErrorToStderr(osArgs, "The \"--trace\" flag is not supported when using WebAssembly")
return nil
}
func createHeapFile(osArgs []string, heapFile string) func() {
logger.PrintErrorToStderr(osArgs, "The \"--heap\" flag is not supported when using WebAssembly")
return nil
}
func createCpuprofileFile(osArgs []string, cpuprofileFile string) func() {
logger.PrintErrorToStderr(osArgs, "The \"--cpuprofile\" flag is not supported when using WebAssembly")
return nil
}

View File

@@ -4,6 +4,8 @@ package api
import "fmt"
// Remove the serve API in the WebAssembly build. This removes 2.7mb of stuff.
func serveImpl(serveOptions ServeOptions, buildOptions BuildOptions) (ServeResult, error) {
return ServeResult{}, fmt.Errorf("The \"serve\" API is not supported when using WebAssembly")
}