Created
June 3, 2015 14:49
-
-
Save anton-povarov/90740565548bd5c0517c to your computer and use it in GitHub Desktop.
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 ( | |
"encoding/binary" | |
"fmt" | |
"github.com/coocood/freecache" | |
"github.com/davecheney/profile" | |
"log" | |
"math/rand" | |
"runtime" | |
"sync" | |
"sync/atomic" | |
"time" | |
) | |
func main() { | |
runtime.GOMAXPROCS(runtime.NumCPU()) | |
defer profile.Start(profile.CPUProfile).Stop() | |
_ = profile.Start | |
_ = log.Print | |
_ = rand.Uint32 | |
_ = time.NewTicker | |
const cache_size = 1 * 1024 * 1024 | |
cache := freecache.NewCache(cache_size) | |
value_buf := []byte("129nadanh2[0h3r2k3 42,m 3232 3m2 43m2 1931231DSSASADASDADSAaaaaaaaaaaaaajb23ijeb23u4e2h3") | |
const key_len = 16 | |
const n_routines = 1 //10 | |
value_total_len := uint64(0) | |
n_keys := 1 * 1000 * 1000 | |
// rmain := rand.New(rand.NewSource(time.Now().UnixNano())) | |
// r := rand.NewZipf(rmain, 2.0, 71.0, uint64(len(value_buf))) | |
var wg sync.WaitGroup | |
for k := 0; k < n_routines; k++ { | |
wg.Add(1) | |
go func(k int) { | |
for i := 0; i < n_keys; i++ { | |
var key [16]byte | |
// k1 := rand.Intn(1000000000) | |
// k2 := k1 + rand.Intn(1000000000) | |
// binary.LittleEndian.PutUint64(key[:8], uint64(k1)) //uint64(i*k+1000000)) | |
// binary.LittleEndian.PutUint64(key[8:], uint64(k2)) //20394390473*i*k+1000000)) | |
// binary.LittleEndian.PutUint64(key[:8], uint64(i*k+1000000)) | |
// binary.LittleEndian.PutUint64(key[8:], uint64(20394390473*i*k+1000000)) | |
binary.LittleEndian.PutUint64(key[:8], uint64(i)) | |
binary.LittleEndian.PutUint64(key[8:], uint64(i)) | |
// upper_bound := uint(r.Uint64()) //rand.Intn(len(value_buf)) | |
upper_bound := rand.Intn(len(value_buf)) | |
// upper_bound := 16 //len(value_buf) | |
atomic.AddUint64(&value_total_len, uint64(upper_bound)) | |
// log.Printf(">> %d setting key: %v\n", k, key[:]) | |
cache.Set(key[:], value_buf[:upper_bound], 0) | |
// log.Printf("<< %d setting key: %v\n", k, key[:]) | |
} | |
wg.Done() | |
}(k) | |
} | |
wg.Wait() | |
avg_value_len := float64(value_total_len) / float64(n_routines) / float64(n_keys) | |
bytes_per_item := float64(cache_size) / float64(cache.EntryCount()) | |
fmt.Printf("entries: %d, evacuated: %d, overwritten: %d\n", cache.EntryCount(), cache.EvacuateCount(), cache.OverwriteCount()) | |
fmt.Printf("avg_value_len: %v\n", avg_value_len) | |
fmt.Printf("bytes per item: %v - %v - %v = %v\n", bytes_per_item, avg_value_len, key_len, bytes_per_item-avg_value_len-key_len) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment