This is an automated email from the ASF dual-hosted git repository.

spacewander pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix-go-plugin-runner.git


The following commit(s) were added to refs/heads/master by this push:
     new 6e33937  feat: add an entrance for profile (#8)
6e33937 is described below

commit 6e33937d6c3d6216978ee1fed41e8211a1458151
Author: 罗泽轩 <spacewander...@gmail.com>
AuthorDate: Fri Jun 18 10:04:07 2021 +0800

    feat: add an entrance for profile (#8)
---
 .gitignore            |  1 +
 cmd/go-runner/main.go | 34 +++++++++++++++++++++++++++++++++-
 2 files changed, 34 insertions(+), 1 deletion(-)

diff --git a/.gitignore b/.gitignore
index d3ab02b..590e1b6 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,3 +6,4 @@ release/
 .DS_Store
 coverage.txt
 logs/
+*.svg
diff --git a/cmd/go-runner/main.go b/cmd/go-runner/main.go
index 881dbc1..98a0e56 100644
--- a/cmd/go-runner/main.go
+++ b/cmd/go-runner/main.go
@@ -20,6 +20,8 @@ import (
        "log"
        "os"
        "path/filepath"
+       "runtime"
+       "runtime/pprof"
 
        "github.com/spf13/cobra"
        "github.com/thediveo/enumflag"
@@ -55,13 +57,16 @@ type RunMode enumflag.Flag
 const (
        Dev  RunMode = iota // Development
        Prod                // Product
+       Prof                // Profile
 
-       LogFilePath = "./logs/runner.log"
+       ProfileFilePath = "./logs/profile."
+       LogFilePath     = "./logs/runner.log"
 )
 
 var RunModeIds = map[RunMode][]string{
        Prod: {"prod"},
        Dev:  {"dev"},
+       Prof: {"prof"},
 }
 
 func openFileToWrite(name string) (*os.File, error) {
@@ -94,6 +99,33 @@ func newRunCommand() *cobra.Command {
                                        log.Fatalf("failed to open log: %s", 
err)
                                }
                                cfg.LogOutput = f
+                       } else if mode == Prof {
+                               cfg.LogLevel = zapcore.WarnLevel
+
+                               cpuProfileFile := ProfileFilePath + "cpu"
+                               f, err := os.Create(cpuProfileFile)
+                               if err != nil {
+                                       log.Fatal("could not create CPU 
profile: ", err)
+                               }
+                               defer f.Close()
+                               if err := pprof.StartCPUProfile(f); err != nil {
+                                       log.Fatal("could not start CPU profile: 
", err)
+                               }
+                               defer pprof.StopCPUProfile()
+
+                               defer func() {
+                                       memProfileFile := ProfileFilePath + 
"mem"
+                                       f, err := os.Create(memProfileFile)
+                                       if err != nil {
+                                               log.Fatal("could not create 
memory profile: ", err)
+                                       }
+                                       defer f.Close()
+
+                                       runtime.GC()
+                                       if err := pprof.WriteHeapProfile(f); 
err != nil {
+                                               log.Fatal("could not write 
memory profile: ", err)
+                                       }
+                               }()
                        }
                        runner.Run(cfg)
                },

Reply via email to