-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
bench.go
119 lines (101 loc) · 2.3 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
package config
import (
"io/ioutil"
"net/http"
"gopkg.in/yaml.v2"
)
var (
// K9sBench the name of the benchmarks config file.
K9sBench = "bench"
)
type (
// Bench tracks K9s styling options.
Bench struct {
Benchmarks *Benchmarks `yaml:"benchmarks"`
}
// Benchmarks tracks K9s benchmarks configuration.
Benchmarks struct {
Defaults Benchmark `yaml:"defaults"`
Services map[string]BenchConfig `yam':"services"`
Containers map[string]BenchConfig `yam':"containers"`
}
// Auth basic auth creds
Auth struct {
User string `yaml:"user"`
Password string `yaml:"password"`
}
// Benchmark represents a generic benchmark.
Benchmark struct {
C int `yaml:"concurrency"`
N int `yaml:"requests"`
}
// HTTP represents an http request.
HTTP struct {
Method string `yaml:"method"`
Host string `yaml:"host"`
Path string `yaml:"path"`
HTTP2 bool `yaml:"http2"`
Body string `yaml:"body"`
Headers http.Header `yaml:"headers"`
}
// BenchConfig represents a service benchmark.
BenchConfig struct {
Name string
C int `yaml:"concurrency"`
N int `yaml:"requests"`
Auth Auth `yaml:"auth"`
HTTP HTTP `yaml:"http"`
}
)
const (
// DefaultC default concurrency.
DefaultC = 1
// DefaultN default number of requests.
DefaultN = 200
// DefaultMethod default http verb.
DefaultMethod = "GET"
)
func newBenchmark() Benchmark {
return Benchmark{
C: DefaultC,
N: DefaultN,
}
}
// Empty checks if the benchmark is set
func (b Benchmark) Empty() bool {
return b.C == 0 && b.N == 0
}
func newBenchmarks() *Benchmarks {
return &Benchmarks{
Defaults: newBenchmark(),
}
}
// NewBench creates a new default config.
func NewBench(path string) (*Bench, error) {
s := &Bench{Benchmarks: newBenchmarks()}
err := s.load(path)
return s, err
}
// Reload update the configuration from disk.
func (s *Bench) Reload(path string) error {
return s.load(path)
}
// Load K9s benchmark configs from file
func (s *Bench) load(path string) error {
f, err := ioutil.ReadFile(path)
if err != nil {
return err
}
return yaml.Unmarshal(f, &s)
}
// DefaultBenchSpec returns a default bench spec.
func DefaultBenchSpec() BenchConfig {
return BenchConfig{
C: DefaultC,
N: DefaultN,
HTTP: HTTP{
Method: DefaultMethod,
Path: "/",
},
}
}