Sfoglia il codice sorgente

add cpu profile flag

Harry de Boer 10 anni fa
parent
commit
47465d18fb
3 ha cambiato i file con 17 aggiunte e 17 eliminazioni
  1. 4 2
      flag/flag.go
  2. 0 9
      games/game.go
  3. 13 6
      main.go

+ 4 - 2
flag/flag.go

@@ -2,11 +2,13 @@ package flag
 
 import "flag"
 
-var StompUrl, BoardGenUrl *string
+var StompUrl, BoardGenUrl, CpuProfile *string
 var Port *int
 
-func CreateFlags() {
+func ParseFlags() {
 	Port = flag.Int("port", 8080, "The port of the battlecamp-go-server")
 	BoardGenUrl = flag.String("boardGen", "localhost:8081", "The url for the board region generator")
 	StompUrl = flag.String("stompUrl", "localhost:61613", "The url for active mq stomp")
+	CpuProfile = flag.String("cpuprofile", "", "write cpu profile to file")
+	flag.Parse()
 }

+ 0 - 9
games/game.go

@@ -6,8 +6,6 @@ import (
 	"battlecamp-go-server/stomp"
 	"log"
 	"math/rand"
-	"os"
-	"runtime/pprof"
 	"sync"
 	"time"
 )
@@ -26,13 +24,6 @@ type Game struct {
 func NewGame(cols, rows int) *Game {
 	createTime := time.Now().Unix()
 
-	f, err := os.Create("cpuprofile.log")
-	if err != nil {
-		log.Fatal(err)
-	}
-	pprof.StartCPUProfile(f)
-	defer pprof.StopCPUProfile()
-
 	game := &Game{
 		StartTime: createTime,
 		Board:     board.NewRemote(cols, rows),

+ 13 - 6
main.go

@@ -2,26 +2,25 @@
 package main
 
 import (
-	"flag"
 	"log"
 	"net/http"
 	"os"
+	"runtime/pprof"
 	"strconv"
 
-	bcFlag "battlecamp-go-server/flag"
+	"battlecamp-go-server/flag"
 	"battlecamp-go-server/games"
 )
 
 var currentGames games.GameServer = games.New()
 
-
 func main() {
 	initLogging()
 	log.Println("Game server version 0.1")
 
 	initCliFlags()
 
-	log.Fatal(http.ListenAndServe(":"+strconv.Itoa(*bcFlag.Port), newUrlRouter()))
+	log.Fatal(http.ListenAndServe(":"+strconv.Itoa(*flag.Port), newUrlRouter()))
 }
 
 func initLogging() {
@@ -36,6 +35,14 @@ func initLogging() {
 }
 
 func initCliFlags() {
-	bcFlag.CreateFlags()
-	flag.Parse()
+	flag.ParseFlags()
+
+	if *flag.CpuProfile != "" {
+		f, err := os.Create(*flag.CpuProfile)
+		if err != nil {
+			log.Fatal(err)
+		}
+		pprof.StartCPUProfile(f)
+		defer pprof.StopCPUProfile()
+	}
 }