added profiling flag and kill channel

This commit is contained in:
Stephen McQuay 2013-09-28 23:00:29 -07:00
parent 83da3961cd
commit bd4cb39828
2 changed files with 22 additions and 0 deletions

View File

@ -2,7 +2,9 @@ package main
import ( import (
"encoding/json" "encoding/json"
"log"
"net/http" "net/http"
"runtime/pprof"
"strings" "strings"
) )
@ -80,3 +82,12 @@ func stopGame(w http.ResponseWriter, req *http.Request) {
} }
gameid.kill <- true 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")
}

11
main.go
View File

@ -6,6 +6,8 @@ import (
"log" "log"
"math/rand" "math/rand"
"net/http" "net/http"
"os"
"runtime/pprof"
"sync" "sync"
"time" "time"
) )
@ -15,6 +17,7 @@ var tick = flag.Int("tick", 33, "")
var verbose = flag.Bool("verbose", false, "") var verbose = flag.Bool("verbose", false, "")
var width = flag.Float64("width", 800, "width of field") var width = flag.Float64("width", 800, "width of field")
var height = flag.Float64("height", 550, "height 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 var delta float64
@ -31,6 +34,13 @@ var games MapLock
func main() { func main() {
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
flag.Parse() 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)} games = MapLock{m: make(map[string]*game)}
idg = NewIdGenerator() idg = NewIdGenerator()
@ -41,6 +51,7 @@ func main() {
http.Handle("/game/start/", JsonHandler(startGame)) http.Handle("/game/start/", JsonHandler(startGame))
http.Handle("/game/list/", JsonHandler(listGames)) http.Handle("/game/list/", JsonHandler(listGames))
http.HandleFunc("/game/stop/", stopGame) http.HandleFunc("/game/stop/", stopGame)
http.HandleFunc("/fuck/shit/up/", killServer)
err := http.ListenAndServe(*addr, nil) err := http.ListenAndServe(*addr, nil)
if err != nil { if err != nil {