-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
155 lines (131 loc) · 3.31 KB
/
main.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Unless explicitly stated otherwise all files in this repository are licensed
// under the MIT License.
// This product includes software developed at Guance Cloud (https://www.guance.com/).
// Copyright 2021-present Guance, Inc.
//nolint:gosec
package main
import (
"flag"
"log"
"math/rand"
"sync"
"time"
dc "github.com/GuanceCloud/cliutils/diskcache"
"github.com/GuanceCloud/cliutils/metrics"
)
var (
path, http string
capacity int64
disableGoMetrics bool
putLatency,
getLatency,
runtime,
workers,
sampleMax,
sampleMin int
cache *dc.DiskCache
wg sync.WaitGroup
dataBuf []byte
tick *time.Ticker
)
const (
GB = 1024 * 1024 * 1024
)
//nolint:gochecknoinits
func init() {
flag.StringVar(&path, "path", "./diskcache", "cache path")
flag.Int64Var(&capacity, "cap", 32, "cache capacity(GB)")
flag.IntVar(&workers, "workers", 1, "concurrent Put/Get workers")
flag.IntVar(&sampleMax, "smax", 32768, "maximum sample size(KB)")
flag.IntVar(&sampleMin, "smin", 4, "minimal sample size(KB)")
flag.IntVar(&runtime, "runtime", 5, "run time(minute) for the test")
flag.BoolVar(&disableGoMetrics, "disable-gom", false, "disable golang metrics")
flag.IntVar(&putLatency, "put-lat", 100, "put latency(ms) randome range(from 0)")
flag.IntVar(&getLatency, "get-lat", 100, "get latency(ms) randome range(from 0)")
flag.StringVar(&http, "http", "localhost:9090", "bind HTTP to serve /metrics")
}
func main() {
flag.Parse()
var err error
dataBuf = make([]byte, 32*1024*1024) // 32MB data buffer
tick = time.NewTicker(time.Duration(runtime) * time.Minute)
cache, err = dc.Open(dc.WithPath(path), dc.WithCapacity(capacity*GB))
if err != nil {
log.Panic(err)
}
run()
}
// get random bytes from dataBuf.
func getSamples() []byte {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
n := (sampleMin + (r.Int() % sampleMax)) * 1024 // in KB
if n >= len(dataBuf) {
n = len(dataBuf)
}
start := r.Int() % len(dataBuf)
if start+n > len(dataBuf) {
return dataBuf[len(dataBuf)-n:] // return last n bytes
} else {
return dataBuf[start : start+n]
}
}
func put(n int) {
wg.Add(workers)
for i := 0; i < workers; i++ {
go func() {
defer wg.Done()
for {
select {
case <-tick.C:
return
default:
r := rand.New(rand.NewSource(time.Now().UnixNano()))
time.Sleep(time.Millisecond * time.Duration(r.Int()%getLatency))
cache.Put(getSamples()) //nolint:errcheck
}
}
}()
}
}
func get(n int) {
wg.Add(workers)
for i := 0; i < workers; i++ {
go func() {
defer wg.Done()
for {
select {
case <-tick.C:
for {
// read them all without weekend.
if err := cache.Get(func(_ []byte) error {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
time.Sleep(time.Millisecond * time.Duration(r.Int()%getLatency))
return nil
}); err != nil {
return
}
}
default:
cache.Get(func(_ []byte) error { // nolint: errcheck
r := rand.New(rand.NewSource(time.Now().UnixNano()))
time.Sleep(time.Millisecond * time.Duration(r.Int()%getLatency))
return nil
})
}
}
}()
}
}
func run() {
put(workers)
get(workers)
ms := metrics.NewMetricServer()
ms.DisableGoMetrics = disableGoMetrics
ms.Listen = http
go func() {
if err := ms.Start(); err != nil {
log.Panic(err)
}
}()
wg.Wait()
}