From 5061fb96f5e8a4318fcaadbe129bdd25104496a4 Mon Sep 17 00:00:00 2001 From: Remco Date: Thu, 19 May 2016 17:36:44 +0200 Subject: [PATCH] added profiling --- Dockerfile | 2 +- transfersh-server/main.go | 45 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index aa633b0..fe9ba76 100644 --- a/Dockerfile +++ b/Dockerfile @@ -15,4 +15,4 @@ RUN go install . ENTRYPOINT ["/go/bin/app", "--port", "8080"] -EXPOSE 8080 +EXPOSE 8080 6060 diff --git a/transfersh-server/main.go b/transfersh-server/main.go index 7b34fdc..dd3aa82 100644 --- a/transfersh-server/main.go +++ b/transfersh-server/main.go @@ -35,11 +35,17 @@ import ( "net/http" "net/url" "os" + "os/signal" "runtime" + "syscall" "time" "github.com/PuerkitoBio/ghost/handlers" "github.com/gorilla/mux" + + "github.com/pkg/profile" + + _ "net/http/pprof" ) const SERVER_INFO = "transfer.sh" @@ -79,6 +85,30 @@ func main() { runtime.GOMAXPROCS(nCPU) fmt.Println("Number of CPUs: ", nCPU) + var profiler interface { + Stop() + } = nil + + profiler = profile.Start(profile.CPUProfile, profile.ProfilePath("."), profile.NoShutdownHook) + /* + if c.GlobalBool("cpu-profile") { + log.Info("CPU profiler started.") + profiler = profile.Start(profile.CPUProfile, profile.ProfilePath("."), profile.NoShutdownHook) + } else if c.GlobalBool("mem-profile") { + log.Info("Memory profiler started.") + profiler = profile.Start(profile.MemProfile, profile.ProfilePath("."), profile.NoShutdownHook) + } + + if c.GlobalBool("profiler") { + log.Info("Profiler listening.") + + } + */ + + go func() { + http.ListenAndServe(":6060", nil) + }() + r := mux.NewRouter() r.PathPrefix("/scripts/").Methods("GET").Handler(http.FileServer(http.Dir("./static/"))) @@ -178,6 +208,19 @@ func main() { Handler: handlers.PanicHandler(LoveHandler(RedirectHandler(handlers.LogHandler(r, handlers.NewLogOptions(log.Printf, "_default_")))), nil), } - log.Panic(s.ListenAndServe()) + go func() { + log.Panic(s.ListenAndServe()) + }() + + term := make(chan os.Signal, 1) + signal.Notify(term, os.Interrupt) + signal.Notify(term, syscall.SIGTERM) + + <-term + + if profiler != nil { + profiler.Stop() + } + log.Printf("Server stopped.") }