Explorar el Código

profiler werkt nu (niet web based)

Harry de Boer hace 10 años
padre
commit
b5f2401711
Se han modificado 2 ficheros con 17 adiciones y 11 borrados
  1. 6 0
      gameserver/urlrouter.go
  2. 11 11
      main.go

+ 6 - 0
gameserver/urlrouter.go

@@ -7,6 +7,7 @@ import (
 	"encoding/json"
 	"log"
 	"net/http"
+	"runtime/pprof"
 	"strconv"
 
 	"github.com/julienschmidt/httprouter"
@@ -27,6 +28,7 @@ func newUrlRouter(gs *GameServer) *httprouter.Router {
 	router.GET("/games/:gameid/:boardid", proxyHandler)
 	router.GET("/games/:gameid/:boardid/:playerid", showPlayer)
 	router.POST("/games/:gameid/move/:playerid/:direction", movePlayer)
+	router.GET("/debug/profiledump", profileDump)
 
 	return router
 }
@@ -169,3 +171,7 @@ func showAsciiBoard(w http.ResponseWriter, req *http.Request, ps httprouter.Para
 	b := gameServer.GetGame(id).Board.String()
 	w.Write([]byte(b))
 }
+
+func profileDump(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
+	pprof.StopCPUProfile()
+}

+ 11 - 11
main.go

@@ -3,9 +3,8 @@ package main
 
 import (
 	"log"
-	"net/http"
-	_ "net/http/pprof"
 	"os"
+	"runtime/pprof"
 
 	"battlecamp-go-server/flag"
 	"battlecamp-go-server/gameserver"
@@ -17,7 +16,16 @@ func main() {
 	initLogging()
 	log.Println("Game server version 0.1")
 
-	initCliFlags()
+	flag.ParseFlags()
+
+	if *flag.CpuProfile != "" {
+		f, err := os.Create(*flag.CpuProfile)
+		if err != nil {
+			log.Fatal(err)
+		}
+		pprof.StartCPUProfile(f)
+		defer pprof.StopCPUProfile()
+	}
 
 	gameServer.Serve()
 }
@@ -32,11 +40,3 @@ func initLogging() {
 		log.Printf("%v=n", err)
 	}
 }
-
-func initCliFlags() {
-	flag.ParseFlags()
-
-	if *flag.CpuProfile != "" {
-		log.Println(http.ListenAndServe("localhost:6060", nil))
-	}
-}