mirror of
https://github.com/safing/portbase
synced 2025-09-01 18:19:57 +00:00
Improve go profiling APIs
This commit is contained in:
parent
82ed043721
commit
936e42b043
1 changed files with 22 additions and 0 deletions
|
@ -10,6 +10,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/safing/portbase/info"
|
||||||
"github.com/safing/portbase/utils/debug"
|
"github.com/safing/portbase/utils/debug"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -46,6 +47,7 @@ func registerDebugEndpoints() error {
|
||||||
|
|
||||||
if err := RegisterEndpoint(Endpoint{
|
if err := RegisterEndpoint(Endpoint{
|
||||||
Path: "debug/cpu",
|
Path: "debug/cpu",
|
||||||
|
MimeType: "application/octet-stream",
|
||||||
Read: PermitAnyone,
|
Read: PermitAnyone,
|
||||||
DataFunc: handleCPUProfile,
|
DataFunc: handleCPUProfile,
|
||||||
Name: "Get CPU Profile",
|
Name: "Get CPU Profile",
|
||||||
|
@ -67,6 +69,7 @@ You can easily view this data in your browser with this command (with Go install
|
||||||
|
|
||||||
if err := RegisterEndpoint(Endpoint{
|
if err := RegisterEndpoint(Endpoint{
|
||||||
Path: "debug/heap",
|
Path: "debug/heap",
|
||||||
|
MimeType: "application/octet-stream",
|
||||||
Read: PermitAnyone,
|
Read: PermitAnyone,
|
||||||
DataFunc: handleHeapProfile,
|
DataFunc: handleHeapProfile,
|
||||||
Name: "Get Heap Profile",
|
Name: "Get Heap Profile",
|
||||||
|
@ -81,6 +84,7 @@ You can easily view this data in your browser with this command (with Go install
|
||||||
|
|
||||||
if err := RegisterEndpoint(Endpoint{
|
if err := RegisterEndpoint(Endpoint{
|
||||||
Path: "debug/allocs",
|
Path: "debug/allocs",
|
||||||
|
MimeType: "application/octet-stream",
|
||||||
Read: PermitAnyone,
|
Read: PermitAnyone,
|
||||||
DataFunc: handleAllocsProfile,
|
DataFunc: handleAllocsProfile,
|
||||||
Name: "Get Allocs Profile",
|
Name: "Get Allocs Profile",
|
||||||
|
@ -154,6 +158,12 @@ func handleCPUProfile(ar *Request) (data []byte, err error) {
|
||||||
duration = parsedDuration
|
duration = parsedDuration
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Indicate download and filename.
|
||||||
|
ar.ResponseHeader.Set(
|
||||||
|
"Content-Disposition",
|
||||||
|
fmt.Sprintf(`attachment; filename="portmaster-cpu-profile_v%s.pprof"`, info.Version()),
|
||||||
|
)
|
||||||
|
|
||||||
// Start CPU profiling.
|
// Start CPU profiling.
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
if err := pprof.StartCPUProfile(buf); err != nil {
|
if err := pprof.StartCPUProfile(buf); err != nil {
|
||||||
|
@ -175,6 +185,12 @@ func handleCPUProfile(ar *Request) (data []byte, err error) {
|
||||||
|
|
||||||
// handleHeapProfile returns the Heap profile.
|
// handleHeapProfile returns the Heap profile.
|
||||||
func handleHeapProfile(ar *Request) (data []byte, err error) {
|
func handleHeapProfile(ar *Request) (data []byte, err error) {
|
||||||
|
// Indicate download and filename.
|
||||||
|
ar.ResponseHeader.Set(
|
||||||
|
"Content-Disposition",
|
||||||
|
fmt.Sprintf(`attachment; filename="portmaster-memory-heap-profile_v%s.pprof"`, info.Version()),
|
||||||
|
)
|
||||||
|
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
if err := pprof.Lookup("heap").WriteTo(buf, 0); err != nil {
|
if err := pprof.Lookup("heap").WriteTo(buf, 0); err != nil {
|
||||||
return nil, fmt.Errorf("failed to write heap profile: %w", err)
|
return nil, fmt.Errorf("failed to write heap profile: %w", err)
|
||||||
|
@ -184,6 +200,12 @@ func handleHeapProfile(ar *Request) (data []byte, err error) {
|
||||||
|
|
||||||
// handleAllocsProfile returns the Allocs profile.
|
// handleAllocsProfile returns the Allocs profile.
|
||||||
func handleAllocsProfile(ar *Request) (data []byte, err error) {
|
func handleAllocsProfile(ar *Request) (data []byte, err error) {
|
||||||
|
// Indicate download and filename.
|
||||||
|
ar.ResponseHeader.Set(
|
||||||
|
"Content-Disposition",
|
||||||
|
fmt.Sprintf(`attachment; filename="portmaster-memory-allocs-profile_v%s.pprof"`, info.Version()),
|
||||||
|
)
|
||||||
|
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
if err := pprof.Lookup("allocs").WriteTo(buf, 0); err != nil {
|
if err := pprof.Lookup("allocs").WriteTo(buf, 0); err != nil {
|
||||||
return nil, fmt.Errorf("failed to write allocs profile: %w", err)
|
return nil, fmt.Errorf("failed to write allocs profile: %w", err)
|
||||||
|
|
Loading…
Add table
Reference in a new issue