Prechádzať zdrojové kódy

Merge pull request #1950 from valyala/master

fasthttp: small fixes v2
Mike Smith 9 rokov pred
rodič
commit
cba8b53343

+ 1 - 1
frameworks/Go/fasthttp-mysql/README.md

@@ -1,4 +1,4 @@
-# [fasthttp](https://github.com/valyala/fasthttp) (GoLang) Benchmarking Test
+# [fasthttp](https://github.com/valyala/fasthttp) (GoLang) Benchmarking Test for mysql
 
 This is the go portion of a [benchmarking test suite](https://www.techempower.com/benchmarks/) comparing a variety of web development platforms.
 

+ 3 - 1
frameworks/Go/fasthttp-mysql/setup.sh

@@ -7,4 +7,6 @@ fw_depends go
 go get -u github.com/go-sql-driver/mysql
 go get -u github.com/valyala/fasthttp
 
-go run src/hello/hello.go &
+rm -f ./hello
+go build src/hello/hello.go
+./hello &

+ 3 - 1
frameworks/Go/fasthttp-mysql/setup_prefork.sh

@@ -7,4 +7,6 @@ fw_depends go
 go get -u github.com/go-sql-driver/mysql
 go get -u github.com/valyala/fasthttp
 
-go run src/hello/hello.go -prefork &
+rm -f ./hello
+go build src/hello/hello.go
+./hello -prefork &

+ 5 - 6
frameworks/Go/fasthttp-mysql/source_code

@@ -1,6 +1,5 @@
-./go/src/
-./go/src/hello
-./go/src/hello/hello.go
-./go/templates/
-./go/templates/fortune.html
-./go/templates/layout.html
+./fasthttp-mysql/src/
+./fasthttp-mysql/src/hello
+./fasthttp-mysql/src/hello/hello.go
+./fasthttp-mysql/templates/
+./fasthttp-mysql/templates/fortune.html

+ 13 - 20
frameworks/Go/fasthttp-mysql/src/hello/hello.go

@@ -12,15 +12,14 @@ import (
 	"os/exec"
 	"runtime"
 	"sort"
-	"sync/atomic"
-	"time"
+	"sync"
 
 	_ "github.com/go-sql-driver/mysql"
 	"github.com/valyala/fasthttp"
 	"github.com/valyala/fasthttp/reuseport"
 )
 
-type Message struct {
+type JSONResponse struct {
 	Message string `json:"message"`
 }
 
@@ -37,7 +36,7 @@ type Fortune struct {
 const (
 	connectionString   = "benchmarkdbuser:benchmarkdbpass@tcp(localhost:3306)/hello_world"
 	worldRowCount      = 10000
-	maxConnectionCount = 256
+	maxConnectionCount = 40
 )
 
 var (
@@ -94,22 +93,7 @@ func main() {
 	}
 }
 
-const maxConnDuration = time.Millisecond * 200
-
-var connDurationJitter uint64
-
 func mainHandler(ctx *fasthttp.RequestCtx) {
-	// Performance hack for prefork mode - periodically close keepalive
-	// connections for evenly distributing connections among available
-	// processes.
-	if *prefork {
-		maxDuration := maxConnDuration + time.Millisecond*time.Duration(atomic.LoadUint64(&connDurationJitter))
-		if time.Since(ctx.ConnTime()) > maxDuration {
-			atomic.StoreUint64(&connDurationJitter, uint64(rand.Intn(100)))
-			ctx.SetConnectionClose()
-		}
-	}
-
 	path := ctx.Path()
 	switch string(path) {
 	case "/plaintext":
@@ -131,7 +115,16 @@ func mainHandler(ctx *fasthttp.RequestCtx) {
 
 // Test 1: JSON serialization
 func jsonHandler(ctx *fasthttp.RequestCtx) {
-	jsonMarshal(ctx, &Message{helloWorldString})
+	r := jsonResponsePool.Get().(*JSONResponse)
+	r.Message = helloWorldString
+	jsonMarshal(ctx, r)
+	jsonResponsePool.Put(r)
+}
+
+var jsonResponsePool = &sync.Pool{
+	New: func() interface{} {
+		return &JSONResponse{}
+	},
 }
 
 // Test 2: Single database query

+ 1 - 1
frameworks/Go/fasthttp-postgresql/README.md

@@ -1,4 +1,4 @@
-# [fasthttp](https://github.com/valyala/fasthttp) (GoLang) Benchmarking Test
+# [fasthttp](https://github.com/valyala/fasthttp) (GoLang) Benchmarking Test for postgresql
 
 This is the go portion of a [benchmarking test suite](https://www.techempower.com/benchmarks/) comparing a variety of web development platforms.
 

+ 3 - 1
frameworks/Go/fasthttp-postgresql/setup.sh

@@ -7,4 +7,6 @@ fw_depends go
 go get -u github.com/jackc/pgx
 go get -u github.com/valyala/fasthttp
 
-go run src/hello/hello.go &
+rm -f ./hello
+go build src/hello/hello.go
+./hello &

+ 3 - 1
frameworks/Go/fasthttp-postgresql/setup_prefork.sh

@@ -7,4 +7,6 @@ fw_depends go
 go get -u github.com/jackc/pgx
 go get -u github.com/valyala/fasthttp
 
-go run src/hello/hello.go -prefork &
+rm -f ./hello
+go build src/hello/hello.go
+./hello -prefork &

+ 5 - 6
frameworks/Go/fasthttp-postgresql/source_code

@@ -1,6 +1,5 @@
-./go/src/
-./go/src/hello
-./go/src/hello/hello.go
-./go/templates/
-./go/templates/fortune.html
-./go/templates/layout.html
+./fasthttp-postgresql/src/
+./fasthttp-postgresql/src/hello
+./fasthttp-postgresql/src/hello/hello.go
+./fasthttp-postgresql/templates/
+./fasthttp-postgresql/templates/fortune.html

+ 13 - 20
frameworks/Go/fasthttp-postgresql/src/hello/hello.go

@@ -12,15 +12,14 @@ import (
 	"os/exec"
 	"runtime"
 	"sort"
-	"sync/atomic"
-	"time"
+	"sync"
 
 	"github.com/jackc/pgx"
 	"github.com/valyala/fasthttp"
 	"github.com/valyala/fasthttp/reuseport"
 )
 
-type Message struct {
+type JSONResponse struct {
 	Message string `json:"message"`
 }
 
@@ -36,7 +35,7 @@ type Fortune struct {
 
 const (
 	worldRowCount      = 10000
-	maxConnectionCount = 256
+	maxConnectionCount = 40
 )
 
 var (
@@ -85,22 +84,7 @@ func main() {
 	}
 }
 
-const maxConnDuration = time.Millisecond * 200
-
-var connDurationJitter uint64
-
 func mainHandler(ctx *fasthttp.RequestCtx) {
-	// Performance hack for prefork mode - periodically close keepalive
-	// connections for evenly distributing connections among available
-	// processes.
-	if *prefork {
-		maxDuration := maxConnDuration + time.Millisecond*time.Duration(atomic.LoadUint64(&connDurationJitter))
-		if time.Since(ctx.ConnTime()) > maxDuration {
-			atomic.StoreUint64(&connDurationJitter, uint64(rand.Intn(100)))
-			ctx.SetConnectionClose()
-		}
-	}
-
 	path := ctx.Path()
 	switch string(path) {
 	case "/plaintext":
@@ -122,7 +106,16 @@ func mainHandler(ctx *fasthttp.RequestCtx) {
 
 // Test 1: JSON serialization
 func jsonHandler(ctx *fasthttp.RequestCtx) {
-	jsonMarshal(ctx, &Message{helloWorldString})
+	r := jsonResponsePool.Get().(*JSONResponse)
+	r.Message = helloWorldString
+	jsonMarshal(ctx, r)
+	jsonResponsePool.Put(r)
+}
+
+var jsonResponsePool = &sync.Pool{
+	New: func() interface{} {
+		return &JSONResponse{}
+	},
 }
 
 // Test 2: Single database query