Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions internal/pprof/pprof.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"os"
"path/filepath"
"runtime"
"runtime/pprof"
runtimepprof "runtime/pprof"
"sync"
"time"
)
Expand All @@ -19,6 +19,9 @@ type ProfileSession struct {
logWriter io.Writer
}

// startCPUProfile is a test seam for simulating runtime/pprof startup failures.
var startCPUProfile = runtimepprof.StartCPUProfile

// BeginProfiling starts CPU and memory profiling, writing the profiles to the specified directory.
func BeginProfiling(profileDir string, logWriter io.Writer) *ProfileSession {
if err := os.MkdirAll(profileDir, 0o755); err != nil {
Expand All @@ -34,7 +37,9 @@ func BeginProfiling(profileDir string, logWriter io.Writer) *ProfileSession {
panic(err)
}

if err := pprof.StartCPUProfile(cpuFile); err != nil {
if err := startCPUProfile(cpuFile); err != nil {
cpuFile.Close()
os.Remove(cpuProfilePath)
panic(err)
}

Expand All @@ -47,15 +52,15 @@ func BeginProfiling(profileDir string, logWriter io.Writer) *ProfileSession {
}

func (p *ProfileSession) Stop() {
pprof.StopCPUProfile()
runtimepprof.StopCPUProfile()
p.cpuFile.Close()

if p.memFilePath != "" {
memFile, err := os.Create(p.memFilePath)
if err != nil {
panic(err)
}
if err := pprof.Lookup("allocs").WriteTo(memFile, 0); err != nil {
if err := runtimepprof.Lookup("allocs").WriteTo(memFile, 0); err != nil {
panic(err)
}
memFile.Close()
Expand Down Expand Up @@ -90,7 +95,7 @@ func (c *CPUProfiler) StartCPUProfile(profileDir string) error {
return fmt.Errorf("failed to create CPU profile file: %w", err)
}

if err := pprof.StartCPUProfile(cpuFile); err != nil {
if err := startCPUProfile(cpuFile); err != nil {
cpuFile.Close()
os.Remove(cpuProfilePath)
return fmt.Errorf("failed to start CPU profile: %w", err)
Expand Down Expand Up @@ -134,7 +139,7 @@ func SaveHeapProfile(profileDir string) (string, error) {
defer heapFile.Close()

runtime.GC()
if err := pprof.Lookup("heap").WriteTo(heapFile, 0); err != nil {
if err := runtimepprof.Lookup("heap").WriteTo(heapFile, 0); err != nil {
os.Remove(heapProfilePath)
return "", fmt.Errorf("failed to write heap profile: %w", err)
}
Expand All @@ -155,7 +160,7 @@ func SaveAllocProfile(profileDir string) (string, error) {
}
defer allocFile.Close()

if err := pprof.Lookup("allocs").WriteTo(allocFile, 0); err != nil {
if err := runtimepprof.Lookup("allocs").WriteTo(allocFile, 0); err != nil {
os.Remove(allocProfilePath)
return "", fmt.Errorf("failed to write alloc profile: %w", err)
}
Expand Down
34 changes: 34 additions & 0 deletions internal/pprof/pprof_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package pprof

import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
"testing"

"github.com/microsoft/typescript-go/internal/testutil"
"gotest.tools/v3/assert"
)

func TestBeginProfilingCleansUpCPUFileWhenStartFails(t *testing.T) {
wantErr := errors.New("failed to start profile")
originalStartCPUProfile := startCPUProfile
startCPUProfile = func(io.Writer) error {
return wantErr
}
t.Cleanup(func() {
startCPUProfile = originalStartCPUProfile
})

profileDir := t.TempDir()
cpuProfilePath := filepath.Join(profileDir, fmt.Sprintf("%d-cpuprofile.pb.gz", os.Getpid()))

testutil.AssertPanics(t, func() {
BeginProfiling(profileDir, io.Discard)
}, wantErr)

_, err := os.Stat(cpuProfilePath)
assert.Assert(t, os.IsNotExist(err), "expected CPU profile file to be removed after start failure, got %v", err)
}
Loading