-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload.go
216 lines (188 loc) · 4.75 KB
/
load.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
package main
import (
"bytes"
"context"
"crypto/tls"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
"os"
"path"
"sort"
"sync"
"time"
"github.com/fnproject/fn_go/clientv2"
"github.com/fnproject/fn_go/clientv2/apps"
"github.com/fnproject/fn_go/clientv2/fns"
models "github.com/fnproject/fn_go/modelsv2"
openapi "github.com/go-openapi/runtime/client"
"github.com/go-openapi/strfmt"
"gonum.org/v1/gonum/stat"
)
func main() {
start := time.Now()
n := flag.Int("n", 1, "number of invokes")
p := flag.Int("p", 1, "parallel threads")
appName := flag.String("app", "", "app name of function")
fnName := flag.String("fn", "", "name of function")
host := flag.String("host", "http://localhost:8080", "url of fn service")
// TODO(reed): take body from stdin
flag.Parse()
invokes := *n
threads := *p
if threads < 1 {
log.Fatal("p < 1")
}
if invokes < 1 {
log.Fatal("n < 1")
}
if *appName == "" {
log.Fatal("app name must be non-empty string")
}
if *fnName == "" {
log.Fatal("fn name must be non-empty string")
}
// TODO(reed): this is awful, nobody should have to do this
url, err := url.Parse(*host)
if err != nil {
log.Fatal(err)
}
transport := openapi.New(url.Host, path.Join(url.Path, clientv2.DefaultBasePath), []string{url.Scheme})
client := clientv2.New(transport, strfmt.Default)
appsResp, err := client.Apps.ListApps(&apps.ListAppsParams{
Context: context.Background(),
Name: appName,
})
if err != nil {
log.Fatal(err)
}
var app *models.App
if len(appsResp.Payload.Items) > 0 {
app = appsResp.Payload.Items[0]
} else {
log.Fatal("app not found")
}
resp, err := client.Fns.ListFns(&fns.ListFnsParams{
Context: context.Background(),
AppID: &app.ID,
Name: fnName,
})
if err != nil {
log.Fatal(err)
}
var fn *models.Fn
for i := 0; i < len(resp.Payload.Items); i++ {
if resp.Payload.Items[i].Name == *fnName {
fn = resp.Payload.Items[i]
}
}
if fn == nil {
log.Fatal("fn not found")
}
httpClient := &http.Client{Transport: &http.Transport{
Dial: (&net.Dialer{
Timeout: 10 * time.Second,
KeepAlive: 1 * time.Minute,
}).Dial,
TLSClientConfig: &tls.Config{
ClientSessionCache: tls.NewLRUClientSessionCache(8192),
},
TLSHandshakeTimeout: 10 * time.Second,
MaxIdleConnsPerHost: 512,
Proxy: http.ProxyFromEnvironment,
MaxIdleConns: 512,
IdleConnTimeout: 90 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}}
var wg sync.WaitGroup
var plot points
var plock sync.Mutex
for i := 0; i < threads; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < invokes/threads; j++ {
req, err := http.NewRequest("POST", *host+"/invoke/"+fn.ID, nil)
if err != nil {
log.Println(err)
}
// TODO(reed):
req.Header.Set("Content-Type", "text/plain")
start := time.Now()
resp, err := httpClient.Do(req)
if err != nil {
log.Println(err)
}
end := time.Now()
plock.Lock()
plot = append(plot, point{start, end})
plock.Unlock()
if resp.StatusCode != 200 {
log.Printf("bad status code: %v", resp.StatusCode)
io.Copy(os.Stderr, resp.Body)
}
io.Copy(ioutil.Discard, resp.Body)
}
}()
}
wg.Wait()
end := time.Now()
// fmt.Println(end.Format(time.StampMilli))
sort.Sort(plot)
// fmt.Println(plot)
// weight out the last 'threads' number - this shaves off cold start/freeze
var weights []float64
if len(plot) > threads {
plot = plot[:len(plot)-threads]
}
floats := plot.toFloats()
mean := time.Duration(stat.Mean(floats, weights))
_, stdf := stat.MeanStdDev(floats, weights)
std := time.Duration(stdf)
_, varif := stat.MeanVariance(floats, weights)
variance := time.Duration(varif)
_ = variance
median := plot[len(plot)/2].dur()
duration := end.Sub(start)
throughput := float64(invokes) / (float64(duration) / float64(time.Second))
fmt.Println(
"n", invokes,
"p", threads,
"duration:", duration,
"tps:", throughput,
"max:", plot[len(plot)-1].dur(),
"min:", plot[0].dur(),
"mean:", mean,
"median:", median,
"std:", std,
// "variance:", variance, TODO
)
}
type point struct {
start, end time.Time
}
func (p point) dur() time.Duration { return p.end.Sub(p.start) }
type points []point
func (p points) Len() int { return len(p) }
func (p points) Less(i, j int) bool { return p[i].end.Sub(p[i].start) < p[j].end.Sub(p[j].start) }
func (p points) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p points) String() string {
var b bytes.Buffer
for i, pp := range p {
fmt.Fprintln(&b, i, pp.end.Sub(pp.start), pp.start, pp.end)
}
return b.String()
}
// duration floats
func (p points) toFloats() []float64 {
f := make([]float64, len(p))
for i, pp := range p {
f[i] = float64(pp.end.Sub(pp.start))
}
return f
}