|
@@ -1,10 +1,10 @@
|
|
|
+//go:generate qtc -dir=src/templates
|
|
|
package main
|
|
|
|
|
|
import (
|
|
|
"database/sql"
|
|
|
"encoding/json"
|
|
|
"flag"
|
|
|
- "html/template"
|
|
|
"log"
|
|
|
"math/rand"
|
|
|
"net"
|
|
@@ -17,6 +17,8 @@ import (
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
|
"github.com/valyala/fasthttp"
|
|
|
"github.com/valyala/fasthttp/reuseport"
|
|
|
+
|
|
|
+ "templates"
|
|
|
)
|
|
|
|
|
|
type JSONResponse struct {
|
|
@@ -28,11 +30,6 @@ type World struct {
|
|
|
RandomNumber uint16 `json:"randomNumber"`
|
|
|
}
|
|
|
|
|
|
-type Fortune struct {
|
|
|
- Id uint16 `json:"id"`
|
|
|
- Message string `json:"message"`
|
|
|
-}
|
|
|
-
|
|
|
const (
|
|
|
connectionString = "benchmarkdbuser:benchmarkdbpass@tcp(localhost:3306)/hello_world"
|
|
|
worldRowCount = 10000
|
|
@@ -43,16 +40,8 @@ var (
|
|
|
worldSelectStmt *sql.Stmt
|
|
|
worldUpdateStmt *sql.Stmt
|
|
|
fortuneSelectStmt *sql.Stmt
|
|
|
-)
|
|
|
-
|
|
|
-const helloWorldString = "Hello, World!"
|
|
|
-
|
|
|
-var (
|
|
|
- tmpl = template.Must(template.ParseFiles("templates/fortune.html"))
|
|
|
|
|
|
db *sql.DB
|
|
|
-
|
|
|
- helloWorldBytes = []byte(helloWorldString)
|
|
|
)
|
|
|
|
|
|
var (
|
|
@@ -85,7 +74,7 @@ func main() {
|
|
|
|
|
|
s := &fasthttp.Server{
|
|
|
Handler: mainHandler,
|
|
|
- Name: "fasthttp",
|
|
|
+ Name: "go",
|
|
|
}
|
|
|
ln := getListener()
|
|
|
if err = s.Serve(ln); err != nil {
|
|
@@ -116,7 +105,7 @@ func mainHandler(ctx *fasthttp.RequestCtx) {
|
|
|
// Test 1: JSON serialization
|
|
|
func jsonHandler(ctx *fasthttp.RequestCtx) {
|
|
|
r := jsonResponsePool.Get().(*JSONResponse)
|
|
|
- r.Message = helloWorldString
|
|
|
+ r.Message = "Hello, World!"
|
|
|
jsonMarshal(ctx, r)
|
|
|
jsonResponsePool.Put(r)
|
|
|
}
|
|
@@ -153,23 +142,21 @@ func fortuneHandler(ctx *fasthttp.RequestCtx) {
|
|
|
log.Fatalf("Error selecting db data: %v", err)
|
|
|
}
|
|
|
|
|
|
- fortunes := make([]Fortune, 0, 16)
|
|
|
+ fortunes := make([]templates.Fortune, 0, 16)
|
|
|
for rows.Next() {
|
|
|
- var f Fortune
|
|
|
- if err := rows.Scan(&f.Id, &f.Message); err != nil {
|
|
|
+ var f templates.Fortune
|
|
|
+ if err := rows.Scan(&f.ID, &f.Message); err != nil {
|
|
|
log.Fatalf("Error scanning fortune row: %s", err)
|
|
|
}
|
|
|
fortunes = append(fortunes, f)
|
|
|
}
|
|
|
rows.Close()
|
|
|
- fortunes = append(fortunes, Fortune{Message: "Additional fortune added at request time."})
|
|
|
+ fortunes = append(fortunes, templates.Fortune{Message: "Additional fortune added at request time."})
|
|
|
|
|
|
sort.Sort(FortunesByMessage(fortunes))
|
|
|
|
|
|
ctx.SetContentType("text/html; charset=utf-8")
|
|
|
- if err := tmpl.Execute(ctx, fortunes); err != nil {
|
|
|
- log.Fatalf("Error executing fortune: %s", err)
|
|
|
- }
|
|
|
+ templates.WriteFortunePage(ctx, fortunes)
|
|
|
}
|
|
|
|
|
|
// Test 5: Database updates
|
|
@@ -205,7 +192,8 @@ func updateHandler(ctx *fasthttp.RequestCtx) {
|
|
|
|
|
|
// Test 6: Plaintext
|
|
|
func plaintextHandler(ctx *fasthttp.RequestCtx) {
|
|
|
- ctx.Write(helloWorldBytes)
|
|
|
+ ctx.SetContentType("text/plain")
|
|
|
+ ctx.WriteString("Hello, World!")
|
|
|
}
|
|
|
|
|
|
func jsonMarshal(ctx *fasthttp.RequestCtx, v interface{}) {
|
|
@@ -236,7 +224,7 @@ func getQueriesCount(ctx *fasthttp.RequestCtx) int {
|
|
|
return n
|
|
|
}
|
|
|
|
|
|
-type FortunesByMessage []Fortune
|
|
|
+type FortunesByMessage []templates.Fortune
|
|
|
|
|
|
func (s FortunesByMessage) Len() int { return len(s) }
|
|
|
func (s FortunesByMessage) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|