This repository has been archived by the owner on Feb 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
258 lines (235 loc) · 6.84 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// An alternative version of the Go observability benchmark found at
// github.com/felixge/go-observability-bench.
//
// The main difference is the output format. This version outputs CSV records,
// intended to be concatenated together and fed straight into a table-based
// analysis tool like R (read_csv), Pandas (pd.read_csv), or SQLite3 (.mode csv;
// .import <filename>). There you can do any filtering, transformation, grouping,
// summarizing, etc.
//
// I also chose to report the benchmark times over the whole benchmark, rather
// than per-iteration. This is how the Go benchmark tool does it. Though I'm not
// sure which way is better TBH.
//
// It also is intended to take everything from the command line, with maybe a
// wrapper script to orchestrate multiple runs.
package main
import (
"flag"
"fmt"
"os"
"runtime"
"runtime/pprof"
"sort"
"strconv"
"strings"
"syscall"
"time"
"github.com/felixge/go-observability-bench/workload"
"github.com/pkg/profile"
)
var usingCgotraceback bool
// ByteCounter is an io.Writer which records how many bytes have been written.
type ByteCounter int64
func (b *ByteCounter) Write(p []byte) (int, error) {
*b += ByteCounter(len(p))
return len(p), nil
}
func main() {
duration := flag.String("duration", "1s", "length of benchmark (in Go time.Duration format")
benchmark := flag.String("benchmark", "http", "name of benchmark")
repeat := flag.Int("repeat", 1, "how many times to repeat benchmark")
profiles := flag.String("profiles", "none", "semicolon-separated list of profiles")
header := flag.Bool("header", true, "print CSV header")
concurrency := flag.Int("concurrency", 1, "how many concurrent goroutines to run benchmark")
ballast := flag.Int("ballast", 0, "> 1 to allocate some extra memory (for GC testing)")
mem := flag.Bool("mem", false, "record memory profile")
memstat := flag.Bool("memstat", false, "report memory stats at program exit")
flag.Parse()
ballastch := make(chan []byte)
if *ballast > 0 {
// A "ballast" is basically a trick to keep the in-use heap
// memory higher. GC is triggered by heap growth, and the
// bigger the heap, the bigger the next target heap size for GC
// will be. So with a ballast making the heap large, GC will
// trigger less frequently provided the rest of our application
// doesn't allocate that much.
//
// I use a channel here, and receive from it at the end of the
// program, so that the memory actually sticks around.
go func() {
memballast := make([]byte, *ballast)
ballastch <- memballast
}()
}
if len(os.Getenv("DISABLE_MEM_PROFILE")) > 0 {
runtime.MemProfileRate = 0
}
if *mem {
defer profile.Start(profile.MemProfile, profile.ProfilePath(".")).Stop()
}
var w workload.Workload
switch *benchmark {
case "json":
w = &workload.JSON{File: "data/small.json"}
case "http":
w = &workload.HTTP{}
case "cgo":
w = CGo{}
default:
fmt.Fprintf(os.Stderr, "unrecognized test %s\n", *benchmark)
return
}
d, err := time.ParseDuration(*duration)
if err != nil {
fmt.Fprintf(os.Stderr, "bad time format: %s\n", err)
return
}
enabledProfs := strings.Split(*profiles, ";")
sort.Strings(enabledProfs)
*profiles = strings.Join(enabledProfs, ";")
if *header {
fmt.Println("name,iters,ns,cpu-ns,profiles,profile-bytes,concurrency,using-cgotraceback")
}
for i := 0; i < *repeat; i++ {
bc := new(ByteCounter)
for _, prof := range enabledProfs {
switch prof {
case "cpu":
pprof.StartCPUProfile(bc)
}
}
var (
res Result
err error
)
res, err = ConcurrentRunner(w, *benchmark, d, *concurrency)
if err != nil {
fmt.Fprintf(os.Stderr, "discarding failed test: %s\n", err)
continue
}
res.Profiles = *profiles
res.Concurrency = *concurrency
for _, prof := range enabledProfs {
switch prof {
case "cpu":
pprof.StopCPUProfile()
}
}
res.ProfileBytes = int64(*bc)
fmt.Printf("%s\n", strings.Join(res.ToRecord(), ","))
}
if *memstat {
var mstat runtime.MemStats
runtime.ReadMemStats(&mstat)
fmt.Fprintf(os.Stderr, "pause-ns: %+v, total-alloc: %v, num-gc: %v\n", mstat.PauseTotalNs, mstat.TotalAlloc, mstat.NumGC)
var rusage syscall.Rusage
syscall.Getrusage(0, &rusage)
fmt.Fprintf(os.Stderr, "max-rss: %d\n", rusage.Maxrss)
}
if *ballast > 0 {
s := <-ballastch
fmt.Fprintln(os.Stderr, "ballast size:", len(s))
}
}
// Result is the information collected after running a benchmark.
// Modeled after testing.BenchmarkResult
type Result struct {
// Name is the particular benchmark, e.g. http or json
Name string
// N is the number of iterations
N int
// T is the total elapsed time
T time.Duration
// CPUTime is the total CPU time, including user and system time
CPUTime time.Duration
// Profiles is a semicolon-separated list of profiles which were enabled
Profiles string
// ProfileBytes is how much profiling data was recorded.
ProfileBytes int64
// Concurrency is how many goroutines were running the benchmark
Concurrency int
}
// ToRecord encodes the Result as a CSV record
func (r Result) ToRecord() []string {
return []string{
r.Name,
strconv.FormatInt(int64(r.N), 10),
strconv.FormatInt(r.T.Nanoseconds(), 10),
strconv.FormatInt(r.CPUTime.Nanoseconds(), 10),
r.Profiles,
strconv.FormatInt(r.ProfileBytes, 10),
strconv.FormatInt(int64(r.Concurrency), 10),
strconv.FormatBool(usingCgotraceback),
}
}
// ConcurrentRunner repeatedly calls w.Run() until the given duration has
// elapsed. Returns a result with the timing and iteration information
// populated
func ConcurrentRunner(w workload.Workload, name string, duration time.Duration, concurrency int) (res Result, err error) {
done := make(chan struct{})
time.AfterFunc(duration, func() { close(done) })
type runInfo struct {
iters int
elapsed time.Duration
err error
}
ch := make(chan runInfo, concurrency)
run := func() {
var n int
var elapsed time.Duration
var info runInfo
err = w.Setup()
if err != nil {
info.err = err
ch <- info
return
}
for {
start := time.Now()
err = w.Run()
if err != nil {
info.err = err
ch <- info
return
}
elapsed += time.Since(start)
n++
select {
case <-done:
info.iters = n
info.elapsed = elapsed
ch <- info
return
default:
}
}
}
before := CPURusage()
for i := 0; i < concurrency; i++ {
go run()
}
res = Result{
Name: name,
}
for i := 0; i < concurrency; i++ {
info := <-ch
res.N += info.iters
res.T += info.elapsed
if info.err != nil {
fmt.Fprintln(os.Stderr, "bench failed early with error %s\n", info.err)
}
}
res.CPUTime = CPURusage() - before
return res, nil
}
// CPURusage reports the total elapsed CPU time scheduled to the process,
// including user and system time.
func CPURusage() time.Duration {
var r syscall.Rusage
syscall.Getrusage(0, &r)
return tvtotd(r.Stime) + tvtotd(r.Utime)
}
func tvtotd(t syscall.Timeval) time.Duration {
return time.Duration(t.Nano())
}