Browse Source

Merge pull request #1968 from valyala/master

fasthttp: switch from html/template to github.com/valyala/quicktemplate
ssmith-techempower 9 years ago
parent
commit
57c5707c39

+ 13 - 25
frameworks/Go/fasthttp-mysql/src/hello/hello.go → frameworks/Go/fasthttp-mysql/server.go

@@ -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] }

+ 2 - 1
frameworks/Go/fasthttp-mysql/setup.bat

@@ -1,2 +1,3 @@
 set GOPATH=C:\FrameworkBenchmarks\Go\fasthttp-mysql
-go run src\hello\hello.go
+go build -o server
+.\server

+ 6 - 4
frameworks/Go/fasthttp-mysql/setup.sh

@@ -1,12 +1,14 @@
 #!/bin/bash
 
-sed -i 's|tcp(.*:3306)|tcp('"${DBHOST}"':3306)|g' src/hello/hello.go
+sed -i 's|tcp(.*:3306)|tcp('"${DBHOST}"':3306)|g' server.go
 
 fw_depends go
 
 go get -u github.com/go-sql-driver/mysql
 go get -u github.com/valyala/fasthttp
+go get -u github.com/valyala/quicktemplate/qtc
 
-rm -f ./hello
-go build src/hello/hello.go
-./hello &
+rm -f ./server
+go generate
+go build -o server
+./server &

+ 6 - 4
frameworks/Go/fasthttp-mysql/setup_prefork.sh

@@ -1,12 +1,14 @@
 #!/bin/bash
 
-sed -i 's|tcp(.*:3306)|tcp('"${DBHOST}"':3306)|g' src/hello/hello.go
+sed -i 's|tcp(.*:3306)|tcp('"${DBHOST}"':3306)|g' server.go
 
 fw_depends go
 
 go get -u github.com/go-sql-driver/mysql
 go get -u github.com/valyala/fasthttp
+go get -u github.com/valyala/quicktemplate/qtc
 
-rm -f ./hello
-go build src/hello/hello.go
-./hello -prefork &
+rm -f ./server
+go generate
+go build -o server
+./server -prefork &

+ 3 - 4
frameworks/Go/fasthttp-mysql/source_code

@@ -1,5 +1,4 @@
+./fasthttp-mysql/server.go
 ./fasthttp-mysql/src/
-./fasthttp-mysql/src/hello
-./fasthttp-mysql/src/hello/hello.go
-./fasthttp-mysql/templates/
-./fasthttp-mysql/templates/fortune.html
+./fasthttp-mysql/src/templates/
+./fasthttp-mysql/src/templates/fortune.qtpl

+ 22 - 0
frameworks/Go/fasthttp-mysql/src/templates/fortune.qtpl

@@ -0,0 +1,22 @@
+{% code
+type Fortune struct {
+	ID int
+	Message string
+}
+%}
+
+{% func FortunePage(rows []Fortune) %}<!DOCTYPE html>
+<html>
+<head>
+<title>Fortunes</title>
+</head>
+<body>
+<table>
+<tr><th>id</th><th>message</th></tr>
+{% for _, r := range rows %}
+<tr><td>{%d r.ID %}</td><td>{%s r.Message %}</td></tr>
+{% endfor %}
+</table>
+</body>
+</html>
+{% endfunc %}

+ 0 - 14
frameworks/Go/fasthttp-mysql/templates/fortune.html

@@ -1,14 +0,0 @@
-<!DOCTYPE html>
-<html>
-<head>
-<title>Fortunes</title>
-</head>
-<body>
-<table>
-<tr><th>id</th><th>message</th></tr>
-{{range .}}
-<tr><td>{{.Id}}</td><td>{{.Message}}</td></tr>
-{{end}}
-</table>
-</body>
-</html>

+ 13 - 25
frameworks/Go/fasthttp-postgresql/src/hello/hello.go → frameworks/Go/fasthttp-postgresql/server.go

@@ -1,10 +1,10 @@
+//go:generate qtc -dir=src/templates
 package main
 
 import (
 	"encoding/json"
 	"flag"
 	"fmt"
-	"html/template"
 	"log"
 	"math/rand"
 	"net"
@@ -17,6 +17,8 @@ import (
 	"github.com/jackc/pgx"
 	"github.com/valyala/fasthttp"
 	"github.com/valyala/fasthttp/reuseport"
+
+	"templates"
 )
 
 type JSONResponse struct {
@@ -28,11 +30,6 @@ type World struct {
 	RandomNumber int32 `json:"randomNumber"`
 }
 
-type Fortune struct {
-	Id      int32  `json:"id"`
-	Message string `json:"message"`
-}
-
 const (
 	worldRowCount      = 10000
 	maxConnectionCount = 40
@@ -42,16 +39,8 @@ var (
 	worldSelectStmt   *pgx.PreparedStatement
 	worldUpdateStmt   *pgx.PreparedStatement
 	fortuneSelectStmt *pgx.PreparedStatement
-)
-
-const helloWorldString = "Hello, World!"
-
-var (
-	tmpl = template.Must(template.ParseFiles("templates/fortune.html"))
 
 	db *pgx.ConnPool
-
-	helloWorldBytes = []byte(helloWorldString)
 )
 
 var (
@@ -76,7 +65,7 @@ func main() {
 
 	s := &fasthttp.Server{
 		Handler: mainHandler,
-		Name:    "fasthttp",
+		Name:    "go",
 	}
 	ln := getListener()
 	if err = s.Serve(ln); err != nil {
@@ -107,7 +96,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)
 }
@@ -144,23 +133,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
@@ -196,7 +183,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{}) {
@@ -228,7 +216,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] }

+ 2 - 1
frameworks/Go/fasthttp-postgresql/setup.bat

@@ -1,2 +1,3 @@
 set GOPATH=C:\FrameworkBenchmarks\Go\fasthttp-postgresql
-go run src\hello\hello.go
+go build -o server
+.\server

+ 6 - 4
frameworks/Go/fasthttp-postgresql/setup.sh

@@ -1,12 +1,14 @@
 #!/bin/bash
 
-sed -i 's|localhost|'"${DBHOST}"'|g' src/hello/hello.go
+sed -i 's|localhost|'"${DBHOST}"'|g' server.go
 
 fw_depends go
 
 go get -u github.com/jackc/pgx
 go get -u github.com/valyala/fasthttp
+go get -u github.com/valyala/quicktemplate/qtc
 
-rm -f ./hello
-go build src/hello/hello.go
-./hello &
+rm -f ./server
+go generate
+go build -o server
+./server &

+ 6 - 4
frameworks/Go/fasthttp-postgresql/setup_prefork.sh

@@ -1,12 +1,14 @@
 #!/bin/bash
 
-sed -i 's|localhost|'"${DBHOST}"'|g' src/hello/hello.go
+sed -i 's|localhost|'"${DBHOST}"'|g' server.go
 
 fw_depends go
 
 go get -u github.com/jackc/pgx
 go get -u github.com/valyala/fasthttp
+go get -u github.com/valyala/quicktemplate/qtc
 
-rm -f ./hello
-go build src/hello/hello.go
-./hello -prefork &
+rm -f ./server
+go generate
+go build -o server
+./server -prefork &

+ 3 - 4
frameworks/Go/fasthttp-postgresql/source_code

@@ -1,5 +1,4 @@
+./fasthttp-postgresql/server.go
 ./fasthttp-postgresql/src/
-./fasthttp-postgresql/src/hello
-./fasthttp-postgresql/src/hello/hello.go
-./fasthttp-postgresql/templates/
-./fasthttp-postgresql/templates/fortune.html
+./fasthttp-postgresql/src/templates/
+./fasthttp-postgresql/src/templates/fortune.qtpl

+ 22 - 0
frameworks/Go/fasthttp-postgresql/src/templates/fortune.qtpl

@@ -0,0 +1,22 @@
+{% code
+type Fortune struct {
+	ID int32
+	Message string
+}
+%}
+
+{% func FortunePage(rows []Fortune) %}<!DOCTYPE html>
+<html>
+<head>
+<title>Fortunes</title>
+</head>
+<body>
+<table>
+<tr><th>id</th><th>message</th></tr>
+{% for _, r := range rows %}
+<tr><td>{%d int(r.ID) %}</td><td>{%s r.Message %}</td></tr>
+{% endfor %}
+</table>
+</body>
+</html>
+{% endfunc %}

+ 0 - 14
frameworks/Go/fasthttp-postgresql/templates/fortune.html

@@ -1,14 +0,0 @@
-<!DOCTYPE html>
-<html>
-<head>
-<title>Fortunes</title>
-</head>
-<body>
-<table>
-<tr><th>id</th><th>message</th></tr>
-{{range .}}
-<tr><td>{{.Id}}</td><td>{{.Message}}</td></tr>
-{{end}}
-</table>
-</body>
-</html>

+ 1 - 1
toolset/setup/linux/languages/go.sh

@@ -13,7 +13,7 @@ fw_untar go$VERSION.linux-amd64.tar.gz
 
 echo "export GOROOT=${IROOT}/go" > $IROOT/go.installed
 echo -e "export GOPATH=\$TROOT" >> $IROOT/go.installed
-echo -e "export PATH=\$GOROOT/bin:\$PATH" >> $IROOT/go.installed
+echo -e "export PATH=\$GOROOT/bin:\$GOPATH/bin:\$PATH" >> $IROOT/go.installed
 echo "export GOGC=1000" >> $IROOT/go.installed
 
 source $IROOT/go.installed