-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic.go
68 lines (56 loc) · 1.23 KB
/
basic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"crypto/rand"
"fmt"
"github.com/abhi-bit/go-lrucache"
"time"
)
type CacheValue struct {
size string
}
type MyValue []byte
func (cv *CacheValue) Size() int {
return len(cv.size)
}
func (mval MyValue) Size() int {
return cap(mval)
}
func main() {
c := lrucache.NewLRUCache(64 * 1024 * 1024)
testKey := "test-key"
//Benchmarking Get
value := make(MyValue, 1000)
c.Set(testKey, value)
counter := 0
now := time.Now()
for time.Now().Sub(now).Seconds() < 2 {
_, ok := c.Get(testKey)
counter += 1
if !ok {
panic("error")
}
}
fmt.Println("GET:: Requests per sec:", counter/2)
//Benchmarking Set
counter = 0
now = time.Now()
for time.Now().Sub(now).Seconds() < 2 {
//Static value
//c.Set(testKey, value)
//Random values of key size 5 and val size 10
c.Set(randString(5), &CacheValue{randString(10)})
counter += 1
}
fmt.Println("SET:: Requests per sec:", counter/2)
stats := c.StatsJSON()
fmt.Println("Cache Stats:", stats)
}
func randString(n int) string {
const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
var bytes = make([]byte, n)
rand.Read(bytes)
for i, b := range bytes {
bytes[i] = alphanum[b%byte(len(alphanum))]
}
return string(bytes)
}