Skip to content

Commit

Permalink
add profiling infra (commented out in main.go)
Browse files Browse the repository at this point in the history
  • Loading branch information
bnfinet committed May 14, 2020
1 parent 5728584 commit 13ed4af
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
12 changes: 11 additions & 1 deletion do.sh
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,15 @@ _has_license() {
echo $(grep -P 'Copyright \d\d\d\d The Vouch Proxy Authors' ${FILE})
}

profile() {
echo "for profiling to work you may need uncomment the code in main.go"
build
./vouch-proxy -profile
go tool pprof -http=0.0.0.0:19091 http://0.0.0.0:9090/debug/pprof/profile?seconds=10

}


usage() {
cat <<EOF
usage:
Expand All @@ -299,6 +308,7 @@ usage() {
$0 test [./pkg_test.go] - run go tests (defaults to all tests)
$0 test_logging - test the logging output
$0 coverage - coverage report
$0 profile - go pprof tools
$0 bug_report domain.com - print config file removing secrets and each provided domain
$0 gogo [gocmd] - run, build, any go cmd
$0 stats - simple metrics (lines of code in project, number of go files)
Expand All @@ -315,7 +325,7 @@ EOF
ARG=$1;

case "$ARG" in
'run'|'build'|'dbuild'|'drun'|'install'|'test'|'goget'|'gogo'|'watch'|'gobuildstatic'|'coverage'|'stats'|'usage'|'bug_report'|'test_logging'|'license')
'run'|'build'|'dbuild'|'drun'|'install'|'test'|'goget'|'gogo'|'watch'|'gobuildstatic'|'coverage'|'stats'|'usage'|'bug_report'|'test_logging'|'license'|'profile')
shift
$ARG $*
;;
Expand Down
23 changes: 23 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import (
"strconv"
"time"

// "net/http/pprof"

"github.com/gorilla/mux"
"go.uber.org/zap"

Expand All @@ -58,6 +60,7 @@ var (
logger *zap.SugaredLogger
fastlog *zap.Logger
help = flag.Bool("help", false, "show usage")
// doProfile = flag.Bool("profile", false, "run profiler at /debug/pprof")
)

// fwdToZapWriter allows us to use the zap.Logger as our http.Server ErrorLog
Expand Down Expand Up @@ -147,6 +150,11 @@ func main() {
// https://golangcode.com/serve-static-assets-using-the-mux-router/
muxR.PathPrefix(staticDir).Handler(http.StripPrefix(staticDir, http.FileServer(http.Dir(sPath))))

//
// if *doProfile {
// addProfilingHandlers(muxR)
// }

srv := &http.Server{
Handler: muxR,
Addr: listen,
Expand All @@ -171,3 +179,18 @@ func checkTCPPortAvailable(listen string) {
logger.Error(err)
}
}

// if you'd like to enable profiling uncomment these
// func addProfilingHandlers(muxR *mux.Router) {
// // https://stackoverflow.com/questions/47452471/pprof-profile-with-julienschmidtrouter-and-benchmarks-not-profiling-handler
// logger.Debugf("profiling routes added at http://%s:%d/debug/pprof/", cfg.Cfg.Listen, cfg.Cfg.Port)
// muxR.HandleFunc("/debug/pprof/", pprof.Index)
// muxR.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
// muxR.HandleFunc("/debug/pprof/profile", pprof.Profile)
// muxR.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
// muxR.HandleFunc("/debug/pprof/trace", pprof.Trace)
// muxR.Handle("/debug/pprof/goroutine", pprof.Handler("goroutine"))
// muxR.Handle("/debug/pprof/heap", pprof.Handler("heap"))
// muxR.Handle("/debug/pprof/threadcreate", pprof.Handler("threadcreate"))
// muxR.Handle("/debug/pprof/block", pprof.Handler("block"))
// }

0 comments on commit 13ed4af

Please sign in to comment.