Browse Source

Fiber 💨 v0.8.1 (#5392)

* Fiber: v0.5.5 - Add database tests

* Update fw

* v0.6.1

* Merging

* v0.6.9

* v0.8.1
Fenny 5 năm trước cách đây
mục cha
commit
09649a129e

+ 8 - 28
frameworks/Go/fiber/README.md

@@ -1,36 +1,16 @@
-<img src="https://fenny.github.io/fiber/static/logo.jpg" width="150" alt="Fiber"><br><br>
-**[Fiber](https://github.com/fenny/fiber)** is a router framework build on top of **[FastHTTP](https://github.com/valyala/fasthttp)**, the fastest HTTP package for **[Go](https://golang.org/doc/)**.<br>
+<img src="https://gofiber.github.io/fiber/static/logo.jpg" width="150" alt="Fiber"><br><br>
+**[Fiber](https://github.com/gofiber/fiber)** is a router framework build on top of **[FastHTTP](https://github.com/valyala/fasthttp)**, the fastest HTTP package for **[Go](https://golang.org/doc/)**.<br>
 This library is inspired by **[Express](https://expressjs.com/en/4x/api.html)**, one of the most populair and well known web framework for **[Nodejs](https://nodejs.org/en/about/)**.
 
-### Github Source
-* [Fiber](https://github.com/fenny/fiber)
+### Github
+* [Fiber](https://github.com/gofiber/fiber)
 
 ## Version Tested
-* [Fiber v0.4.0](https://github.com/fenny/fiber/releases)
+* [Fiber v0.8.0](https://github.com/gofiber/fiber/releases)
 
 ## Test URLs
 * http://localhost:8080/json
+* http://localhost:8080/db
+* http://localhost:8080/queries?queries=
+* http://localhost:8080/update?queries=
 * http://localhost:8080/plaintext
-
-## Benchmark Source
-```go
-package main
-
-import "github.com/fenny/fiber"
-
-type JSON struct {
-	Message string `json:"message"`
-}
-
-func main() {
-	app := fiber.New()
-	app.Settings.Name = "Fiber"
-	app.Get("/plaintext", func(c *fiber.Ctx) {
-		c.Send("Hello, World!")
-	})
-	app.Get("/json", func(c *fiber.Ctx) {
-		c.Json(JSON{"Hello, World!"})
-	})
-	app.Listen(8080)
-}
-```

+ 4 - 1
frameworks/Go/fiber/benchmark_config.json

@@ -3,11 +3,14 @@
   "tests": [{
     "default": {
       "json_url": "/json",
+      "db_url": "/db",
+      "query_url": "/queries?queries=",
+      "update_url": "/update?queries=",
       "plaintext_url": "/plaintext",
       "port": 8080,
       "approach": "Realistic",
       "classification": "Platform",
-      "database": "None",
+      "database": "MySQL",
       "framework": "Fiber",
       "language": "Go",
       "flavor": "None",

+ 3 - 4
frameworks/Go/fiber/src/go.mod

@@ -3,8 +3,7 @@ module fiber
 go 1.13
 
 require (
-	github.com/fatih/color v1.9.0 // indirect
-	github.com/fenny/fiber v0.4.0
-	github.com/json-iterator/go v1.1.9 // indirect
-	github.com/valyala/fasthttp v1.7.1 // indirect
+	github.com/go-sql-driver/mysql v1.5.0
+	github.com/gofiber/fiber v0.8.1
+	github.com/valyala/fasthttp v1.8.0
 )

+ 6 - 14
frameworks/Go/fiber/src/go.sum

@@ -1,9 +1,9 @@
 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
-github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s=
-github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
-github.com/fenny/fiber v0.4.0 h1:oNT+wKVhOM4zCGvnN2EWzjhlQueKvheY4XaqMLzP7xM=
-github.com/fenny/fiber v0.4.0/go.mod h1:H858+oCv2+vNMCqVY8+bpcyeavpqcqWKL8MbNIjC24o=
+github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
+github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
+github.com/gofiber/fiber v0.8.1 h1:RFJyD708eCfYj/L9iJCOUj+tunTL+H7vMf+wyBoKDAM=
+github.com/gofiber/fiber v0.8.1/go.mod h1:H4p5UQjAUsewuosS8vFHQwMJMe1oUrX1QQgvDNx8s20=
 github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
 github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
 github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
@@ -11,11 +11,6 @@ github.com/klauspost/compress v1.8.2 h1:Bx0qjetmNjdFXASH02NSAREKpiaDwkO1DRZ3dV2K
 github.com/klauspost/compress v1.8.2/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
 github.com/klauspost/cpuid v1.2.1 h1:vJi+O/nMdFt0vqm8NZBI6wzALWdA2X+egi0ogNyrC/w=
 github.com/klauspost/cpuid v1.2.1/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
-github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
-github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
-github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
-github.com/mattn/go-isatty v0.0.11 h1:FxPOTFNqGkuDUGi3H/qkUbQO4ZiBa2brKq5r0l8TGeM=
-github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
 github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
 github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
 github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
@@ -25,13 +20,10 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
 github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
 github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
 github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
-github.com/valyala/fasthttp v1.7.1 h1:UHtt5/7O70RSUZTR/hSu0PNWMAfWx5AtsPp9Jk+g17M=
-github.com/valyala/fasthttp v1.7.1/go.mod h1:FstJa9V+Pj9vQ7OJie2qMHdwemEDaDiSdBnvPM1Su9w=
+github.com/valyala/fasthttp v1.8.0 h1:actnGGBYtGQmxVaZxyZpp57Vcc2NhcO7mMN0IMwCC0w=
+github.com/valyala/fasthttp v1.8.0/go.mod h1:FstJa9V+Pj9vQ7OJie2qMHdwemEDaDiSdBnvPM1Su9w=
 github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio=
 golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
 golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
 golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
-golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
-golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
-golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

+ 142 - 8
frameworks/Go/fiber/src/server.go

@@ -1,19 +1,153 @@
 package main
 
-import "github.com/fenny/fiber"
+import (
+	"database/sql"
+	"math/rand"
+	"runtime"
+	"sort"
+	"strconv"
+	"sync"
 
+	_ "github.com/go-sql-driver/mysql"
+	"github.com/gofiber/fiber"
+)
+
+const (
+	WorldCount = 10000
+	Text       = "Hello, World!"
+)
+
+var (
+	db                *sql.DB
+	stmtWorldSelect   *sql.Stmt
+	stmtWorldUpdate   *sql.Stmt
+	stmtFortuneSelect *sql.Stmt
+)
+
+// JSON struct for json serialization
 type JSON struct {
 	Message string `json:"message"`
 }
 
+// World struct for single database query
+type World struct {
+	Id           int32 `json:"id"`
+	RandomNumber int32 `json:"randomNumber"`
+}
+
+// Worlds struct for multiple database queries
+type Worlds []World
+
 func main() {
+	connectDB()
 	app := fiber.New()
-	app.Settings.Name = "Fiber"
-	app.Get("/plaintext", func(c *fiber.Ctx) {
-		c.Send("Hello, World!")
-	})
-	app.Get("/json", func(c *fiber.Ctx) {
-		c.Json(JSON{"Hello, World!"})
-	})
+	app.Server = "Fiber"
+	app.Get("/json", jsonSerialization)
+	app.Get("/db", singleDatabaseQuery)
+	app.Get("/queries", multipleDatabaseQueries)
+	app.Get("/update", databaseUpdates)
+	app.Get("/plaintext", plainText)
 	app.Listen(8080)
 }
+
+// https://github.com/TechEmpower/FrameworkBenchmarks/wiki/Project-Information-Framework-Tests-Overview#json-serialization
+func jsonSerialization(c *fiber.Ctx) {
+	j := AcquireJSON()
+	j.Message = Text
+	c.Json(j)
+	ReleaseJSON(j)
+}
+
+// https://github.com/TechEmpower/FrameworkBenchmarks/wiki/Project-Information-Framework-Tests-Overview#single-database-query
+func singleDatabaseQuery(c *fiber.Ctx) {
+	var json World
+	stmtWorldSelect.QueryRow(RandomWorld()).Scan(&json.Id, &json.RandomNumber)
+	c.Json(json)
+}
+
+// https://github.com/TechEmpower/FrameworkBenchmarks/wiki/Project-Information-Framework-Tests-Overview#multiple-database-queries
+func multipleDatabaseQueries(c *fiber.Ctx) {
+	n := QueriesCount(c)
+	worlds := make([]World, n)
+	for i := 0; i < n; i++ {
+		w := &worlds[i]
+		stmtWorldSelect.QueryRow(RandomWorld()).Scan(&w.Id, &w.RandomNumber)
+	}
+	c.Json(Worlds(worlds))
+}
+
+// https://github.com/TechEmpower/FrameworkBenchmarks/wiki/Project-Information-Framework-Tests-Overview#database-updates
+func databaseUpdates(c *fiber.Ctx) {
+	n := QueriesCount(c)
+	worlds := make([]World, n)
+	for i := 0; i < n; i++ {
+		w := &worlds[i]
+		stmtWorldSelect.QueryRow(RandomWorld()).Scan(&w.Id, &w.RandomNumber)
+		w.RandomNumber = int32(RandomWorld())
+	}
+	// sorting is required for insert deadlock prevention.
+	sort.Slice(worlds, func(i, j int) bool {
+		return worlds[i].Id < worlds[j].Id
+	})
+
+	txn, _ := db.Begin()
+	stmt := txn.Stmt(stmtWorldUpdate)
+	for i := 0; i < n; i++ {
+		w := &worlds[i]
+		stmt.Exec(w.RandomNumber, w.Id)
+	}
+	txn.Commit()
+	c.Json(Worlds(worlds))
+}
+
+// https://github.com/TechEmpower/FrameworkBenchmarks/wiki/Project-Information-Framework-Tests-Overview#plaintext
+func plainText(c *fiber.Ctx) {
+	c.SendString(Text)
+}
+
+// ************************
+//    Helper functions
+// ************************
+
+func connectDB() {
+	db, _ = sql.Open("mysql", "benchmarkdbuser:benchmarkdbpass@tcp(tfb-database:3306)/hello_world")
+	db.SetMaxIdleConns(runtime.NumCPU() * 2)
+	db.SetMaxOpenConns(runtime.NumCPU() * 2)
+	stmtWorldSelect, _ = db.Prepare("SELECT id, randomNumber FROM World WHERE id = ?")
+	stmtWorldUpdate, _ = db.Prepare("UPDATE World SET randomNumber = ? WHERE id = ?")
+	stmtFortuneSelect, _ = db.Prepare("SELECT id, message FROM Fortune")
+}
+
+// JSONpool :
+var JSONpool = sync.Pool{
+	New: func() interface{} {
+		return new(JSON)
+	},
+}
+
+// AcquireJSON returns new message from pool
+func AcquireJSON() *JSON {
+	return JSONpool.Get().(*JSON)
+}
+
+// ReleaseJSON resets the message and return it to the pool
+func ReleaseJSON(j *JSON) {
+	j.Message = ""
+	JSONpool.Put(j)
+}
+
+// RandomWorld :
+func RandomWorld() int {
+	return rand.Intn(WorldCount) + 1
+}
+
+// QueriesCount :
+func QueriesCount(c *fiber.Ctx) int {
+	n, _ := strconv.Atoi(c.Query("queries"))
+	if n < 1 {
+		n = 1
+	} else if n > 500 {
+		n = 500
+	}
+	return n
+}