-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbenchmark.go
63 lines (52 loc) · 1.36 KB
/
benchmark.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
package main
import (
"context"
"fmt"
"math/rand"
"sync"
"sync/atomic"
"time"
"github.com/hazelcast/hazelcast-go-client/core"
"github.com/mdogan/hdrhistogram"
)
type result struct {
*hdrhistogram.Histogram
duration time.Duration
}
var totalOperations uint64
func benchmark(m core.Map, wg *sync.WaitGroup, ctx context.Context, ch chan *result) {
hist := hdrhistogram.New(1, int64(time.Second), 3)
value := make([]byte, valueSize)
rnd := rand.New(rand.NewSource(rand.Int63()))
var err error
result := result{Histogram: hist}
defer func() {
ch <- &result
wg.Done()
}()
for i := 0; i < requests / clients; i++ {
select {
case <-ctx.Done():
return
default:
op := rnd.Intn(setRatio + getRatio)
k := rnd.Intn(keyCount)
start := time.Now()
if op < getRatio {
_, err = m.Get(k)
} else {
err = m.Set(k, value)
}
elapsed := time.Now().Sub(start)
_ = hist.RecordValue(elapsed.Nanoseconds())
result.duration += elapsed
if i % 100 == 0 {
atomic.AddUint64(&totalOperations, 100)
}
if err != nil {
fmt.Println(err)
return
}
}
}
}