added profiling

This commit is contained in:
Remco 2016-05-19 17:36:44 +02:00
parent 8ed9a5d5ca
commit 5061fb96f5
2 changed files with 45 additions and 2 deletions

View file

@ -15,4 +15,4 @@ RUN go install .
ENTRYPOINT ["/go/bin/app", "--port", "8080"] ENTRYPOINT ["/go/bin/app", "--port", "8080"]
EXPOSE 8080 EXPOSE 8080 6060

View file

@ -35,11 +35,17 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"os/signal"
"runtime" "runtime"
"syscall"
"time" "time"
"github.com/PuerkitoBio/ghost/handlers" "github.com/PuerkitoBio/ghost/handlers"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/pkg/profile"
_ "net/http/pprof"
) )
const SERVER_INFO = "transfer.sh" const SERVER_INFO = "transfer.sh"
@ -79,6 +85,30 @@ func main() {
runtime.GOMAXPROCS(nCPU) runtime.GOMAXPROCS(nCPU)
fmt.Println("Number of CPUs: ", 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 := mux.NewRouter()
r.PathPrefix("/scripts/").Methods("GET").Handler(http.FileServer(http.Dir("./static/"))) 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), Handler: handlers.PanicHandler(LoveHandler(RedirectHandler(handlers.LogHandler(r, handlers.NewLogOptions(log.Printf, "_default_")))), nil),
} }
go func() {
log.Panic(s.ListenAndServe()) 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.") log.Printf("Server stopped.")
} }