diff --git a/control.go b/control.go index f9328c0..3d6483f 100644 --- a/control.go +++ b/control.go @@ -2,7 +2,9 @@ package main import ( "encoding/json" + "log" "net/http" + "runtime/pprof" "strings" ) @@ -80,3 +82,12 @@ func stopGame(w http.ResponseWriter, req *http.Request) { } gameid.kill <- true } + +func killServer(w http.ResponseWriter, req *http.Request) { + if *profile != "" { + log.Print("trying to stop cpu profile") + pprof.StopCPUProfile() + log.Print("stopped cpu profile") + } + log.Fatal("shit got fucked up") +} diff --git a/main.go b/main.go index e7c073e..05cfbe0 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,8 @@ import ( "log" "math/rand" "net/http" + "os" + "runtime/pprof" "sync" "time" ) @@ -15,6 +17,7 @@ var tick = flag.Int("tick", 33, "") var verbose = flag.Bool("verbose", false, "") var width = flag.Float64("width", 800, "width of field") var height = flag.Float64("height", 550, "height of field") +var profile = flag.String("pprof", "", "if specified will run with pprof") var delta float64 @@ -31,6 +34,13 @@ var games MapLock func main() { rand.Seed(time.Now().UnixNano()) flag.Parse() + if *profile != "" { + f, err := os.Create(*profile) + if err != nil { + log.Fatal(err) + } + pprof.StartCPUProfile(f) + } games = MapLock{m: make(map[string]*game)} idg = NewIdGenerator() @@ -41,6 +51,7 @@ func main() { http.Handle("/game/start/", JsonHandler(startGame)) http.Handle("/game/list/", JsonHandler(listGames)) http.HandleFunc("/game/stop/", stopGame) + http.HandleFunc("/fuck/shit/up/", killServer) err := http.ListenAndServe(*addr, nil) if err != nil {