|
@@ -12,15 +12,14 @@ import (
|
|
"os/exec"
|
|
"os/exec"
|
|
"runtime"
|
|
"runtime"
|
|
"sort"
|
|
"sort"
|
|
- "sync/atomic"
|
|
|
|
- "time"
|
|
|
|
|
|
+ "sync"
|
|
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
_ "github.com/go-sql-driver/mysql"
|
|
"github.com/valyala/fasthttp"
|
|
"github.com/valyala/fasthttp"
|
|
"github.com/valyala/fasthttp/reuseport"
|
|
"github.com/valyala/fasthttp/reuseport"
|
|
)
|
|
)
|
|
|
|
|
|
-type Message struct {
|
|
|
|
|
|
+type JSONResponse struct {
|
|
Message string `json:"message"`
|
|
Message string `json:"message"`
|
|
}
|
|
}
|
|
|
|
|
|
@@ -37,7 +36,7 @@ type Fortune struct {
|
|
const (
|
|
const (
|
|
connectionString = "benchmarkdbuser:benchmarkdbpass@tcp(localhost:3306)/hello_world"
|
|
connectionString = "benchmarkdbuser:benchmarkdbpass@tcp(localhost:3306)/hello_world"
|
|
worldRowCount = 10000
|
|
worldRowCount = 10000
|
|
- maxConnectionCount = 256
|
|
|
|
|
|
+ maxConnectionCount = 40
|
|
)
|
|
)
|
|
|
|
|
|
var (
|
|
var (
|
|
@@ -94,22 +93,7 @@ func main() {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-const maxConnDuration = time.Millisecond * 200
|
|
|
|
-
|
|
|
|
-var connDurationJitter uint64
|
|
|
|
-
|
|
|
|
func mainHandler(ctx *fasthttp.RequestCtx) {
|
|
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()
|
|
path := ctx.Path()
|
|
switch string(path) {
|
|
switch string(path) {
|
|
case "/plaintext":
|
|
case "/plaintext":
|
|
@@ -131,7 +115,16 @@ func mainHandler(ctx *fasthttp.RequestCtx) {
|
|
|
|
|
|
// Test 1: JSON serialization
|
|
// Test 1: JSON serialization
|
|
func jsonHandler(ctx *fasthttp.RequestCtx) {
|
|
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
|
|
// Test 2: Single database query
|