-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench.go
188 lines (154 loc) · 4.08 KB
/
bench.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package honu
import (
"context"
"fmt"
"time"
"golang.org/x/sync/errgroup"
pb "github.com/bbengfort/honu/rpc"
"github.com/bbengfort/x/stats"
)
// Benchmark runs clients sending continuous accesses to the remote server.
type Benchmark struct {
workers int
clients []*Client
extra map[string]interface{}
metrics *stats.Benchmark
}
// NewBenchmark creates the data structure and clients.
func NewBenchmark(workers int, prefix string, visibility bool, extra map[string]interface{}) (*Benchmark, error) {
b := new(Benchmark)
b.workers = workers
b.clients = make([]*Client, 0, workers)
b.extra = extra
b.extra["workers"] = workers
b.extra["prefix"] = prefix
b.extra["version"] = PackageVersion
b.extra["timestamp"] = time.Now().Format(time.RFC3339)
for i := 0; i < workers; i++ {
client := new(Client)
client.visibility = visibility
// Generate a key with specified prefix
if len(prefix) > 1 {
client.key = prefix
} else {
client.key = generateKey(prefix)
}
b.clients = append(b.clients, client)
}
return b, nil
}
// Run the benchmark with the specified duration
func (b *Benchmark) Run(addr, outpath string, duration, delay, rate time.Duration) error {
if delay > 0 {
status("delaying benchmark for %s", delay)
time.Sleep(delay)
}
status("starting throughput benchmark with %d clients for %s", b.workers, duration)
group := new(errgroup.Group)
for _, client := range b.clients {
c := client
group.Go(func() error { return c.Run(addr, duration, rate) })
}
if err := group.Wait(); err != nil {
return err
}
if err := b.Results(outpath); err != nil {
return err
}
status("benchmark complete: %s", b)
return nil
}
// Results writes the results to disk.
func (b *Benchmark) Results(path string) error {
latencies := make([]float64, 0)
b.metrics = new(stats.Benchmark)
for _, client := range b.clients {
b.metrics.Append(client.metrics)
}
// Write the results to disk
if path != "" {
data := b.metrics.Serialize()
for key, val := range b.extra {
data[key] = val
}
if len(latencies) > 0 {
data["latencies"] = latencies
}
return appendJSON(path, data)
}
return nil
}
// String returns the metrics results
func (b *Benchmark) String() string {
return fmt.Sprintf(
"%d accesses (%d timeouts): %0.3f accesses/second",
b.metrics.N(), b.metrics.Timeouts(), b.metrics.Throughput(),
)
}
//===========================================================================
// Client Run function
//===========================================================================
// Access sends a put request, measuring latency.
func (c *Client) Access(done chan<- bool, echan chan<- error, rate time.Duration) {
defer func() {
// If we're rate limited, then wait a bit before we return
if rate > 0 {
time.Sleep(rate)
}
done <- true
}()
// Create a string value for the object
val := fmt.Sprintf(
"Put msg %d to object %s",
c.metrics.N()+1, c.key,
)
// Create the Put request
req := &pb.PutRequest{
Key: c.key,
Value: []byte(val),
TrackVisibility: c.visibility,
}
// Send the request
start := time.Now()
rep, err := c.rpc.PutValue(context.Background(), req)
if err != nil {
echan <- err
return
}
// Compute the latency ASAP on successful message
if rep.Success {
delta := time.Since(start)
c.metrics.Update(delta)
return
}
// Otherwise return the error
if rep.Error != "" {
caution("could not Put '%s': %s", c.key, rep.Error)
} else {
caution("could not Put '%s': unknown error occurred", c.key)
}
}
// Run a continuous access client for the specified duration.
func (c *Client) Run(addr string, duration, rate time.Duration) error {
c.metrics = new(stats.Benchmark)
if err := c.Connect(addr); err != nil {
return err
}
timer := time.NewTimer(duration)
echan := make(chan error, 1)
done := make(chan bool, 1)
// Kick off the first access
go c.Access(done, echan, rate)
// Keep accessing until time runs out or something goes down
for {
select {
case <-timer.C:
// Done!
return nil
case err := <-echan:
return err
case <-done:
go c.Access(done, echan, rate)
}
}
}