/
config.go
65 lines (55 loc) · 1.97 KB
/
config.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
package bench
import (
"fmt"
"math/rand"
"strings"
humanize "github.com/dustin/go-humanize"
flags "github.com/jessevdk/go-flags"
)
type Config struct {
Messages int `long:"msgs" description:"Number of messages to publish" default:"1000000"`
MessageSize int `long:"msg-size" description:"Size of the test messages" default:"128"`
Topic string `long:"topic" description:"Topic name to use for benchmark (if empty generates random topic)" default:""`
Publishers int `long:"pubs" description:"Number of concurrent publishers" default:"1"`
Subscribers int `long:"subs" description:"Number of concurrent subscribers" default:"1"`
Resumers int `long:"resumers" description:"Number of concurrent resumers (subscribers that start subscribing after all messages published)" default:"1"`
Runs int `long:"runs" description:"Number of times to run the benchmark" default:"1"`
Addr string `long:"addr" description:"Address of the Figg server" default:"127.0.0.1:8119"`
Verbose bool `short:"v" long:"verbose" description:"Add verbose logging to SDK clients"`
}
func (c Config) String() string {
return fmt.Sprintf(
"msgs=%s msg-size=%s topic=%s addr=%s publishers=%d subscribers=%d resumers=%d runs=%d",
humanize.Comma(int64(c.Messages)),
strings.ReplaceAll(humanize.Bytes(uint64(c.MessageSize)), " ", ""),
c.Topic,
c.Addr,
c.Publishers,
c.Subscribers,
c.Resumers,
c.Runs,
)
}
func ParseConfig() (*Config, error) {
config := &Config{}
_, err := flags.Parse(config)
if err != nil {
return nil, err
}
if config.Messages < 0 {
return nil, fmt.Errorf("msgs cannot be negative")
}
if config.MessageSize < 0 {
return nil, fmt.Errorf("msg-size cannot be negative")
}
if config.Publishers <= 0 {
return nil, fmt.Errorf("publishers must be positive")
}
if config.Runs <= 0 {
return nil, fmt.Errorf("runs must be positive")
}
if config.Topic == "" {
config.Topic = fmt.Sprintf("bench-%d", rand.Int()%0xffff)
}
return config, nil
}