1

Adding some profiling code

This commit is contained in:
Alex Yatskov 2015-07-31 15:33:09 +09:00
parent 648d6a192d
commit 63df3adab1

View File

@ -30,9 +30,13 @@ import (
"log"
"math"
"net/http"
"os"
"os/signal"
"runtime"
"runtime/pprof"
"strings"
"sync"
"syscall"
"time"
"github.com/GaryBoone/GoStats/stats"
@ -309,6 +313,7 @@ func main() {
staticDir := flag.String("static", "static", "path to static files")
portNum := flag.Int("port", 8080, "port to serve content on")
dataSrc := flag.String("data", "hscd@/hscd", "data source for database")
profile := flag.String("profile", "", "write cpu profile to file")
flag.Parse()
var err error
@ -317,6 +322,25 @@ func main() {
}
defer db.Close()
if *profile != "" {
f, err := os.Create(*profile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-c
log.Print("interrupted")
pprof.StopCPUProfile()
os.Exit(1)
}()
}
http.HandleFunc("/query", executeQuery)
http.HandleFunc("/categories", getCategories)
http.HandleFunc("/learn", addCategory)