Last active
December 15, 2015 11:09
-
-
Save int3/5250850 to your computer and use it in GitHub Desktop.
Simple memcached benchmarking script.
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 ( | |
"fmt"; | |
"strings"; | |
"time"; | |
"math/rand"; | |
"strconv"; | |
"github.com/bradfitz/gomemcache/memcache" | |
) | |
func genData(keyCount, valueLength int) []memcache.Item { | |
data := make([]memcache.Item, keyCount) | |
value := strings.Repeat("a", valueLength) | |
for i := 0; i < keyCount; i++ { | |
key := strconv.Itoa(rand.Int()) | |
data[i] = memcache.Item{Key: key, Value: []byte(value)} | |
} | |
return data | |
} | |
func bench(mc *memcache.Client, keyCount, valueLength, goroutineCount int) (misses, total int) { | |
data := genData(keyCount, valueLength) | |
total = keyCount * 10 | |
for _, v := range data { | |
mc.Set(&v) | |
} | |
randSource := rand.NewSource(time.Now().UnixNano()) | |
zipf := rand.NewZipf(rand.New(randSource), 1.1, 1, uint64(keyCount) - 1) | |
if zipf == nil { | |
panic("Could not create Zipf generator") | |
} | |
ch := make(chan int) | |
for i := 0; i < goroutineCount; i++ { | |
go get(mc, data, total / goroutineCount, zipf, ch) | |
} | |
for i := 0; i < goroutineCount; i++ { | |
misses += <-ch | |
} | |
return | |
} | |
func get(mc *memcache.Client, data []memcache.Item, count int, | |
zipf *rand.Zipf, done chan int) { | |
misses := 0 | |
for i := 0; i < count; i++ { | |
idx := zipf.Uint64() | |
_, err := mc.Get(data[idx].Key) | |
if err != nil { | |
mc.Set(&data[idx]) | |
misses++ | |
} | |
} | |
done <- misses | |
} | |
func main() { | |
fmt.Println("Starting benchmark...") | |
mc := memcache.New("127.0.0.1:11211") | |
const KEY_COUNT = 1000 | |
const VALUE_LENGTH = 1000 | |
const GOROUTINE_COUNT = 4 | |
misses, total := bench(mc, KEY_COUNT, VALUE_LENGTH, GOROUTINE_COUNT) | |
fmt.Printf("Number of keys: %d\n", KEY_COUNT) | |
fmt.Printf("Bytes per value: %d\n", VALUE_LENGTH) | |
fmt.Printf("Number of goroutines used: %d\n", GOROUTINE_COUNT) | |
fmt.Printf("Misses / Total Queries: %d / %d\n", misses, total) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment