Skip to content

Commit

Permalink
feat/add_profile
Browse files Browse the repository at this point in the history
  • Loading branch information
HeyJavaBean committed Sep 26, 2024
1 parent bd3917d commit 2e53a4b
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
15 changes: 15 additions & 0 deletions grpc/kitex/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,20 @@ import (

// main is use for routing.
func main() {
if os.Getenv("KITEX_ENABLE_PROFILE") == "1" {
fmt.Println("[Kitex profile is enabled]")
// start cpu profile
cpuProfile, _ := os.Create("output/benchmark-grpc-client-cpu.pprof")
defer cpuProfile.Close()
_ = pprof.StartCPUProfile(cpuProfile)
defer pprof.StopCPUProfile()

// heap profile after finish
heapProfile, _ := os.Create("output/benchmark-grpc-client-mem.pprof")
defer func() {
_ = pprof.WriteHeapProfile(heapProfile)
heapProfile.Close()
}()
}
runner.Main("KITEX", NewKClient)
}
15 changes: 15 additions & 0 deletions grpc/kitex/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@ func (s *EchoImpl) Echo(ctx context.Context, req *echo.Request) (*echo.Response,
}

func main() {
if os.Getenv("KITEX_ENABLE_PROFILE") == "1" {
fmt.Println("[Kitex profile is enabled]")
// start cpu profile
cpuProfile, _ := os.Create("output/benchmark-grpc-server-cpu.pprof")
defer cpuProfile.Close()
_ = pprof.StartCPUProfile(cpuProfile)
defer pprof.StopCPUProfile()

// heap profile after finish
heapProfile, _ := os.Create("output/benchmark-grpc-server-mem.pprof")
defer func() {
_ = pprof.WriteHeapProfile(heapProfile)
heapProfile.Close()
}()
}
// start pprof server
go func() {
perf.ServeMonitor(fmt.Sprintf(":%d", port+10000))
Expand Down
14 changes: 14 additions & 0 deletions scripts/base.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ body=(1024)
concurrent=(100 200 400 600 800 1000)
qps=(0)
sleep=0
# NOTICE: if you want to dump profile, set "enable_profile" to 1
enable_profile=0
if [ $enable_profile -eq 1 ]; then
export KITEX_ENABLE_PROFILE=1
fi

CURDIR=$(cd $(dirname $0); pwd)

Expand Down Expand Up @@ -74,6 +79,15 @@ function kill_pid_listening_on_port() {
exit 1
fi
pids=`lsof -i ":$port" | grep LISTEN | awk '{print $2}' | uniq`

for p in $pids; do
echo "Sending termination signal to $p..."
kill -s SIGTERM $p # 发送 SIGTERM 信号给程序
done

# 给程序一定时间处理信号后再强制终止
sleep 2

for p in $pids; do
echo killing $p...
kill $p
Expand Down
15 changes: 15 additions & 0 deletions thrift/kitex/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ import (

// main is use for routing.
func main() {
if os.Getenv("KITEX_ENABLE_PROFILE") == "1" {
fmt.Println("[Kitex profile is enabled]")
// start cpu profile
cpuProfile, _ := os.Create("output/benchmark-thrift-client-cpu.pprof")
defer cpuProfile.Close()
_ = pprof.StartCPUProfile(cpuProfile)
defer pprof.StopCPUProfile()

// heap profile after finish
heapProfile, _ := os.Create("output/benchmark-thrift-client-mem.pprof")
defer func() {
_ = pprof.WriteHeapProfile(heapProfile)
heapProfile.Close()
}()
}
runner.Main("KITEX", NewThriftKitexClient)
}

Expand Down
15 changes: 15 additions & 0 deletions thrift/kitex/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,21 @@ func (s *EchoServerImpl) EchoComplex(ctx context.Context, req *echo.ComplexReque
}

func main() {
if os.Getenv("KITEX_ENABLE_PROFILE") == "1" {
fmt.Println("[Kitex profile is enabled]")
// start cpu profile
cpuProfile, _ := os.Create("output/benchmark-thrift-server-cpu.pprof")
defer cpuProfile.Close()
_ = pprof.StartCPUProfile(cpuProfile)
defer pprof.StopCPUProfile()

// heap profile after finish
heapProfile, _ := os.Create("output/benchmark-thrift-server-mem.pprof")
defer func() {
_ = pprof.WriteHeapProfile(heapProfile)
heapProfile.Close()
}()
}
// start pprof server
go func() {
perf.ServeMonitor(fmt.Sprintf(":%d", port+10000))
Expand Down

0 comments on commit 2e53a4b

Please sign in to comment.