-
Notifications
You must be signed in to change notification settings - Fork 6
/
benchmark_test.go
367 lines (324 loc) · 15.5 KB
/
benchmark_test.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package benchmark_test
import (
"os"
"testing"
"time"
"v.io/v23"
"v.io/v23/context"
"v.io/v23/options"
"v.io/v23/rpc"
"v.io/v23/security"
"v.io/x/lib/ibe"
lsecurity "v.io/x/ref/lib/security"
"v.io/x/ref/lib/security/bcrypter"
_ "v.io/x/ref/runtime/factories/roaming"
bm "v.io/x/ref/runtime/internal/rpc/benchmark"
"v.io/x/ref/runtime/internal/rpc/benchmark/internal"
"v.io/x/ref/services/xproxy/xproxy"
"v.io/x/ref/test"
"v.io/x/ref/test/benchmark"
"v.io/x/ref/test/testutil"
)
var (
serverAddr, expServerAddr, tpcServerAddr, proxiedServerAddr string
ctx, expCtx, tpcCtx *context.T
serverCrypter, clientCrypter *bcrypter.Crypter
)
// Benchmarks for non-streaming RPC.
func runEcho(b *testing.B, payloadSize int) {
internal.CallEcho(b, ctx, serverAddr, b.N, payloadSize, benchmark.AddStats(b, 16))
}
func Benchmark____1B(b *testing.B) { runEcho(b, 1) }
func Benchmark___10B(b *testing.B) { runEcho(b, 10) }
func Benchmark__100B(b *testing.B) { runEcho(b, 100) }
func Benchmark___1KB(b *testing.B) { runEcho(b, 1000) }
func Benchmark__10KB(b *testing.B) { runEcho(b, 10000) }
func Benchmark_100KB(b *testing.B) { runEcho(b, 100000) }
// Benchmark for non-streaming RPC with a Client ExpiryCaveat.
func runEchoClientExpiry(b *testing.B, payloadSize int) {
internal.CallEcho(b, expCtx, serverAddr, b.N, payloadSize, benchmark.AddStats(b, 16))
}
func Benchmark____1B_ClientExpiryCav(b *testing.B) { runEchoClientExpiry(b, 1) }
func Benchmark___10B_ClientExpiryCav(b *testing.B) { runEchoClientExpiry(b, 10) }
func Benchmark__100B_ClientExpiryCav(b *testing.B) { runEchoClientExpiry(b, 100) }
func Benchmark___1KB_ClientExpiryCav(b *testing.B) { runEchoClientExpiry(b, 1000) }
func Benchmark__10KB_ClientExpiryCav(b *testing.B) { runEchoClientExpiry(b, 10000) }
func Benchmark_100KB_ClientExpiryCav(b *testing.B) { runEchoClientExpiry(b, 100000) }
func runEchoServerExpiry(b *testing.B, payloadSize int) {
internal.CallEcho(b, ctx, expServerAddr, b.N, payloadSize, benchmark.AddStats(b, 16))
}
func Benchmark____1B_ServerExpiryCav(b *testing.B) { runEchoServerExpiry(b, 1) }
func Benchmark___10B_ServerExpiryCav(b *testing.B) { runEchoServerExpiry(b, 10) }
func Benchmark__100B_ServerExpiryCav(b *testing.B) { runEchoServerExpiry(b, 100) }
func Benchmark___1KB_ServerExpiryCav(b *testing.B) { runEchoServerExpiry(b, 1000) }
func Benchmark__10KB_ServerExpiryCav(b *testing.B) { runEchoServerExpiry(b, 10000) }
func Benchmark_100KB_ServerExpiryCav(b *testing.B) { runEchoServerExpiry(b, 100000) }
// Benchmark for non-streaming RPC with a Client ThirdPartyCaveat.
func runEchoClientTPC(b *testing.B, payloadSize int) {
internal.CallEcho(b, tpcCtx, serverAddr, b.N, payloadSize, benchmark.AddStats(b, 16))
}
func Benchmark____1B_ClientTPCav(b *testing.B) { runEchoClientTPC(b, 1) }
func Benchmark___10B_ClientTPCav(b *testing.B) { runEchoClientTPC(b, 10) }
func Benchmark__100B_ClientTPCav(b *testing.B) { runEchoClientTPC(b, 100) }
func Benchmark___1KB_ClientTPCav(b *testing.B) { runEchoClientTPC(b, 1000) }
func Benchmark__10KB_ClientTPCav(b *testing.B) { runEchoClientTPC(b, 10000) }
func Benchmark_100KB_ClientTPCav(b *testing.B) { runEchoClientTPC(b, 100000) }
// Benchmark for non-streaming RPC with a Server ThirdPartyCaveat.
func runEchoServerTPC(b *testing.B, payloadSize int) {
internal.CallEcho(b, ctx, tpcServerAddr, b.N, payloadSize, benchmark.AddStats(b, 16))
}
func Benchmark____1B_ServerTPCav(b *testing.B) { runEchoServerTPC(b, 1) }
func Benchmark___10B_ServerTPCav(b *testing.B) { runEchoServerTPC(b, 10) }
func Benchmark__100B_ServerTPCav(b *testing.B) { runEchoServerTPC(b, 100) }
func Benchmark___1KB_ServerTPCav(b *testing.B) { runEchoServerTPC(b, 1000) }
func Benchmark__10KB_ServerTPCav(b *testing.B) { runEchoServerTPC(b, 10000) }
func Benchmark_100KB_ServerTPCav(b *testing.B) { runEchoServerTPC(b, 100000) }
// Benchmark for non-streaming RPC with a proxied Server.
func runEchoProxiedServer(b *testing.B, payloadSize int) {
internal.CallEcho(b, ctx, proxiedServerAddr, b.N, payloadSize, benchmark.AddStats(b, 16))
}
func Benchmark____1B_ProxiedServer(b *testing.B) { runEchoProxiedServer(b, 1) }
func Benchmark___10B_ProxiedServer(b *testing.B) { runEchoProxiedServer(b, 10) }
func Benchmark__100B_ProxiedServer(b *testing.B) { runEchoProxiedServer(b, 100) }
func Benchmark___1KB_ProxiedServer(b *testing.B) { runEchoProxiedServer(b, 1000) }
func Benchmark__10KB_ProxiedServer(b *testing.B) { runEchoProxiedServer(b, 10000) }
func Benchmark_100KB_ProxiedServer(b *testing.B) { runEchoProxiedServer(b, 100000) }
// Benchmarks for streaming RPC.
func runEchoStream(b *testing.B, chunkCnt, payloadSize int) {
internal.CallEchoStream(b, ctx, serverAddr, b.N, chunkCnt, payloadSize, benchmark.AddStats(b, 16))
}
func Benchmark____1_chunk_____1B(b *testing.B) { runEchoStream(b, 1, 1) }
func Benchmark____1_chunk____10B(b *testing.B) { runEchoStream(b, 1, 10) }
func Benchmark____1_chunk___100B(b *testing.B) { runEchoStream(b, 1, 100) }
func Benchmark____1_chunk____1KB(b *testing.B) { runEchoStream(b, 1, 1000) }
func Benchmark____1_chunk___10KB(b *testing.B) { runEchoStream(b, 1, 10000) }
func Benchmark____1_chunk__100KB(b *testing.B) { runEchoStream(b, 1, 100000) }
func Benchmark___10_chunk_____1B(b *testing.B) { runEchoStream(b, 10, 1) }
func Benchmark___10_chunk____10B(b *testing.B) { runEchoStream(b, 10, 10) }
func Benchmark___10_chunk___100B(b *testing.B) { runEchoStream(b, 10, 100) }
func Benchmark___10_chunk____1KB(b *testing.B) { runEchoStream(b, 10, 1000) }
func Benchmark___10_chunk___10KB(b *testing.B) { runEchoStream(b, 10, 10000) }
func Benchmark___10_chunk__100KB(b *testing.B) { runEchoStream(b, 10, 100000) }
func Benchmark__100_chunk_____1B(b *testing.B) { runEchoStream(b, 100, 1) }
func Benchmark__100_chunk____10B(b *testing.B) { runEchoStream(b, 100, 10) }
func Benchmark__100_chunk___100B(b *testing.B) { runEchoStream(b, 100, 100) }
func Benchmark__100_chunk____1KB(b *testing.B) { runEchoStream(b, 100, 1000) }
func Benchmark__100_chunk___10KB(b *testing.B) { runEchoStream(b, 100, 10000) }
func Benchmark__100_chunk__100KB(b *testing.B) { runEchoStream(b, 100, 100000) }
func Benchmark___1K_chunk_____1B(b *testing.B) { runEchoStream(b, 1000, 1) }
func Benchmark___1K_chunk____10B(b *testing.B) { runEchoStream(b, 1000, 10) }
func Benchmark___1K_chunk___100B(b *testing.B) { runEchoStream(b, 1000, 100) }
func Benchmark___1K_chunk____1KB(b *testing.B) { runEchoStream(b, 1000, 1000) }
func Benchmark___1K_chunk___10KB(b *testing.B) { runEchoStream(b, 1000, 10000) }
func Benchmark___1K_chunk__100KB(b *testing.B) { runEchoStream(b, 1000, 100000) }
// Benchmarks for per-chunk throughput in streaming RPC.
func runPerChunk(b *testing.B, payloadSize int) {
internal.CallEchoStream(b, ctx, serverAddr, 1, b.N, payloadSize, benchmark.NewStats(1))
}
func Benchmark__per_chunk____1B(b *testing.B) { runPerChunk(b, 1) }
func Benchmark__per_chunk___10B(b *testing.B) { runPerChunk(b, 10) }
func Benchmark__per_chunk__100B(b *testing.B) { runPerChunk(b, 100) }
func Benchmark__per_chunk___1KB(b *testing.B) { runPerChunk(b, 1000) }
func Benchmark__per_chunk__10KB(b *testing.B) { runPerChunk(b, 10000) }
func Benchmark__per_chunk_100KB(b *testing.B) { runPerChunk(b, 100000) }
// Benchmarks for non-streaming RPC while running streaming RPC in background.
func runMux(b *testing.B, payloadSize, chunkCntB, payloadSizeB int) {
_, stop := internal.StartEchoStream(&testing.B{}, ctx, serverAddr, 0, chunkCntB, payloadSizeB, benchmark.NewStats(1))
internal.CallEcho(b, ctx, serverAddr, b.N, payloadSize, benchmark.AddStats(b, 16))
stop()
}
func Benchmark___10B_mux__100_chunks___10B(b *testing.B) { runMux(b, 10, 100, 10) }
func Benchmark___10B_mux__100_chunks__100B(b *testing.B) { runMux(b, 10, 100, 100) }
func Benchmark___10B_mux__100_chunks___1KB(b *testing.B) { runMux(b, 10, 100, 1000) }
func Benchmark___10B_mux__100_chunks__10KB(b *testing.B) { runMux(b, 10, 100, 10000) }
func Benchmark___10B_mux___1K_chunks___10B(b *testing.B) { runMux(b, 10, 1000, 10) }
func Benchmark___10B_mux___1K_chunks__100B(b *testing.B) { runMux(b, 10, 1000, 100) }
func Benchmark___10B_mux___1K_chunks___1KB(b *testing.B) { runMux(b, 10, 1000, 1000) }
func Benchmark___10B_mux___1K_chunks__10KB(b *testing.B) { runMux(b, 10, 1000, 10000) }
func Benchmark___1KB_mux__100_chunks___10B(b *testing.B) { runMux(b, 1000, 100, 10) }
func Benchmark___1KB_mux__100_chunks__100B(b *testing.B) { runMux(b, 1000, 100, 100) }
func Benchmark___1KB_mux__100_chunks___1KB(b *testing.B) { runMux(b, 1000, 100, 1000) }
func Benchmark___1KB_mux__100_chunks__10KB(b *testing.B) { runMux(b, 1000, 100, 10000) }
func Benchmark___1KB_mux___1K_chunks___10B(b *testing.B) { runMux(b, 1000, 1000, 10) }
func Benchmark___1KB_mux___1K_chunks__100B(b *testing.B) { runMux(b, 1000, 1000, 100) }
func Benchmark___1KB_mux___1K_chunks___1KB(b *testing.B) { runMux(b, 1000, 1000, 1000) }
func Benchmark___1KB_mux___1K_chunks__10KB(b *testing.B) { runMux(b, 1000, 1000, 10000) }
// Benchmark for measuring RPC connection time including authentication.
//
// rpc.Client doesn't export an interface for closing connection. So we
// recreate a client on every iteration (avoiding re-use of underlying
// network connections between iterations since the stream manager is
// recreated with the client).
func benchmarkConnectionEstablishment(b *testing.B, ctx *context.T, server string) {
var buf [1]byte
for i := 0; i < b.N; i++ {
// The client and server are running with the same principal
// here (since they are both keyed off the same 'ctx').
cctx, cancel := context.WithCancel(ctx)
cctx, _, err := v23.WithNewClient(cctx)
if err != nil {
b.Fatalf("Iteration %d/%d: %v", i, b.N, err)
}
// Execute an RPC to ensure that the connection is established
// by both ends.
if _, err := bm.BenchmarkClient(server).Echo(cctx, buf[:]); err != nil {
b.Fatal(err)
}
cancel()
}
}
func benchmarkPrivateConnectionEstablishment(b *testing.B, serverAuth ...security.BlessingPattern) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
_, server, err := v23.WithNewServer(
bcrypter.WithCrypter(ctx, serverCrypter),
"",
internal.NewService(),
security.DefaultAuthorizer(),
options.ServerPeers(serverAuth))
if err != nil {
b.Fatal(err)
}
cctx := bcrypter.WithCrypter(ctx, clientCrypter)
benchmarkConnectionEstablishment(b, cctx, server.Status().Endpoints[0].Name())
}
func BenchmarkConnectionEstablishment(b *testing.B) {
benchmarkConnectionEstablishment(b, ctx, serverAddr)
}
func BenchmarkProxiedConnectionEstablishment(b *testing.B) {
benchmarkConnectionEstablishment(b, ctx, proxiedServerAddr)
}
func BenchmarkPrivateConnectionEstablishment1(b *testing.B) {
benchmarkPrivateConnectionEstablishment(b, "root:client")
}
func BenchmarkPrivateConnectionEstablishment3(b *testing.B) {
benchmarkPrivateConnectionEstablishment(b, "root:client", "root:client2", "root:client3")
}
// A single empty test to avoid:
// testing: warning: no tests to run
// from showing up when running benchmarks in this package via "go test"
func TestNoOp(t *testing.T) {}
func TestMain(m *testing.M) {
// We do not use defer here since this program will exit at the end of
// this function through os.Exit().
var shutdown v23.Shutdown
ctx, shutdown = test.V23Init()
setupServerClient(ctx)
setupExpiryCaveatServerClient(ctx)
setupThirdPartyCaveatServerClient(ctx)
setupProxiedServerClient(ctx)
setupPrivateMutualAuthentication(ctx)
r := benchmark.RunTestMain(m)
shutdown()
os.Exit(r)
}
func setupServerClient(ctx *context.T) {
// Server with no ThirdPartyCaveat.
_, server, err := v23.WithNewServer(ctx, "", internal.NewService(), security.DefaultAuthorizer())
if err != nil {
ctx.Fatalf("NewServer failed: %v", err)
}
serverAddr = server.Status().Endpoints[0].Name()
// Create a Conn to exclude the Conn setup time from the benchmark.
internal.CallEcho(&testing.B{}, ctx, serverAddr, 1, 0, benchmark.NewStats(1))
}
func setupExpiryCaveatServerClient(ctx *context.T) {
p := v23.GetPrincipal(ctx)
b, _ := p.BlessingStore().Default()
expcav, err := security.NewExpiryCaveat(time.Now().Add(time.Hour))
if err != nil {
ctx.Fatal(err)
}
expcavb, err := p.Bless(p.PublicKey(), b, "expcav", expcav)
if err != nil {
ctx.Fatal(err)
}
store := lsecurity.FixedBlessingsStore(expcavb, nil)
expcavp, err := lsecurity.ForkPrincipal(p, store, p.Roots())
if err != nil {
ctx.Fatal(err)
}
expCtx, err = v23.WithPrincipal(ctx, expcavp)
if err != nil {
ctx.Fatal(err)
}
// Create a server with the ExpiryCaveat principal.
_, expServer, err := v23.WithNewServer(expCtx, "", internal.NewService(), security.DefaultAuthorizer())
if err != nil {
ctx.Fatalf("NewServer failed: %v", err)
}
expServerAddr = expServer.Status().Endpoints[0].Name()
// Create Conns to exclude the Conn setup time from the benchmark.
internal.CallEcho(&testing.B{}, expCtx, serverAddr, 1, 0, benchmark.NewStats(1))
internal.CallEcho(&testing.B{}, ctx, expServerAddr, 1, 0, benchmark.NewStats(1))
}
func setupThirdPartyCaveatServerClient(ctx *context.T) {
// Create a DischargeServer and a principal with a corresponding ThirdPartyCaveat.
tpcav := internal.NewDischargeServer(ctx)
p := v23.GetPrincipal(ctx)
b, _ := p.BlessingStore().Default()
tpcavb, err := p.Bless(p.PublicKey(), b, "tpcav", tpcav)
if err != nil {
ctx.Fatal(err)
}
store := lsecurity.FixedBlessingsStore(tpcavb, nil)
tpcavp, err := lsecurity.ForkPrincipal(p, store, p.Roots())
if err != nil {
ctx.Fatal(err)
}
tpcCtx, err = v23.WithPrincipal(ctx, tpcavp)
if err != nil {
ctx.Fatal(err)
}
// Create a server with the ThirdPartyCaveat principal.
_, tpcServer, err := v23.WithNewServer(tpcCtx, "", internal.NewService(), security.DefaultAuthorizer())
if err != nil {
ctx.Fatalf("NewServer failed: %v", err)
}
tpcServerAddr = tpcServer.Status().Endpoints[0].Name()
// Create Conns to exclude the Conn setup time from the benchmark.
internal.CallEcho(&testing.B{}, tpcCtx, serverAddr, 1, 0, benchmark.NewStats(1))
internal.CallEcho(&testing.B{}, ctx, tpcServerAddr, 1, 0, benchmark.NewStats(1))
}
func setupProxiedServerClient(ctx *context.T) {
// Create a proxy.
pxy, err := xproxy.New(ctx, "", security.DefaultAuthorizer())
if err != nil {
ctx.Fatal(err)
}
pname := pxy.ListeningEndpoints()[0].Name()
// Create a server that listens on the proxy.
pCtx := v23.WithListenSpec(ctx, rpc.ListenSpec{Proxy: pname})
_, proxiedServer, err := v23.WithNewServer(pCtx, "", internal.NewService(), security.DefaultAuthorizer())
if err != nil {
ctx.Fatal(err)
}
status := testutil.WaitForProxyEndpoints(proxiedServer, pname)
proxiedServerAddr = status.Endpoints[0].Name()
// Create Conns to exclude the Conn setup time from the benchmark.
internal.CallEcho(&testing.B{}, ctx, proxiedServerAddr, 1, 0, benchmark.NewStats(1))
}
func setupPrivateMutualAuthentication(ctx *context.T) {
master, err := ibe.SetupBB2()
if err != nil {
ctx.Fatalf("ibe.SetupBB2: %v", err)
}
root := bcrypter.NewRoot("root", master)
clientKey, err := root.Extract(ctx, "root:client")
if err != nil {
ctx.Fatal(err)
}
// The client needs a key to be able to decrypt the server blessings.
clientCrypter = bcrypter.NewCrypter()
if err := clientCrypter.AddKey(ctx, clientKey); err != nil {
ctx.Fatal(err)
}
// The server needs the params object so that it can encrypt its blessings.
serverCrypter = bcrypter.NewCrypter()
if err := serverCrypter.AddParams(ctx, root.Params()); err != nil {
ctx.Fatal(err)
}
}