-
Notifications
You must be signed in to change notification settings - Fork 241
/
httprunner.go
303 lines (285 loc) · 10.4 KB
/
httprunner.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Copyright 2017 Istio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package fhttp
import (
"context"
"fmt"
"net/http"
"os"
"runtime"
"runtime/pprof"
"sort"
"sync"
"fortio.org/fortio/log"
"fortio.org/fortio/periodic"
"fortio.org/fortio/stats"
)
// Most of the code in this file is the library-fication of code originally
// in cmd/fortio/main.go
// HTTPRunnerResults is the aggregated result of an HTTPRunner.
// Also is the internal type used per thread/goroutine.
type HTTPRunnerResults struct {
periodic.RunnerResults
client Fetcher
RetCodes map[int]int64
IPCountMap map[string]int // TODO: Move it to a shared results struct where all runner should have this field
// internal type/data
sizes *stats.Histogram
headerSizes *stats.Histogram
// exported result
HTTPOptions
Sizes *stats.HistogramData
HeaderSizes *stats.HistogramData
Sockets []int64
SocketCount int64
// Connection Time stats
ConnectionStats *stats.HistogramData
// http code to abort the run on (-1 for connection or other socket error)
AbortOn int
aborter *periodic.Aborter
}
// Run tests http request fetching. Main call being run at the target QPS.
// To be set as the Function in RunnerOptions.
func (httpstate *HTTPRunnerResults) Run(ctx context.Context, t periodic.ThreadID) (bool, string) {
log.Debugf("Calling in %d", t)
code, body, headerSize := httpstate.client.Fetch(ctx)
size := len(body)
log.Debugf("Got in %3d hsz %d sz %d - will abort on %d", code, headerSize, size, httpstate.AbortOn)
httpstate.RetCodes[code]++
httpstate.sizes.Record(float64(size))
httpstate.headerSizes.Record(float64(headerSize))
if httpstate.AbortOn == code {
httpstate.aborter.Abort(false)
log.Infof("Aborted run because of code %d - data %s", code, DebugSummary(body, 1024))
}
if code == http.StatusOK {
return true, "200"
}
return false, fmt.Sprint(code)
}
// HTTPRunnerOptions includes the base RunnerOptions plus http specific
// options.
type HTTPRunnerOptions struct {
periodic.RunnerOptions
HTTPOptions // Need to call Init() to initialize
Profiler string // file to save profiles to. defaults to no profiling
AllowInitialErrors bool // whether initial errors don't cause an abort
// Which status code cause an abort of the run (default 0 = don't abort; reminder -1 is returned for socket errors)
AbortOn int
}
// RunHTTPTest runs an http test and returns the aggregated stats.
//
//nolint:funlen, gocognit, gocyclo, maintidx
func RunHTTPTest(o *HTTPRunnerOptions) (*HTTPRunnerResults, error) {
o.RunType = "HTTP"
warmupMode := "parallel"
if o.SequentialWarmup {
warmupMode = "sequential"
}
connReuseMsg := ""
if o.ConnReuseRange != [2]int{0, 0} {
connReuseMsg = fmt.Sprintf(", with connection reuse [%d, %d]", o.ConnReuseRange[0], o.ConnReuseRange[1])
}
log.Infof("Starting http test for %s with %d threads at %.1f qps and %s warmup%s",
o.URL, o.NumThreads, o.QPS, warmupMode, connReuseMsg)
r := periodic.NewPeriodicRunner(&o.RunnerOptions)
if o.HTTPOptions.Resolution <= 0 {
// Set both connect histogram params when Resolution isn't set explicitly on the HTTP options
// (that way you can set the offet to 0 in connect and to something else for the call)
o.HTTPOptions.Resolution = r.Options().Resolution
o.HTTPOptions.Offset = r.Options().Offset
}
defer r.Options().Abort()
numThreads := r.Options().NumThreads // can change during run for c > 2 n
o.HTTPOptions.Init(o.URL)
out := r.Options().Out // Important as the default value is set from nil to stdout inside NewPeriodicRunner
total := HTTPRunnerResults{
HTTPOptions: o.HTTPOptions,
RetCodes: make(map[int]int64),
IPCountMap: make(map[string]int),
sizes: stats.NewHistogram(0, 100),
headerSizes: stats.NewHistogram(0, 5),
AbortOn: o.AbortOn,
aborter: r.Options().Stop,
}
httpstate := make([]HTTPRunnerResults, numThreads)
// First build all the clients sequentially. This ensures we do not have data races when
// constructing requests.
ctx := context.Background()
for i := 0; i < numThreads; i++ {
r.Options().Runners[i] = &httpstate[i]
// Temp mutate the option so each client gets a logging id
o.HTTPOptions.ID = i
// Create a client (and transport) and connect once for each 'thread'
var err error
httpstate[i].client, err = NewClient(&o.HTTPOptions)
// nil check on interface doesn't work
if err != nil {
return nil, err
}
if o.SequentialWarmup && o.Exactly <= 0 {
code, data, headerSize := httpstate[i].client.Fetch(ctx)
if !o.AllowInitialErrors && !codeIsOK(code) {
return nil, fmt.Errorf("error %d for %s: %q", code, o.URL, string(data))
}
if i == 0 && log.LogVerbose() {
log.LogVf("first hit of url %s: status %03d, headers %d, total %d\n%s\n", o.URL, code, headerSize, len(data), data)
}
}
// Setup the stats for each 'thread'
httpstate[i].sizes = total.sizes.Clone()
httpstate[i].headerSizes = total.headerSizes.Clone()
httpstate[i].RetCodes = make(map[int]int64)
httpstate[i].AbortOn = total.AbortOn
httpstate[i].aborter = total.aborter
}
if o.Exactly <= 0 && !o.SequentialWarmup {
warmup := errgroup{}
for i := 0; i < numThreads; i++ {
i := i
warmup.Go(func() error {
code, data, headerSize := httpstate[i].client.Fetch(ctx)
if !o.AllowInitialErrors && !codeIsOK(code) {
return fmt.Errorf("error %d for %s: %q", code, o.URL, string(data))
}
if i == 0 && log.LogVerbose() {
log.LogVf("first hit of url %s: status %03d, headers %d, total %d\n%s\n", o.URL, code, headerSize, len(data), data)
}
return nil
})
}
if err := warmup.Wait(); err != nil {
return nil, err
}
}
// TODO avoid copy pasta with grpcrunner
if o.Profiler != "" {
fc, err := os.Create(o.Profiler + ".cpu")
if err != nil {
log.Critf("Unable to create .cpu profile: %v", err)
return nil, err
}
if err = pprof.StartCPUProfile(fc); err != nil {
log.Critf("Unable to start cpu profile: %v", err)
}
}
total.RunnerResults = r.Run()
if o.Profiler != "" {
pprof.StopCPUProfile()
fm, err := os.Create(o.Profiler + ".mem")
if err != nil {
log.Critf("Unable to create .mem profile: %v", err)
return nil, err
}
runtime.GC() // get up-to-date statistics
if err = pprof.WriteHeapProfile(fm); err != nil {
log.Critf("Unable to write heap profile: %v", err)
}
fm.Close()
_, _ = fmt.Fprintf(out, "Wrote profile data to %s.{cpu|mem}\n", o.Profiler)
}
// Connection stats, aggregated
connectionStats := stats.NewHistogram(o.HTTPOptions.Offset.Seconds(), o.HTTPOptions.Resolution)
// Numthreads may have reduced:
numThreads = total.RunnerResults.NumThreads
// But we also must cleanup all the created clients.
keys := []int{}
fmt.Fprintf(out, "# Socket and IP used for each connection:\n")
for i := 0; i < numThreads; i++ {
// Get the report on the IP address each thread use to send traffic
occurrence, connStats := httpstate[i].client.GetIPAddress()
currentSocketUsed := connStats.Count
httpstate[i].client.Close()
// next 2 in 1 (long) line:
fmt.Fprintf(out, "[%d] %3d socket used, resolved to %s", i, currentSocketUsed, occurrence.AggregateAndToString(total.IPCountMap))
connStats.Counter.Print(out, ", connection timing")
total.SocketCount += currentSocketUsed
total.Sockets = append(total.Sockets, currentSocketUsed)
// Q: is there some copying each time stats[i] is used?
for k := range httpstate[i].RetCodes {
if _, exists := total.RetCodes[k]; !exists {
keys = append(keys, k)
}
total.RetCodes[k] += httpstate[i].RetCodes[k]
}
total.sizes.Transfer(httpstate[i].sizes)
total.headerSizes.Transfer(httpstate[i].headerSizes)
connectionStats.Transfer(connStats)
}
total.ConnectionStats = connectionStats.Export().CalcPercentiles(o.Percentiles)
if log.Log(log.Info) {
total.ConnectionStats.Print(out, "Connection time histogram (s)")
} else if log.Log(log.Warning) {
connectionStats.Counter.Print(out, "Connection time (s)")
}
// Sort the ip address form largest to smallest based on its usage count
ipList := make([]string, 0, len(total.IPCountMap))
for k := range total.IPCountMap {
ipList = append(ipList, k)
}
sort.Slice(ipList, func(i, j int) bool {
return total.IPCountMap[ipList[i]] > total.IPCountMap[ipList[j]]
})
// Cleanup state: (original num thread)
r.Options().ReleaseRunners()
sort.Ints(keys)
totalCount := float64(total.DurationHistogram.Count)
_, _ = fmt.Fprintf(out, "Sockets used: %d (for perfect keepalive, would be %d)\n", total.SocketCount, r.Options().NumThreads)
_, _ = fmt.Fprintf(out, "Uniform: %t, Jitter: %t, Catchup allowed: %t\n", total.Uniform, total.Jitter, !total.NoCatchUp)
_, _ = fmt.Fprintf(out, "IP addresses distribution:\n")
for _, v := range ipList {
_, _ = fmt.Fprintf(out, "%s: %d\n", v, total.IPCountMap[v])
}
for _, k := range keys {
_, _ = fmt.Fprintf(out, "Code %3d : %d (%.1f %%)\n", k, total.RetCodes[k], 100.*float64(total.RetCodes[k])/totalCount)
}
total.HeaderSizes = total.headerSizes.Export()
total.Sizes = total.sizes.Export()
if log.LogVerbose() {
total.HeaderSizes.Print(out, "Response Header Sizes Histogram")
total.Sizes.Print(out, "Response Body/Total Sizes Histogram")
} else if log.Log(log.Warning) {
total.headerSizes.Counter.Print(out, "Response Header Sizes")
total.sizes.Counter.Print(out, "Response Body/Total Sizes")
}
return &total, nil
}
// An errgroup is a collection of goroutines working on subtasks that are part of
// the same overall task.
type errgroup struct {
wg sync.WaitGroup
errOnce sync.Once
err error
}
// Wait blocks until all function calls from the Go method have returned, then
// returns the first non-nil error (if any) from them.
func (g *errgroup) Wait() error {
g.wg.Wait()
return g.err
}
// Go calls the given function in a new goroutine.
//
// The first call to return a non-nil error cancels the group; its error will be
// returned by Wait.
func (g *errgroup) Go(f func() error) {
g.wg.Add(1)
go func() {
defer g.wg.Done()
if err := f(); err != nil {
g.errOnce.Do(func() {
g.err = err
})
}
}()
}