Skip to content

Instantly share code, notes, and snippets.

@tadglines
Last active December 16, 2015 23:28
Show Gist options
  • Save tadglines/5513703 to your computer and use it in GitHub Desktop.
Save tadglines/5513703 to your computer and use it in GitHub Desktop.

There are three result files:

results-for-original: Results for the orignal benchmark

results-for-modified: Resutls for a modified benchmark (see hello.go in this gist)

results-for-modified2: Results for a modified benchmark (same as 2 but where MaxConnectionCount=100)

package main
import (
"database/sql"
"encoding/json"
_ "github.com/go-sql-driver/mysql"
"html/template"
"log"
"math/rand"
"net/http"
"runtime"
"sort"
"strconv"
"sync"
)
type MessageStruct struct {
Message string
}
type World struct {
Id uint16 `json:"id"`
RandomNumber uint16 `json:"randomNumber"`
}
type Fortune struct {
Id uint16 `json:"id"`
Message string `json:"message"`
}
const (
ConnectionString = "benchmarkdbuser:benchmarkdbpass@tcp(localhost:3306)/hello_world?charset=utf8"
WorldSelect = "SELECT id, randomNumber FROM World where id = ?"
FortuneSelect = "SELECT id, message FROM Fortune;"
WorldRowCount = 10000
MaxConnectionCount = 256
)
var (
tmpl = template.Must(template.ParseFiles("templates/layout.html", "templates/fortune.html"))
worldStatement *sql.Stmt
fortuneStatement *sql.Stmt
)
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
db, err := sql.Open("mysql", ConnectionString)
if err != nil {
log.Fatalf("Error opening database: %v", err)
}
db.SetMaxIdleConns(MaxConnectionCount)
worldStatement, err = db.Prepare(WorldSelect)
if err != nil {
log.Fatal(err)
}
fortuneStatement, err = db.Prepare(FortuneSelect)
if err != nil {
log.Fatal(err)
}
http.HandleFunc("/db", worldHandler)
http.HandleFunc("/json", jsonHandler)
http.HandleFunc("/fortune", fortuneHandler)
http.ListenAndServe(":8080", nil)
}
func jsonHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/javascript")
j, _ := json.Marshal(&MessageStruct{"Hello, world"})
w.Header().Set("Content-Length", strconv.Itoa(len(j)))
w.Write(j)
}
func worldHandler(w http.ResponseWriter, r *http.Request) {
n := 1
if nStr := r.URL.Query().Get("queries"); len(nStr) != 0 {
n, _ = strconv.Atoi(nStr)
}
ww := make([]World, n)
if n == 1 {
worldStatement.QueryRow(rand.Intn(WorldRowCount)+1).Scan(&ww[0].Id, &ww[0].RandomNumber)
} else {
for i := 0; i < n; i++ {
err := worldStatement.QueryRow(rand.Intn(WorldRowCount)+1).Scan(&ww[i].Id, &ww[i].RandomNumber)
if err != nil {
log.Fatalf("Error scanning world row: %v", err)
}
}
}
j, _ := json.Marshal(ww)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Length", strconv.Itoa(len(j)))
w.Write(j)
}
func fortuneHandler(w http.ResponseWriter, r *http.Request) {
fortunes := make([]*Fortune, 0, 16)
//Execute the query
rows, err := fortuneStatement.Query()
if err != nil {
log.Fatalf("Error preparing statement: %v", err)
}
i := 0
var fortune *Fortune
for rows.Next() { //Fetch rows
fortune = new(Fortune)
if err = rows.Scan(&fortune.Id, &fortune.Message); err != nil {
log.Fatalf("Error scanning fortune row: %v", err)
}
fortunes = append(fortunes, fortune)
i++
}
fortunes = append(fortunes, &Fortune{Message: "Additional fortune added at request time."})
sort.Sort(ByMessage{fortunes})
w.Header().Set("Content-Type", "text/html")
if err := tmpl.Execute(w, map[string]interface{}{"fortunes": fortunes}); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
type Fortunes []*Fortune
func (s Fortunes) Len() int { return len(s) }
func (s Fortunes) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
type ByMessage struct{ Fortunes }
func (s ByMessage) Less(i, j int) bool { return s.Fortunes[i].Message < s.Fortunes[j].Message }
Welcome to Ubuntu 12.04.1 LTS (GNU/Linux 3.2.0-36-virtual x86_64)
* Documentation: https://help.ubuntu.com/
System information disabled due to load higher than 2.0
Get cloud support with Ubuntu Advantage Cloud Guest
http://www.ubuntu.com/business/services/cloud
Use Juju to deploy your cloud instances and workloads.
https://juju.ubuntu.com/#cloud-precise
---------------------------------------------------------
Running Primer go-mod
wrk -d 5 -c 8 -t 8 http://10.0.0.179:8080/db?queries=2
---------------------------------------------------------
Running 5s test @ http://10.0.0.179:8080/db?queries=2
8 threads and 8 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 1.95ms 2.17ms 36.35ms 98.48%
Req/Sec 0.00 0.00 0.00 100.00%
20706 requests in 5.00s, 3.41MB read
Requests/sec: 4141.07
Transfer/sec: 697.88KB
---------------------------------------------------------
Running Warmup go-mod
wrk -d 60 -c 256 -t 2 http://10.0.0.179:8080/db?queries=2
---------------------------------------------------------
Running 1m test @ http://10.0.0.179:8080/db?queries=2
2 threads and 256 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 40.68ms 25.13ms 179.91ms 74.32%
Req/Sec 2.78k 416.90 3.00k 77.64%
363003 requests in 1.00m, 59.74MB read
Requests/sec: 6049.87
Transfer/sec: 1.00MB
---------------------------------------------------------
Queries: 1 for go-mod
wrk -d 60 -c 256 -t 2 http://10.0.0.179:8080/db?queries=1
---------------------------------------------------------
Running 1m test @ http://10.0.0.179:8080/db?queries=1
2 threads and 256 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 26.41ms 22.69ms 198.60ms 77.67%
Req/Sec 5.00k 61.28 5.00k 99.62%
608724 requests in 1.00m, 81.73MB read
Requests/sec: 10145.29
Transfer/sec: 1.36MB
---------------------------------------------------------
Queries: 5 for go-mod
wrk -d 60 -c 256 -t 2 http://10.0.0.179:8080/db?queries=5
---------------------------------------------------------
Running 1m test @ http://10.0.0.179:8080/db?queries=5
2 threads and 256 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 90.64ms 33.14ms 239.52ms 69.22%
Req/Sec 1.00k 49.69 1.00k 99.75%
167808 requests in 1.00m, 43.04MB read
Requests/sec: 2796.45
Transfer/sec: 734.40KB
---------------------------------------------------------
Queries: 10 for go-mod
wrk -d 60 -c 256 -t 2 http://10.0.0.179:8080/db?queries=10
---------------------------------------------------------
Running 1m test @ http://10.0.0.179:8080/db?queries=10
2 threads and 256 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 172.87ms 46.53ms 372.04ms 72.32%
Req/Sec 0.00 0.00 0.00 100.00%
89392 requests in 1.00m, 36.47MB read
Requests/sec: 1489.78
Transfer/sec: 622.44KB
---------------------------------------------------------
Queries: 15 for go-mod
wrk -d 60 -c 256 -t 2 http://10.0.0.179:8080/db?queries=15
---------------------------------------------------------
Running 1m test @ http://10.0.0.179:8080/db?queries=15
2 threads and 256 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 251.66ms 59.58ms 574.09ms 77.18%
Req/Sec 0.00 0.00 0.00 100.00%
60706 requests in 1.00m, 33.97MB read
Requests/sec: 1011.73
Transfer/sec: 579.74KB
---------------------------------------------------------
Queries: 20 for go-mod
wrk -d 60 -c 256 -t 2 http://10.0.0.179:8080/db?queries=20
---------------------------------------------------------
Running 1m test @ http://10.0.0.179:8080/db?queries=20
2 threads and 256 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 326.44ms 63.06ms 596.43ms 73.53%
Req/Sec 0.00 0.00 0.00 100.00%
46524 requests in 1.00m, 33.08MB read
Requests/sec: 775.39
Transfer/sec: 564.63KB
Welcome to Ubuntu 12.04.1 LTS (GNU/Linux 3.2.0-36-virtual x86_64)
* Documentation: https://help.ubuntu.com/
System information disabled due to load higher than 2.0
Get cloud support with Ubuntu Advantage Cloud Guest
http://www.ubuntu.com/business/services/cloud
Use Juju to deploy your cloud instances and workloads.
https://juju.ubuntu.com/#cloud-precise
---------------------------------------------------------
Running Primer go-mod
wrk -d 5 -c 8 -t 8 http://10.0.0.179:8080/db?queries=2
---------------------------------------------------------
Running 5s test @ http://10.0.0.179:8080/db?queries=2
8 threads and 8 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 1.90ms 375.00us 4.80ms 93.96%
Req/Sec 0.00 0.00 0.00 100.00%
21022 requests in 5.00s, 3.46MB read
Requests/sec: 4203.83
Transfer/sec: 708.47KB
---------------------------------------------------------
Running Warmup go-mod
wrk -d 60 -c 256 -t 2 http://10.0.0.179:8080/db?queries=2
---------------------------------------------------------
Running 1m test @ http://10.0.0.179:8080/db?queries=2
2 threads and 256 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 54.60ms 106.05ms 1.10s 93.38%
Req/Sec 2.04k 205.55 3.00k 95.63%
338417 requests in 1.00m, 55.69MB read
Requests/sec: 5640.23
Transfer/sec: 0.93MB
---------------------------------------------------------
Queries: 1 for go-mod
wrk -d 60 -c 256 -t 2 http://10.0.0.179:8080/db?queries=1
---------------------------------------------------------
Running 1m test @ http://10.0.0.179:8080/db?queries=1
2 threads and 256 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 27.88ms 71.83ms 888.68ms 96.53%
Req/Sec 3.99k 85.96 4.00k 99.26%
577154 requests in 1.00m, 77.49MB read
Requests/sec: 9619.12
Transfer/sec: 1.29MB
---------------------------------------------------------
Queries: 5 for go-mod
wrk -d 60 -c 256 -t 2 http://10.0.0.179:8080/db?queries=5
---------------------------------------------------------
Running 1m test @ http://10.0.0.179:8080/db?queries=5
2 threads and 256 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 105.30ms 190.11ms 1.55s 93.25%
Req/Sec 1.00k 49.97 1.00k 99.75%
164056 requests in 1.00m, 42.07MB read
Socket errors: connect 0, read 0, write 0, timeout 2
Requests/sec: 2734.19
Transfer/sec: 718.05KB
---------------------------------------------------------
Queries: 10 for go-mod
wrk -d 60 -c 256 -t 2 http://10.0.0.179:8080/db?queries=10
---------------------------------------------------------
Running 1m test @ http://10.0.0.179:8080/db?queries=10
2 threads and 256 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 189.70ms 323.39ms 3.05s 91.12%
Req/Sec 0.00 0.00 0.00 100.00%
87907 requests in 1.00m, 35.87MB read
Socket errors: connect 0, read 0, write 0, timeout 46
Requests/sec: 1465.03
Transfer/sec: 612.11KB
---------------------------------------------------------
Queries: 15 for go-mod
wrk -d 60 -c 256 -t 2 http://10.0.0.179:8080/db?queries=15
---------------------------------------------------------
Running 1m test @ http://10.0.0.179:8080/db?queries=15
2 threads and 256 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 263.43ms 331.00ms 2.46s 87.77%
Req/Sec 0.00 0.00 0.00 100.00%
59968 requests in 1.00m, 33.56MB read
Socket errors: connect 0, read 0, write 0, timeout 45
Requests/sec: 999.43
Transfer/sec: 572.69KB
---------------------------------------------------------
Queries: 20 for go-mod
wrk -d 60 -c 256 -t 2 http://10.0.0.179:8080/db?queries=20
---------------------------------------------------------
Running 1m test @ http://10.0.0.179:8080/db?queries=20
2 threads and 256 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 397.53ms 452.10ms 5.07s 86.16%
Req/Sec 0.00 0.00 0.00 100.00%
43550 requests in 1.00m, 30.97MB read
Socket errors: connect 0, read 0, write 0, timeout 79
Requests/sec: 725.82
Transfer/sec: 528.54KB
Welcome to Ubuntu 12.04.1 LTS (GNU/Linux 3.2.0-36-virtual x86_64)
* Documentation: https://help.ubuntu.com/
System information disabled due to load higher than 2.0
Get cloud support with Ubuntu Advantage Cloud Guest
http://www.ubuntu.com/business/services/cloud
Use Juju to deploy your cloud instances and workloads.
https://juju.ubuntu.com/#cloud-precise
---------------------------------------------------------
Running Primer go
wrk -d 5 -c 8 -t 8 http://10.0.0.179:8080/db?queries=2
---------------------------------------------------------
Running 5s test @ http://10.0.0.179:8080/db?queries=2
8 threads and 8 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 1.59ms 447.04us 5.88ms 88.64%
Req/Sec 0.00 0.00 0.00 100.00%
24280 requests in 5.00s, 4.00MB read
Requests/sec: 4855.95
Transfer/sec: 818.32KB
---------------------------------------------------------
Running Warmup go
wrk -d 60 -c 256 -t 2 http://10.0.0.179:8080/db?queries=2
---------------------------------------------------------
Running 1m test @ http://10.0.0.179:8080/db?queries=2
2 threads and 256 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 62.47ms 165.74ms 1.63s 94.35%
Req/Sec 2.00k 60.63 2.00k 99.63%
333015 requests in 1.00m, 54.81MB read
Requests/sec: 5550.21
Transfer/sec: 0.91MB
---------------------------------------------------------
Queries: 1 for go
wrk -d 60 -c 256 -t 2 http://10.0.0.179:8080/db?queries=1
---------------------------------------------------------
Running 1m test @ http://10.0.0.179:8080/db?queries=1
2 threads and 256 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 31.07ms 75.14ms 694.98ms 95.74%
Req/Sec 4.00k 50.00 4.00k 99.75%
571119 requests in 1.00m, 76.68MB read
Requests/sec: 9518.14
Transfer/sec: 1.28MB
---------------------------------------------------------
Queries: 5 for go
wrk -d 60 -c 256 -t 2 http://10.0.0.179:8080/db?queries=5
---------------------------------------------------------
Running 1m test @ http://10.0.0.179:8080/db?queries=5
2 threads and 256 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 111.78ms 390.59ms 2.71s 95.38%
Req/Sec 0.99k 93.13 1.00k 99.13%
156471 requests in 1.00m, 40.13MB read
Socket errors: connect 0, read 0, write 0, timeout 589
Requests/sec: 2607.76
Transfer/sec: 684.84KB
---------------------------------------------------------
Queries: 10 for go
wrk -d 60 -c 256 -t 2 http://10.0.0.179:8080/db?queries=10
---------------------------------------------------------
Running 1m test @ http://10.0.0.179:8080/db?queries=10
2 threads and 256 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 266.93ms 847.15ms 6.05s 92.80%
Req/Sec 0.00 0.00 0.00 100.00%
81459 requests in 1.00m, 33.24MB read
Socket errors: connect 0, read 0, write 0, timeout 2533
Requests/sec: 1357.46
Transfer/sec: 567.17KB
---------------------------------------------------------
Queries: 15 for go
wrk -d 60 -c 256 -t 2 http://10.0.0.179:8080/db?queries=15
---------------------------------------------------------
Running 1m test @ http://10.0.0.179:8080/db?queries=15
2 threads and 256 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 441.95ms 1.47s 17.70s 90.25%
Req/Sec 0.00 0.00 0.00 100.00%
54004 requests in 1.00m, 30.22MB read
Socket errors: connect 0, read 0, write 0, timeout 3085
Requests/sec: 900.00
Transfer/sec: 515.71KB
---------------------------------------------------------
Queries: 20 for go
wrk -d 60 -c 256 -t 2 http://10.0.0.179:8080/db?queries=20
---------------------------------------------------------
Running 1m test @ http://10.0.0.179:8080/db?queries=20
2 threads and 256 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 428.33ms 1.62s 33.07s 91.99%
Req/Sec 0.00 0.00 0.00 100.00%
38250 requests in 1.00m, 27.20MB read
Socket errors: connect 0, read 0, write 0, timeout 3147
Requests/sec: 637.45
Transfer/sec: 464.19KB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment