Skip to content

Instantly share code, notes, and snippets.

@erikdubbelboer
Created October 29, 2019 08:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erikdubbelboer/596010f93da7e2b1852d17ddd921fe86 to your computer and use it in GitHub Desktop.
Save erikdubbelboer/596010f93da7e2b1852d17ddd921fe86 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math/rand"
"net/http"
"runtime"
"strings"
"time"
"github.com/dgraph-io/ristretto"
_ "net/http/pprof"
)
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
func key() string {
b := make([]byte, 2)
for i := 0; i < len(b); i++ {
b[i] = charset[rand.Intn(len(charset))]
}
return string(b)
}
type Value struct {
key string
val string
}
func main() {
runtime.MemProfileRate = 1
cache, err := ristretto.NewCache(&ristretto.Config{
NumCounters: 10000000,
MaxCost: 1024 * 1024 * 50,
BufferItems: 64,
Metrics: false,
})
if err != nil {
panic(err)
}
for i := 0; i < 4; i++ {
go func() {
for {
time.Sleep(time.Duration(rand.Intn(10)) * time.Millisecond)
k := key()
v, ok := cache.Get(k)
if ok && v != nil {
vv := v.(*Value)
if vv.key != k {
panic(fmt.Sprintf("%q and %q collide\n", vv.key, k))
}
}
}
}()
}
go func() {
for {
time.Sleep(time.Duration(rand.Intn(10)) * time.Millisecond)
k := key()
val := ""
if rand.Intn(100) < 10 {
val = "test"
} else {
val = strings.Repeat("a", 1024*128)
}
v := &Value{
key: k,
val: val,
}
cache.Set(k, v, int64(len(v.key)+len(v.val)+8*7))
}
}()
go func() {
panic(http.ListenAndServe("localhost:6060", nil))
}()
for {
time.Sleep(time.Second)
var m runtime.MemStats
runtime.GC()
runtime.ReadMemStats(&m)
fmt.Printf("%.2f MiB\n", float64(m.Alloc)/1024/1024)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment