aboutsummaryrefslogtreecommitdiff
path: root/cmd/receivepack9418/profile.go
diff options
context:
space:
mode:
authorGravatar Runxi Yu2026-03-08 13:03:55 +0800
committerGravatar Runxi Yu2026-03-08 13:03:55 +0800
commit0bf0a124641dc5f7594b65a3991a43f0ecc07ddb (patch)
tree586012a23f65e761caf3284e2e141d91017835d3 /cmd/receivepack9418/profile.go
parentTODO: Improve delta resolution perf (diff)
signatureNo signature
cmd/receivepack9418: Profile
Diffstat (limited to 'cmd/receivepack9418/profile.go')
-rw-r--r--cmd/receivepack9418/profile.go58
1 files changed, 58 insertions, 0 deletions
diff --git a/cmd/receivepack9418/profile.go b/cmd/receivepack9418/profile.go
new file mode 100644
index 00000000..40ba1d56
--- /dev/null
+++ b/cmd/receivepack9418/profile.go
@@ -0,0 +1,58 @@
+package main
+
+import (
+ "fmt"
+ "os"
+ "runtime"
+ "runtime/pprof"
+)
+
+func startCPUProfile(path string) (func() error, error) {
+ //#nosec G304
+ file, err := os.Create(path)
+ if err != nil {
+ return nil, fmt.Errorf("create %q: %w", path, err)
+ }
+
+ err = pprof.StartCPUProfile(file)
+ if err != nil {
+ _ = file.Close()
+
+ return nil, fmt.Errorf("start cpu profile %q: %w", path, err)
+ }
+
+ return func() error {
+ pprof.StopCPUProfile()
+
+ err := file.Close()
+ if err != nil {
+ return fmt.Errorf("close cpu profile %q: %w", path, err)
+ }
+
+ return nil
+ }, nil
+}
+
+func writeMemProfile(path string) error {
+ //#nosec G304
+ file, err := os.Create(path)
+ if err != nil {
+ return fmt.Errorf("create %q: %w", path, err)
+ }
+
+ runtime.GC()
+
+ err = pprof.WriteHeapProfile(file)
+ if err != nil {
+ _ = file.Close()
+
+ return fmt.Errorf("write heap profile %q: %w", path, err)
+ }
+
+ err = file.Close()
+ if err != nil {
+ return fmt.Errorf("close heap profile %q: %w", path, err)
+ }
+
+ return nil
+}