Bläddra i källkod

gzip in board generator + cpu profile stuff + fix rng seed

Harry de Boer 10 år sedan
förälder
incheckning
d7e2991afa

+ 3 - 3
battlecamp-go-boardgenerator/boardgenerator.jmx

@@ -15,7 +15,7 @@
         <stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
         <elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="Loop Controller" enabled="true">
           <boolProp name="LoopController.continue_forever">false</boolProp>
-          <stringProp name="LoopController.loops">100</stringProp>
+          <stringProp name="LoopController.loops">10000</stringProp>
         </elementProp>
         <stringProp name="ThreadGroup.num_threads">10</stringProp>
         <stringProp name="ThreadGroup.ramp_time">1</stringProp>
@@ -30,8 +30,8 @@
           <elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
             <collectionProp name="Arguments.arguments"/>
           </elementProp>
-          <stringProp name="HTTPSampler.domain">localhost</stringProp>
-          <stringProp name="HTTPSampler.port">8081</stringProp>
+          <stringProp name="HTTPSampler.domain">battlecamp-go-boardgenerator.summercamp.local</stringProp>
+          <stringProp name="HTTPSampler.port">80</stringProp>
           <stringProp name="HTTPSampler.connect_timeout"></stringProp>
           <stringProp name="HTTPSampler.response_timeout"></stringProp>
           <stringProp name="HTTPSampler.protocol"></stringProp>

+ 31 - 0
battlecamp-go-boardgenerator/gzip.go

@@ -0,0 +1,31 @@
+package main
+
+import (
+	"compress/gzip"
+	"io"
+	"net/http"
+	"strings"
+)
+
+type gzipResponseWriter struct {
+	io.Writer
+	http.ResponseWriter
+}
+
+func (w gzipResponseWriter) Write(b []byte) (int, error) {
+	return w.Writer.Write(b)
+}
+
+func gzipHandler(fn http.HandlerFunc) http.HandlerFunc {
+	return func(w http.ResponseWriter, r *http.Request) {
+		if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
+			fn(w, r)
+			return
+		}
+		w.Header().Set("Content-Encoding", "gzip")
+		gz := gzip.NewWriter(w)
+		defer gz.Close()
+		gzr := gzipResponseWriter{Writer: gz, ResponseWriter: w}
+		fn(gzr, r)
+	}
+}

+ 6 - 5
battlecamp-go-boardgenerator/main.go

@@ -1,14 +1,15 @@
 package main
 
 import (
-	"fmt"
-	"net"
-	"golang.org/x/net/netutil"
 	"battlecamp-go-server/board"
+	"fmt"
 	"log"
+	"net"
 	"net/http"
 	"os"
 	"strconv"
+
+	"golang.org/x/net/netutil"
 )
 
 func main() {
@@ -22,7 +23,7 @@ func main() {
 		log.Printf("%v=n", err)
 	}
 
-	http.HandleFunc("/", generateBoard)
+	http.HandleFunc("/", gzipHandler(generateBoard))
 	http.HandleFunc("/status", status)
 	l, _ := net.Listen("tcp", ":8081")
 	limitListerer := netutil.LimitListener(l, 100)
@@ -36,7 +37,7 @@ func generateBoard(w http.ResponseWriter, r *http.Request) {
 	height, _ := strconv.Atoi(r.FormValue("height"))
 	totalWidth, _ := strconv.Atoi(r.FormValue("totalWidth"))
 	totalHeight, _ := strconv.Atoi(r.FormValue("totalHeight"))
-	log.Printf("Creating partial x=%v, y=%v, width=%v, height=%v, totalWidth=%v, totalHeight=%v", x, y, width, height, totalWidth, totalHeight)
+	//log.Printf("Creating partial x=%v, y=%v, width=%v, height=%v, totalWidth=%v, totalHeight=%v", x, y, width, height, totalWidth, totalHeight)
 	b := board.NewRegion(x, y, width, height, totalWidth, totalHeight)
 	b.WriteData(w)
 }

+ 5 - 5
board/board.go

@@ -9,8 +9,8 @@ import (
 	"math/rand"
 	"net/http"
 	"time"
-	
-	
+
+
 	"battlecamp-go-server/flag"
 )
 
@@ -31,8 +31,8 @@ type response struct {
 
 const (
 	maxIceWidth  = 26
-	regionWidth  = 32
-	regionHeight = 32
+	regionWidth  = 1024
+	regionHeight = 1024
 )
 
 // Create a new randomly generated board.
@@ -41,7 +41,7 @@ func New(width, height int) *Board {
 }
 
 func NewRegion(startX, startY, width, height, totalWidth, totalHeight int) *Board {
-	r := rand.New(rand.NewSource(time.Now().UnixNano() + int64(startY+startY)))
+	r := rand.New(rand.NewSource(time.Now().UnixNano() + int64(startY*startY)))
 	return newRegion(startX, startY, width, height, totalWidth, totalHeight, r)
 }
 

+ 1 - 0
gameserver/urlrouter.go

@@ -173,5 +173,6 @@ func showAsciiBoard(w http.ResponseWriter, req *http.Request, ps httprouter.Para
 }
 
 func profileDump(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
+	log.Println("Dumping cpu profile")
 	pprof.StopCPUProfile()
 }

+ 0 - 1
main.go

@@ -24,7 +24,6 @@ func main() {
 			log.Fatal(err)
 		}
 		pprof.StartCPUProfile(f)
-		defer pprof.StopCPUProfile()
 	}
 
 	gameServer.Serve()