Browse Source

Add support for chi router (#3520)

* Add support for chi router

* Make use of new strings.Builder

Included in go 1.10

* Set charset=utf-8 to elimninate warning
David Cuadrado 7 years ago
parent
commit
85e1bc9187

+ 1 - 0
.travis.yml

@@ -60,6 +60,7 @@ env:
      - "TESTDIR=Go/fasthttp"
      - "TESTDIR=Go/gin"
      - "TESTDIR=Go/goji"
+     - "TESTDIR=Go/chi"
      - "TESTDIR=Go/go-std"
      - "TESTDIR=Go/revel"
      - "TESTDIR=Go/webgo"

+ 2 - 0
frameworks/Go/chi/.gitignore

@@ -0,0 +1,2 @@
+src/github.com/
+pkg/

+ 17 - 0
frameworks/Go/chi/README.md

@@ -0,0 +1,17 @@
+# [Chi](https://github.com/go-chi/chi) (Go) Benchmarking Test
+
+This is the go portion of a [benchmarking test suite](../) comparing a variety of web development platforms.
+
+"Chi is a lightweight, idiomatic and composable router for building Go HTTP services."
+
+### Source
+* [All test source](src/chi/server.go)
+
+## Test URLs
+
+    http://localhost:8080/json
+    http://localhost:8080/db
+    http://localhost:8080/queries?queries=[1-500]
+    http://localhost:8080/fortunes
+    http://localhost:8080/updates?queries=[1-500]
+    http://localhost:8080/plaintext

+ 28 - 0
frameworks/Go/chi/benchmark_config.json

@@ -0,0 +1,28 @@
+{
+  "framework": "chi",
+  "tests": [{
+    "default": {
+      "json_url": "/json",
+      "db_url": "/db",
+      "query_url": "/queries?queries=",
+      "fortune_url": "/fortunes",
+      "update_url": "/updates?queries=",
+      "plaintext_url": "/plaintext",
+      "port": 8080,
+      "approach": "Realistic",
+      "classification": "Micro",
+      "database": "Postgres",
+      "framework": "Chi",
+      "language": "Go",
+      "flavor": "None",
+      "orm": "Raw",
+      "platform": "None",
+      "webserver": "None",
+      "os": "Linux",
+      "database_os": "Linux",
+      "display_name": "Chi",
+      "notes": "",
+      "versus": "go"
+    }
+  }]
+}

+ 14 - 0
frameworks/Go/chi/chi.dockerfile

@@ -0,0 +1,14 @@
+FROM golang:1.10.1
+
+ADD ./ /chi
+WORKDIR /chi
+
+RUN mkdir bin
+ENV GOPATH /chi
+ENV PATH ${GOPATH}/bin:${PATH}
+
+RUN go get github.com/mailru/easyjson/...
+RUN go get github.com/jackc/pgx
+RUN go get github.com/go-chi/chi
+
+CMD go run src/chi/*.go

+ 256 - 0
frameworks/Go/chi/src/chi/server.go

@@ -0,0 +1,256 @@
+package main
+
+import (
+	"encoding/json"
+	"flag"
+	"fmt"
+	"html"
+	"io"
+	"io/ioutil"
+	"log"
+	"math/rand"
+	"net/http"
+	"sort"
+	"strconv"
+	"strings"
+
+	"github.com/go-chi/chi"
+	"github.com/jackc/pgx"
+)
+
+const (
+	htmlTemplate = `<!DOCTYPE html>
+<html>
+<head><title>Fortunes</title></head>
+<body><table><tr><th>id</th><th>message</th></tr>%s</table></body>
+</html>`
+	fortuneTemplate = `<tr><td>%d</td><td>%s</td></tr>`
+)
+
+const (
+	// Database
+	worldSelect        = "SELECT id, randomNumber FROM World WHERE id = $1"
+	worldUpdate        = "UPDATE World SET randomNumber = $1 WHERE id = $2"
+	fortuneSelect      = "SELECT id, message FROM Fortune;"
+	worldRowCount      = 10000
+	maxConnectionCount = 256
+
+	helloWorldString    = "Hello, World!"
+	extraFortuneMessage = "Additional fortune added at request time."
+)
+
+var (
+	connectionString = "postgres://benchmarkdbuser:benchmarkdbpass@tfb-database/hello_world?sslmode=disable"
+	bindHost         = ":8080"
+	debug            = false
+
+	// Database
+	helloWorldMessage = &Message{helloWorldString}
+	db                *pgx.ConnPool
+)
+
+// Message is a JSON struct to render a message
+type Message struct {
+	Message string `json:"message"`
+}
+
+// World is a JSON struct to render a random number
+type World struct {
+	ID           uint16 `json:"id"`
+	RandomNumber uint16 `json:"randomNumber"`
+}
+
+func randomRow() *pgx.Row {
+	return db.QueryRow("worldSelect", rand.Intn(worldRowCount)+1)
+}
+
+// Fortune renders a fortune in JSON
+type Fortune struct {
+	ID      uint16 `json:"id"`
+	Message string `json:"message"`
+}
+
+// Fortunes is a list of fortunes
+type Fortunes []*Fortune
+
+// Len returns the length of the fortunes list
+func (s Fortunes) Len() int {
+	return len(s)
+}
+
+// Swap swaps fortunes at the given positions
+func (s Fortunes) Swap(i, j int) {
+	s[i], s[j] = s[j], s[i]
+}
+
+// Sets the content type of response. Also adds the Server header.
+func setContentType(w http.ResponseWriter, contentType string) {
+	w.Header().Set("Server", "Chi")
+	w.Header().Set("Content-Type", contentType)
+}
+
+// Test 1: JSON Serialization
+func serializeJSON(w http.ResponseWriter, r *http.Request) {
+	setContentType(w, "application/json")
+	_ = json.NewEncoder(w).Encode(helloWorldMessage)
+}
+
+// Test 2: Single Database Query
+func singleQuery(w http.ResponseWriter, r *http.Request) {
+	world := World{}
+	if err := randomRow().Scan(&world.ID, &world.RandomNumber); err != nil {
+		log.Printf("Error scanning world row: %s", err.Error())
+	}
+
+	setContentType(w, "application/json")
+	_ = json.NewEncoder(w).Encode(&world)
+}
+
+// Caps queries parameter between 1 and 500.
+// Non-int values like "foo" and "" become 1.
+func sanitizeQueryParam(queries string) int {
+	n, _ := strconv.Atoi(queries)
+
+	if n <= 0 {
+		return 1
+	}
+
+	if n > 500 {
+		return 500
+	}
+
+	return n
+}
+
+// Test 3: Multiple Database Queries
+func multipleQueries(w http.ResponseWriter, r *http.Request) {
+	queries := sanitizeQueryParam(r.URL.Query().Get("queries"))
+	worlds := make([]World, queries)
+
+	for i := 0; i < queries; i++ {
+		if err := randomRow().Scan(&worlds[i].ID, &worlds[i].RandomNumber); err != nil {
+			log.Printf("Error scanning world row: %s", err.Error())
+		}
+	}
+
+	setContentType(w, "application/json")
+	_ = json.NewEncoder(w).Encode(worlds)
+}
+
+// Test 4: Fortunes
+func fortunes(w http.ResponseWriter, r *http.Request) {
+	rows, err := db.Query("fortuneSelect")
+	if err != nil {
+		log.Printf("Error preparing statement: %v", err)
+	}
+
+	fortunes := make(Fortunes, 0, 256)
+
+	for rows.Next() {
+		fortune := Fortune{}
+		if err := rows.Scan(&fortune.ID, &fortune.Message); err != nil {
+			log.Printf("Error scanning fortune row: %s", err.Error())
+		}
+		fortunes = append(fortunes, &fortune)
+	}
+	rows.Close()
+	fortunes = append(fortunes, &Fortune{Message: extraFortuneMessage})
+
+	sort.Slice(fortunes, func(i, j int) bool {
+		return fortunes[i].Message < fortunes[j].Message
+	})
+	setContentType(w, "text/html; charset=utf-8")
+
+	var body strings.Builder
+	for _, fortune := range fortunes {
+		fmt.Fprintf(&body, fortuneTemplate, fortune.ID, html.EscapeString(fortune.Message))
+	}
+
+	fmt.Fprintf(w, htmlTemplate, body.String())
+}
+
+// Test 5: Database Updates
+func dbupdate(w http.ResponseWriter, r *http.Request) {
+	queries := sanitizeQueryParam(r.URL.Query().Get("queries"))
+	worlds := make([]World, queries)
+
+	for i := 0; i < queries; i++ {
+		w := &worlds[i]
+		if err := randomRow().Scan(&w.ID, &w.RandomNumber); err != nil {
+			log.Printf("Error scanning world row: %s", err.Error())
+		}
+		worlds[i].RandomNumber = uint16(rand.Intn(worldRowCount) + 1)
+		if _, err := db.Exec("worldUpdate", w.RandomNumber, w.ID); err != nil {
+			log.Printf("Error updating world row: %s", err.Error())
+		}
+	}
+
+	setContentType(w, "application/json")
+	_ = json.NewEncoder(w).Encode(worlds)
+}
+
+// Test 6: Plaintext
+func plaintext(w http.ResponseWriter, r *http.Request) {
+	setContentType(w, "text/plain")
+	_, _ = io.WriteString(w, helloWorldString)
+}
+
+func init() {
+	flag.StringVar(&bindHost, "bind", bindHost, "Set bind host")
+	flag.StringVar(&connectionString, "db", connectionString, "Set database URL")
+	flag.BoolVar(&debug, "debug", false, "Enable debug mode")
+	flag.Parse()
+}
+
+func main() {
+	if !debug {
+		log.SetOutput(ioutil.Discard)
+	}
+
+	config, err := pgx.ParseConnectionString(connectionString)
+	if err != nil {
+		flag.PrintDefaults()
+		log.Fatalf("Error parsing db URL: %v", err)
+	}
+
+	connPoolConfig := pgx.ConnPoolConfig{
+		ConnConfig:     config,
+		MaxConnections: maxConnectionCount,
+	}
+
+	db, err = pgx.NewConnPool(connPoolConfig)
+	if err != nil {
+		flag.PrintDefaults()
+		log.Fatalf("Error opening database: %v", err)
+	}
+
+	_, err = db.Prepare("worldSelect", worldSelect)
+	if err != nil {
+		flag.PrintDefaults()
+		log.Fatal(err)
+	}
+	_, err = db.Prepare("fortuneSelect", fortuneSelect)
+	if err != nil {
+		flag.PrintDefaults()
+		log.Fatal(err)
+	}
+	_, err = db.Prepare("worldUpdate", worldUpdate)
+	if err != nil {
+		flag.PrintDefaults()
+		log.Fatal(err)
+	}
+
+	r := chi.NewRouter()
+
+	r.Get("/json", serializeJSON)
+	r.Get("/db", singleQuery)
+	r.Get("/queries", multipleQueries)
+	r.Get("/fortunes", fortunes)
+	r.Get("/plaintext", plaintext)
+	r.Get("/updates", dbupdate)
+
+	err = http.ListenAndServe(bindHost, r)
+	if err != nil {
+		log.Fatal(err)
+	}
+}

+ 257 - 0
frameworks/Go/chi/src/chi/server_easyjson.go

@@ -0,0 +1,257 @@
+// Code generated by easyjson for marshaling/unmarshaling. DO NOT EDIT.
+
+package main
+
+import (
+	json "encoding/json"
+	easyjson "github.com/mailru/easyjson"
+	jlexer "github.com/mailru/easyjson/jlexer"
+	jwriter "github.com/mailru/easyjson/jwriter"
+)
+
+// suppress unused package warning
+var (
+	_ *json.RawMessage
+	_ *jlexer.Lexer
+	_ *jwriter.Writer
+	_ easyjson.Marshaler
+)
+
+func easyjson22b57fa5DecodeGithubComTechEmpowerFrameworkBenchmarksFrameworksGoChiSrcChi(in *jlexer.Lexer, out *World) {
+	isTopLevel := in.IsStart()
+	if in.IsNull() {
+		if isTopLevel {
+			in.Consumed()
+		}
+		in.Skip()
+		return
+	}
+	in.Delim('{')
+	for !in.IsDelim('}') {
+		key := in.UnsafeString()
+		in.WantColon()
+		if in.IsNull() {
+			in.Skip()
+			in.WantComma()
+			continue
+		}
+		switch key {
+		case "id":
+			out.ID = uint16(in.Uint16())
+		case "randomNumber":
+			out.RandomNumber = uint16(in.Uint16())
+		default:
+			in.SkipRecursive()
+		}
+		in.WantComma()
+	}
+	in.Delim('}')
+	if isTopLevel {
+		in.Consumed()
+	}
+}
+func easyjson22b57fa5EncodeGithubComTechEmpowerFrameworkBenchmarksFrameworksGoChiSrcChi(out *jwriter.Writer, in World) {
+	out.RawByte('{')
+	first := true
+	_ = first
+	{
+		const prefix string = ",\"id\":"
+		if first {
+			first = false
+			out.RawString(prefix[1:])
+		} else {
+			out.RawString(prefix)
+		}
+		out.Uint16(uint16(in.ID))
+	}
+	{
+		const prefix string = ",\"randomNumber\":"
+		if first {
+			first = false
+			out.RawString(prefix[1:])
+		} else {
+			out.RawString(prefix)
+		}
+		out.Uint16(uint16(in.RandomNumber))
+	}
+	out.RawByte('}')
+}
+
+// MarshalJSON supports json.Marshaler interface
+func (v World) MarshalJSON() ([]byte, error) {
+	w := jwriter.Writer{}
+	easyjson22b57fa5EncodeGithubComTechEmpowerFrameworkBenchmarksFrameworksGoChiSrcChi(&w, v)
+	return w.Buffer.BuildBytes(), w.Error
+}
+
+// MarshalEasyJSON supports easyjson.Marshaler interface
+func (v World) MarshalEasyJSON(w *jwriter.Writer) {
+	easyjson22b57fa5EncodeGithubComTechEmpowerFrameworkBenchmarksFrameworksGoChiSrcChi(w, v)
+}
+
+// UnmarshalJSON supports json.Unmarshaler interface
+func (v *World) UnmarshalJSON(data []byte) error {
+	r := jlexer.Lexer{Data: data}
+	easyjson22b57fa5DecodeGithubComTechEmpowerFrameworkBenchmarksFrameworksGoChiSrcChi(&r, v)
+	return r.Error()
+}
+
+// UnmarshalEasyJSON supports easyjson.Unmarshaler interface
+func (v *World) UnmarshalEasyJSON(l *jlexer.Lexer) {
+	easyjson22b57fa5DecodeGithubComTechEmpowerFrameworkBenchmarksFrameworksGoChiSrcChi(l, v)
+}
+func easyjson22b57fa5DecodeGithubComTechEmpowerFrameworkBenchmarksFrameworksGoChiSrcChi1(in *jlexer.Lexer, out *Message) {
+	isTopLevel := in.IsStart()
+	if in.IsNull() {
+		if isTopLevel {
+			in.Consumed()
+		}
+		in.Skip()
+		return
+	}
+	in.Delim('{')
+	for !in.IsDelim('}') {
+		key := in.UnsafeString()
+		in.WantColon()
+		if in.IsNull() {
+			in.Skip()
+			in.WantComma()
+			continue
+		}
+		switch key {
+		case "message":
+			out.Message = string(in.String())
+		default:
+			in.SkipRecursive()
+		}
+		in.WantComma()
+	}
+	in.Delim('}')
+	if isTopLevel {
+		in.Consumed()
+	}
+}
+func easyjson22b57fa5EncodeGithubComTechEmpowerFrameworkBenchmarksFrameworksGoChiSrcChi1(out *jwriter.Writer, in Message) {
+	out.RawByte('{')
+	first := true
+	_ = first
+	{
+		const prefix string = ",\"message\":"
+		if first {
+			first = false
+			out.RawString(prefix[1:])
+		} else {
+			out.RawString(prefix)
+		}
+		out.String(string(in.Message))
+	}
+	out.RawByte('}')
+}
+
+// MarshalJSON supports json.Marshaler interface
+func (v Message) MarshalJSON() ([]byte, error) {
+	println("marshal")
+	w := jwriter.Writer{}
+	easyjson22b57fa5EncodeGithubComTechEmpowerFrameworkBenchmarksFrameworksGoChiSrcChi1(&w, v)
+	return w.Buffer.BuildBytes(), w.Error
+}
+
+// MarshalEasyJSON supports easyjson.Marshaler interface
+func (v Message) MarshalEasyJSON(w *jwriter.Writer) {
+	easyjson22b57fa5EncodeGithubComTechEmpowerFrameworkBenchmarksFrameworksGoChiSrcChi1(w, v)
+}
+
+// UnmarshalJSON supports json.Unmarshaler interface
+func (v *Message) UnmarshalJSON(data []byte) error {
+	r := jlexer.Lexer{Data: data}
+	easyjson22b57fa5DecodeGithubComTechEmpowerFrameworkBenchmarksFrameworksGoChiSrcChi1(&r, v)
+	return r.Error()
+}
+
+// UnmarshalEasyJSON supports easyjson.Unmarshaler interface
+func (v *Message) UnmarshalEasyJSON(l *jlexer.Lexer) {
+	easyjson22b57fa5DecodeGithubComTechEmpowerFrameworkBenchmarksFrameworksGoChiSrcChi1(l, v)
+}
+func easyjson22b57fa5DecodeGithubComTechEmpowerFrameworkBenchmarksFrameworksGoChiSrcChi2(in *jlexer.Lexer, out *Fortune) {
+	isTopLevel := in.IsStart()
+	if in.IsNull() {
+		if isTopLevel {
+			in.Consumed()
+		}
+		in.Skip()
+		return
+	}
+	in.Delim('{')
+	for !in.IsDelim('}') {
+		key := in.UnsafeString()
+		in.WantColon()
+		if in.IsNull() {
+			in.Skip()
+			in.WantComma()
+			continue
+		}
+		switch key {
+		case "id":
+			out.ID = uint16(in.Uint16())
+		case "message":
+			out.Message = string(in.String())
+		default:
+			in.SkipRecursive()
+		}
+		in.WantComma()
+	}
+	in.Delim('}')
+	if isTopLevel {
+		in.Consumed()
+	}
+}
+func easyjson22b57fa5EncodeGithubComTechEmpowerFrameworkBenchmarksFrameworksGoChiSrcChi2(out *jwriter.Writer, in Fortune) {
+	out.RawByte('{')
+	first := true
+	_ = first
+	{
+		const prefix string = ",\"id\":"
+		if first {
+			first = false
+			out.RawString(prefix[1:])
+		} else {
+			out.RawString(prefix)
+		}
+		out.Uint16(uint16(in.ID))
+	}
+	{
+		const prefix string = ",\"message\":"
+		if first {
+			first = false
+			out.RawString(prefix[1:])
+		} else {
+			out.RawString(prefix)
+		}
+		out.String(string(in.Message))
+	}
+	out.RawByte('}')
+}
+
+// MarshalJSON supports json.Marshaler interface
+func (v Fortune) MarshalJSON() ([]byte, error) {
+	w := jwriter.Writer{}
+	easyjson22b57fa5EncodeGithubComTechEmpowerFrameworkBenchmarksFrameworksGoChiSrcChi2(&w, v)
+	return w.Buffer.BuildBytes(), w.Error
+}
+
+// MarshalEasyJSON supports easyjson.Marshaler interface
+func (v Fortune) MarshalEasyJSON(w *jwriter.Writer) {
+	easyjson22b57fa5EncodeGithubComTechEmpowerFrameworkBenchmarksFrameworksGoChiSrcChi2(w, v)
+}
+
+// UnmarshalJSON supports json.Unmarshaler interface
+func (v *Fortune) UnmarshalJSON(data []byte) error {
+	r := jlexer.Lexer{Data: data}
+	easyjson22b57fa5DecodeGithubComTechEmpowerFrameworkBenchmarksFrameworksGoChiSrcChi2(&r, v)
+	return r.Error()
+}
+
+// UnmarshalEasyJSON supports easyjson.Unmarshaler interface
+func (v *Fortune) UnmarshalEasyJSON(l *jlexer.Lexer) {
+	easyjson22b57fa5DecodeGithubComTechEmpowerFrameworkBenchmarksFrameworksGoChiSrcChi2(l, v)
+}