Przeglądaj źródła

Cpu profiling van board generator. + gzip naar bestSpeed

Harry de Boer 10 lat temu
rodzic
commit
b4bb639070

+ 1 - 1
battlecamp-go-boardgenerator/gzip.go

@@ -23,7 +23,7 @@ func gzipHandler(fn http.HandlerFunc) http.HandlerFunc {
 			return
 		}
 		w.Header().Set("Content-Encoding", "gzip")
-		gz := gzip.NewWriter(w)
+		gz, _ := gzip.NewWriterLevel(w, gzip.BestSpeed)
 		defer gz.Close()
 		gzr := gzipResponseWriter{Writer: gz, ResponseWriter: w}
 		fn(gzr, r)

+ 23 - 0
battlecamp-go-boardgenerator/main.go

@@ -2,16 +2,20 @@ package main
 
 import (
 	"battlecamp-go-server/board"
+	"flag"
 	"fmt"
 	"log"
 	"net"
 	"net/http"
 	"os"
+	"runtime/pprof"
 	"strconv"
 
 	"golang.org/x/net/netutil"
 )
 
+var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
+
 func main() {
 	fmt.Println("Board generator version 0.1")
 	logFile, err := os.Create("server.log")
@@ -23,8 +27,11 @@ func main() {
 		log.Printf("%v=n", err)
 	}
 
+	initCpuProfiler()
+
 	http.HandleFunc("/", gzipHandler(generateBoard))
 	http.HandleFunc("/status", status)
+	http.HandleFunc("/debug/profiledump", profileDump)
 	l, _ := net.Listen("tcp", ":8081")
 	limitListerer := netutil.LimitListener(l, 100)
 	log.Fatal(http.Serve(limitListerer, nil))
@@ -45,3 +52,19 @@ func generateBoard(w http.ResponseWriter, r *http.Request) {
 func status(w http.ResponseWriter, r *http.Request) {
 	w.Write([]byte("ok"))
 }
+
+func profileDump(w http.ResponseWriter, req *http.Request) {
+	log.Println("Dumping cpu profile")
+	pprof.StopCPUProfile()
+}
+
+func initCpuProfiler() {
+	flag.Parse()
+	if *cpuprofile != "" {
+		f, err := os.Create(*cpuprofile)
+		if err != nil {
+			log.Fatal(err)
+		}
+		pprof.StartCPUProfile(f)
+	}
+}