Skip to content

Commit

Permalink
feat: add profile for benchmark (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
HeyJavaBean authored Sep 27, 2024
1 parent bd3917d commit 9a29d3a
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 0 deletions.
19 changes: 19 additions & 0 deletions grpc/kitex/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,29 @@
package main

import (
"fmt"
"os"
"runtime/pprof"

"github.com/cloudwego/kitex-benchmark/runner"
)

// 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)
}
17 changes: 17 additions & 0 deletions grpc/kitex/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"fmt"
"log"
"net"
"os"
"runtime/pprof"

"github.com/cloudwego/kitex/server"

Expand Down Expand Up @@ -51,6 +53,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
18 changes: 18 additions & 0 deletions thrift/kitex/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
package main

import (
"fmt"
"os"
"runtime/pprof"
"time"

"github.com/cloudwego/kitex/client"
Expand All @@ -30,6 +33,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
17 changes: 17 additions & 0 deletions thrift/kitex/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"fmt"
"log"
"net"
"os"
"runtime/pprof"

"github.com/cloudwego/kitex/server"

Expand Down Expand Up @@ -60,6 +62,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 9a29d3a

Please sign in to comment.