Created
November 16, 2012 17:13
-
-
Save vys/4089089 to your computer and use it in GitHub Desktop.
simple benchmark to simulate a web request handling in go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
~/code/src/mcguirk# ab -t 60 -c 1 -r http://localhost:8081/ | |
This is ApacheBench, Version 2.3 <$Revision: 655654 $> | |
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ | |
Licensed to The Apache Software Foundation, http://www.apache.org/ | |
Benchmarking localhost (be patient) | |
Completed 5000 requests | |
Completed 10000 requests | |
Completed 15000 requests | |
Completed 20000 requests | |
Finished 20730 requests | |
Server Software: | |
Server Hostname: localhost | |
Server Port: 8081 | |
Document Path: / | |
Document Length: 13 bytes | |
Concurrency Level: 1 | |
Time taken for tests: 60.000 seconds | |
Complete requests: 20730 | |
Failed requests: 0 | |
Write errors: 0 | |
Total transferred: 2674170 bytes | |
HTML transferred: 269490 bytes | |
Requests per second: 345.50 [#/sec] (mean) | |
Time per request: 2.894 [ms] (mean) | |
Time per request: 2.894 [ms] (mean, across all concurrent requests) | |
Transfer rate: 43.52 [Kbytes/sec] received | |
Connection Times (ms) | |
min mean[+/-sd] median max | |
Connect: 0 0 0.0 0 0 | |
Processing: 2 3 2.0 2 14 | |
Waiting: 2 3 2.0 2 14 | |
Total: 2 3 2.0 2 15 | |
Percentage of the requests served within a certain time (ms) | |
50% 2 | |
66% 2 | |
75% 3 | |
80% 3 | |
90% 3 | |
95% 9 | |
98% 12 | |
99% 12 | |
100% 15 (longest request) | |
No timeouts in this run. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
~/code/src/mcguirk# ab -t 60 -c 1000 -r http://localhost:8081/ | |
This is ApacheBench, Version 2.3 <$Revision: 655654 $> | |
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ | |
Licensed to The Apache Software Foundation, http://www.apache.org/ | |
Benchmarking localhost (be patient) | |
Completed 5000 requests | |
Completed 10000 requests | |
Completed 15000 requests | |
Completed 20000 requests | |
Completed 25000 requests | |
Completed 30000 requests | |
Completed 35000 requests | |
Completed 40000 requests | |
Completed 45000 requests | |
Completed 50000 requests | |
Finished 50000 requests | |
Server Software: | |
Server Hostname: localhost | |
Server Port: 8081 | |
Document Path: / | |
Document Length: 13 bytes | |
Concurrency Level: 1000 | |
Time taken for tests: 20.258 seconds | |
Complete requests: 50000 | |
Failed requests: 0 | |
Write errors: 0 | |
Total transferred: 6450000 bytes | |
HTML transferred: 650000 bytes | |
Requests per second: 2468.19 [#/sec] (mean) | |
Time per request: 405.155 [ms] (mean) | |
Time per request: 0.405 [ms] (mean, across all concurrent requests) | |
Transfer rate: 310.93 [Kbytes/sec] received | |
Connection Times (ms) | |
min mean[+/-sd] median max | |
Connect: 0 190 984.2 1 9003 | |
Processing: 6 66 92.6 59 3747 | |
Waiting: 5 65 92.7 57 3746 | |
Total: 7 256 1004.1 61 9879 | |
Percentage of the requests served within a certain time (ms) | |
50% 61 | |
66% 68 | |
75% 71 | |
80% 73 | |
90% 84 | |
95% 297 | |
98% 3076 | |
99% 3693 | |
100% 9879 (longest request) | |
# of timeouts at the end of this run: | |
2012/11/16 17:25:03 load timeout! count: 6770 | |
2012/11/16 17:25:03 save timeout! count: 8606 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"compress/zlib" | |
"encoding/json" | |
"fmt" | |
"log" | |
"math" | |
"net/http" | |
"redis" | |
//"os" | |
//"io" | |
"runtime" | |
) | |
type ClientPool struct { | |
size int | |
clientChan chan redis.AsyncClient | |
} | |
func NewClientPool(size int) *ClientPool { | |
p := &ClientPool{size: size} | |
p.clientChan = make(chan redis.AsyncClient, size) | |
for i := 0; i < size; i++ { | |
client := NewRedisAsyncClient() | |
p.clientChan <- client | |
} | |
return p | |
} | |
func (p *ClientPool) Get() redis.AsyncClient { | |
return <-p.clientChan | |
} | |
func (p *ClientPool) Release(c redis.AsyncClient) { | |
p.clientChan <- c | |
} | |
var pool *ClientPool | |
var loadtimeout uint64 = 0 | |
var savetimeout uint64 = 0 | |
func load(r redis.AsyncClient, k string) (obj interface{}) { | |
f, rerr := r.Get(k) | |
if rerr != nil { | |
panic(rerr) | |
} | |
val, rerr, timeout := f.TryGet(5000000) | |
if rerr != nil { | |
panic(rerr) | |
} | |
if timeout { | |
loadtimeout++ | |
log.Println("load timeout! count: ", loadtimeout) | |
return | |
} | |
zr, err := zlib.NewReader(bytes.NewReader(val)) | |
if err != nil { | |
log.Fatal("Failed to create zlib reader with error: ", err) | |
} | |
defer zr.Close() | |
jd := json.NewDecoder(zr) | |
err = jd.Decode(&obj) | |
if err != nil { | |
log.Fatal("Failed to decode json with error: ", err) | |
} | |
return | |
} | |
func compute() { | |
var k float64 | |
for i := 0; i < 100; i++ { | |
for j := 0; j < 100; j++ { | |
k = math.Sin(float64(i)) * math.Sin(float64(j)) | |
} | |
} | |
_ = k | |
//log.Println("Math Done") | |
} | |
func save(r redis.AsyncClient, key string, obj interface{}) { | |
var b bytes.Buffer | |
z := zlib.NewWriter(&b) | |
defer z.Close() | |
je := json.NewEncoder(z) | |
err := je.Encode(obj) | |
if err != nil { | |
log.Fatal("Failed to json Encode with error: ", err) | |
} | |
z.Flush() | |
f, rerr := r.Set(key, b.Bytes()) | |
if rerr != nil { | |
panic(rerr) | |
} | |
_, rerr, timeout := f.TryGet(5000000) | |
if rerr != nil { | |
panic(rerr) | |
} | |
if timeout { | |
savetimeout++ | |
log.Println("save timeout! count: ", savetimeout) | |
} | |
} | |
type User struct { | |
Id uint64 | |
Name string | |
Xp uint64 | |
Level uint64 | |
Items []Item | |
Friends []uint64 | |
} | |
type Item struct { | |
Id uint64 | |
Type string | |
Data string | |
} | |
func NewUser() *User { | |
it := make([]Item, 20) | |
for i, _ := range it { | |
it[i].Id = 1000 + uint64(i) | |
it[i].Type = "sometype" | |
it[i].Data = "some data blah blah blah" | |
} | |
friends := make([]uint64, 50) | |
for i, _ := range friends { | |
friends[i] = uint64(i) + 10000000 | |
} | |
user := &User{ | |
Id: 1292983, | |
Name: "Vinay", | |
Xp: 100, | |
Level: 200, | |
Items: it, | |
Friends: friends, | |
} | |
return user | |
} | |
var obj interface{} | |
var key string | |
func responseHandler(w http.ResponseWriter, r *http.Request) { | |
client := pool.Get() | |
defer pool.Release(client) | |
obj = load(client, key) | |
//log.Print(obj) | |
compute() | |
//obj = NewUser() | |
save(client, key, obj) | |
fmt.Fprintf(w, "OK! %s", key) | |
} | |
func NewRedisAsyncClient() redis.AsyncClient { | |
spec := redis.DefaultSpec().Db(0).Host("127.0.0.1") | |
client, err := redis.NewAsynchClientWithSpec(spec) | |
if err != nil { | |
panic(err) | |
} | |
return client | |
} | |
func main() { | |
runtime.GOMAXPROCS(16) | |
pool = NewClientPool(100) | |
key = "user_data" | |
http.HandleFunc("/", responseHandler) | |
http.ListenAndServe(":8081", nil) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"compress/zlib" | |
"encoding/json" | |
"fmt" | |
"log" | |
"math" | |
"net/http" | |
"redis" | |
//"os" | |
//"io" | |
"runtime" | |
) | |
var loadtimeout uint64 = 0 | |
var savetimeout uint64 = 0 | |
func load(r redis.AsyncClient, k string) (obj interface{}) { | |
f, rerr := r.Get(k) | |
if rerr != nil { | |
panic(rerr) | |
} | |
val, rerr, timeout := f.TryGet(5000000) | |
if rerr != nil { | |
panic(rerr) | |
} | |
if timeout { | |
loadtimeout++ | |
log.Println("load timeout! count: ", loadtimeout) | |
return | |
} | |
zr, err := zlib.NewReader(bytes.NewReader(val)) | |
if err != nil { | |
log.Fatal("Failed to create zlib reader with error: ", err) | |
} | |
defer zr.Close() | |
jd := json.NewDecoder(zr) | |
err = jd.Decode(&obj) | |
if err != nil { | |
log.Fatal("Failed to decode json with error: ", err) | |
} | |
return | |
} | |
func compute() { | |
var k float64 | |
for i := 0; i < 100; i++ { | |
for j := 0; j < 100; j++ { | |
k = math.Sin(float64(i)) * math.Sin(float64(j)) | |
} | |
} | |
_ = k | |
//log.Println("Math Done") | |
} | |
func save(r redis.AsyncClient, key string, obj interface{}) { | |
var b bytes.Buffer | |
z := zlib.NewWriter(&b) | |
defer z.Close() | |
je := json.NewEncoder(z) | |
err := je.Encode(obj) | |
if err != nil { | |
log.Fatal("Failed to json Encode with error: ", err) | |
} | |
z.Flush() | |
f, rerr := r.Set(key, b.Bytes()) | |
if rerr != nil { | |
panic(rerr) | |
} | |
_, rerr, timeout := f.TryGet(5000000) | |
if rerr != nil { | |
panic(rerr) | |
} | |
if timeout { | |
savetimeout++ | |
log.Println("save timeout! count: ", savetimeout) | |
} | |
} | |
type User struct { | |
Id uint64 | |
Name string | |
Xp uint64 | |
Level uint64 | |
Items []Item | |
Friends []uint64 | |
} | |
type Item struct { | |
Id uint64 | |
Type string | |
Data string | |
} | |
func NewUser() *User { | |
it := make([]Item, 20) | |
for i, _ := range it { | |
it[i].Id = 1000 + uint64(i) | |
it[i].Type = "sometype" | |
it[i].Data = "some data blah blah blah" | |
} | |
friends := make([]uint64, 50) | |
for i, _ := range friends { | |
friends[i] = uint64(i) + 10000000 | |
} | |
user := &User{ | |
Id: 1292983, | |
Name: "Vinay", | |
Xp: 100, | |
Level: 200, | |
Items: it, | |
Friends: friends, | |
} | |
return user | |
} | |
var obj interface{} | |
var key string | |
func responseHandler(w http.ResponseWriter, r *http.Request, client redis.AsyncClient) { | |
obj = load(client, key) | |
//log.Print(obj) | |
compute() | |
//obj = NewUser() | |
save(client, key, obj) | |
fmt.Fprintf(w, "OK! %s", key) | |
} | |
func main() { | |
runtime.GOMAXPROCS(16) | |
key = "user_data" | |
spec := redis.DefaultSpec().Db(0).Host("127.0.0.1") | |
client, err := redis.NewAsynchClientWithSpec(spec) | |
if err != nil { | |
panic(err) | |
} | |
defer client.Quit() | |
handler := func(w http.ResponseWriter, r *http.Request) { | |
responseHandler(w, r, client) | |
} | |
http.HandleFunc("/", handler) | |
http.ListenAndServe(":8081", nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment