Skip to content

Instantly share code, notes, and snippets.

@jmcguirk
Created November 16, 2012 17:38
Show Gist options
  • Save jmcguirk/4089289 to your computer and use it in GitHub Desktop.
Save jmcguirk/4089289 to your computer and use it in GitHub Desktop.
VS Benchmark revised
Concurrency Level: 100
Time taken for tests: 6.262829 seconds
Complete requests: 10000
/***********************************************************
Failed requests: 5028
(Connect: 0, Length: 5028, Exceptions: 0)
***********************************************************/
Write errors: 0
Total transferred: 1475286 bytes
HTML transferred: 315054 bytes
Requests per second: 1596.72 [#/sec] (mean)
Time per request: 62.628 [ms] (mean)
Time per request: 0.626 [ms] (mean, across all concurrent requests)
Transfer rate: 229.93 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 1 2 1.1 2 17
Processing: 10 59 23.6 56 261
Waiting: 10 57 23.4 54 253
Total: 12 61 23.6 58 263
Percentage of the requests served within a certain time (ms)
50% 58
66% 65
75% 70
80% 73
90% 83
95% 91
98% 107
99% 215
100% 263
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, w http.ResponseWriter) (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)
fmt.Fprintf(w, "Save failed for %s", key);
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{}, w http.ResponseWriter) {
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)
fmt.Fprintf(w, "Save failed for %s", key);
}
}
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, w)
//log.Print(obj)
compute()
//obj = NewUser()
save(client, key, obj, w)
fmt.Fprintf(w, "OK! %s", key)
}
func main() {
runtime.GOMAXPROCS(16)
key = "user_data_1"
spec := redis.DefaultSpec().Db(0).Host("10.174.178.235")
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(":80", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment