Skip to content

Commit

Permalink
pprof (#135)
Browse files Browse the repository at this point in the history
Co-authored-by: Luke Lombardi <[email protected]>
  • Loading branch information
luke-lombardi and Luke Lombardi authored Apr 16, 2024
1 parent d106c46 commit 6592e37
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions internal/gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import (
"net/http"
"os"
"os/signal"
"runtime"
"runtime/pprof"
"syscall"
"time"

"github.com/beam-cloud/beta9/internal/abstractions/endpoint"
"github.com/beam-cloud/beta9/internal/task"
Expand Down Expand Up @@ -301,6 +304,35 @@ func (g *Gateway) registerServices() error {
return nil
}

func (g *Gateway) profile() {
go profileCPU(5 * time.Minute)
go profileMemory(5 * time.Minute)
}

func profileCPU(duration time.Duration) {
cpuFile, err := os.Create("cpu_profile.prof")
if err != nil {
log.Fatalf("Could not create CPU profile: %v", err)
}
defer cpuFile.Close()

pprof.StartCPUProfile(cpuFile)
time.Sleep(duration)
pprof.StopCPUProfile()
}

func profileMemory(duration time.Duration) {
time.Sleep(duration)
memFile, err := os.Create("mem_profile.prof")
if err != nil {
log.Fatalf("Could not create memory profile: %v", err)
}
defer memFile.Close()

runtime.GC()
pprof.WriteHeapProfile(memFile)
}

// Gateway entry point
func (g *Gateway) Start() error {
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", g.config.GatewayService.GRPCPort))
Expand Down Expand Up @@ -330,6 +362,8 @@ func (g *Gateway) Start() error {
}
}()

g.profile() // Profile cpu/mem usage

go func() {
if err := g.httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("Failed to start http server: %v", err)
Expand Down

0 comments on commit 6592e37

Please sign in to comment.