add profiling support for sdk
This commit is contained in:
@ -8,6 +8,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"runtime/pprof"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/snail007/goproxy/core/lib/kcpcfg"
|
"github.com/snail007/goproxy/core/lib/kcpcfg"
|
||||||
@ -31,6 +32,9 @@ var SDK_VERSION = "No Version Provided"
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
app *kingpin.Application
|
app *kingpin.Application
|
||||||
|
cpuProfilingFile, memProfilingFile, blockProfilingFile,
|
||||||
|
goroutineProfilingFile, threadcreateProfilingFile *os.File
|
||||||
|
isProfiling bool
|
||||||
)
|
)
|
||||||
|
|
||||||
type LogCallback interface {
|
type LogCallback interface {
|
||||||
@ -147,6 +151,7 @@ func StartWithLog(serviceID, serviceArgsStr string, loggerCallback LogCallback)
|
|||||||
//########tcp#########
|
//########tcp#########
|
||||||
tcp := app.Command("tcp", "proxy on tcp mode")
|
tcp := app.Command("tcp", "proxy on tcp mode")
|
||||||
tcpArgs.Parent = tcp.Flag("parent", "parent address, such as: \"23.32.32.19:28008\"").Default("").Short('P').String()
|
tcpArgs.Parent = tcp.Flag("parent", "parent address, such as: \"23.32.32.19:28008\"").Default("").Short('P').String()
|
||||||
|
kcpArgs.NoComp = app.Flag("kcp-nocomp", "disable compression").Default("false").Bool()
|
||||||
tcpArgs.CertFile = tcp.Flag("cert", "cert file for tls").Short('C').Default("proxy.crt").String()
|
tcpArgs.CertFile = tcp.Flag("cert", "cert file for tls").Short('C').Default("proxy.crt").String()
|
||||||
tcpArgs.KeyFile = tcp.Flag("key", "key file for tls").Short('K').Default("proxy.key").String()
|
tcpArgs.KeyFile = tcp.Flag("key", "key file for tls").Short('K').Default("proxy.key").String()
|
||||||
tcpArgs.Timeout = tcp.Flag("timeout", "tcp timeout milliseconds when connect to real server or parent proxy").Short('e').Default("2000").Int()
|
tcpArgs.Timeout = tcp.Flag("timeout", "tcp timeout milliseconds when connect to real server or parent proxy").Short('e').Default("2000").Int()
|
||||||
@ -477,3 +482,27 @@ func Stop(serviceID string) {
|
|||||||
func Version() string {
|
func Version() string {
|
||||||
return SDK_VERSION
|
return SDK_VERSION
|
||||||
}
|
}
|
||||||
|
func StartProfiling() {
|
||||||
|
isProfiling = true
|
||||||
|
cpuProfilingFile, _ = os.Create("cpu.prof")
|
||||||
|
memProfilingFile, _ = os.Create("memory.prof")
|
||||||
|
blockProfilingFile, _ = os.Create("block.prof")
|
||||||
|
goroutineProfilingFile, _ = os.Create("goroutine.prof")
|
||||||
|
threadcreateProfilingFile, _ = os.Create("threadcreate.prof")
|
||||||
|
pprof.StartCPUProfile(cpuProfilingFile)
|
||||||
|
}
|
||||||
|
func StopProfiling() {
|
||||||
|
if isProfiling {
|
||||||
|
isProfiling = false
|
||||||
|
goroutine := pprof.Lookup("goroutine")
|
||||||
|
goroutine.WriteTo(goroutineProfilingFile, 1)
|
||||||
|
heap := pprof.Lookup("heap")
|
||||||
|
heap.WriteTo(memProfilingFile, 1)
|
||||||
|
block := pprof.Lookup("block")
|
||||||
|
block.WriteTo(blockProfilingFile, 1)
|
||||||
|
threadcreate := pprof.Lookup("threadcreate")
|
||||||
|
threadcreate.WriteTo(threadcreateProfilingFile, 1)
|
||||||
|
pprof.StopCPUProfile()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
@ -39,6 +39,9 @@ func GetService(name string) *ServiceItem {
|
|||||||
func Stop(name string) {
|
func Stop(name string) {
|
||||||
if s, ok := servicesMap.Load(name); ok && s.(*ServiceItem).S != nil {
|
if s, ok := servicesMap.Load(name); ok && s.(*ServiceItem).S != nil {
|
||||||
s.(*ServiceItem).S.Clean()
|
s.(*ServiceItem).S.Clean()
|
||||||
|
*s.(*ServiceItem) = ServiceItem{}
|
||||||
|
s = nil
|
||||||
|
servicesMap.Store(name, nil)
|
||||||
servicesMap.Delete(name)
|
servicesMap.Delete(name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user