This repository has been archived by the owner on Aug 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench.go
97 lines (79 loc) · 2.21 KB
/
bench.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"log"
"net/http"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
)
func Plaintext(w http.ResponseWriter, req *http.Request) {
io.WriteString(w, "Hello World!\n")
}
type JSONStruct struct {
Array []int `json:"array"`
Dict map[string]int `json:"dict"`
Int int `json:"int"`
String string `json:"string"`
Double float64 `json:"double"`
Null interface{} `json:"null"`
}
func JSON(w http.ResponseWriter, req *http.Request) {
j := JSONStruct{Array: []int{1, 2, 3},
Dict: map[string]int{"one": 1, "two": 2, "three": 3},
Int: 42,
String: "test",
Double: 3.14,
Null: nil}
b, _ := json.MarshalIndent(j, "", " ")
io.WriteString(w, string(b))
}
type User struct {
ID int `db:"id" json:"id,omitempty"`
Name string `db:"name" json:"name,omitempty"`
Email string `db:"email" json:"email,omitempty"`
}
// typical usage would keep or cache the open DB connection
var db, _ = sqlx.Open("sqlite3", "../database/test.sqlite")
func SQLiteFetch(w http.ResponseWriter, req *http.Request) {
user := User{}
rows, err := db.Queryx("select * from users order by random() limit 1")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
err = rows.StructScan(&user)
if err != nil {
log.Fatal(err)
}
b, _ := json.MarshalIndent(user, "", " ")
io.WriteString(w, string(b))
}
}
// simpler version of SQLiteFetch, but no different performance wise
func SQLiteFetchAlt(w http.ResponseWriter, req *http.Request) {
var user User
err := db.QueryRowx("select * from users order by random() limit 1").StructScan(&user)
if err != nil {
log.Fatal(err)
}
b, _ := json.MarshalIndent(user, "", " ")
io.WriteString(w, string(b))
}
var portNumber int
func main() {
flag.IntVar(&portNumber, "port", 8300, "port number to listen on")
flag.Parse()
http.HandleFunc("/plaintext", Plaintext)
http.HandleFunc("/json", JSON)
http.HandleFunc("/sqlite-fetch", SQLiteFetch)
http.HandleFunc("/sqlite-fetch2", SQLiteFetchAlt)
log.Println("bench running on", fmt.Sprintf("%d", portNumber))
err := http.ListenAndServe(fmt.Sprintf(":%d", portNumber), nil)
if err != nil {
log.Fatal(err)
}
}