-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark_unary_test.go
302 lines (268 loc) · 7.68 KB
/
benchmark_unary_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
// Copyright (c) 2021 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package main
import (
"fmt"
"math/rand"
"testing"
"time"
"github.com/yarpc/yab/encoding"
"github.com/yarpc/yab/transport"
"github.com/opentracing/opentracing-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/uber/tchannel-go/testutils"
"go.uber.org/atomic"
)
func benchmarkMethodForTest(t *testing.T, procedure string, p transport.Protocol) benchmarkUnaryMethod {
return benchmarkMethodForROpts(t, RequestOptions{
Encoding: encoding.Thrift,
ThriftFile: validThrift,
Procedure: procedure,
}, p)
}
func benchmarkMethodForROpts(t *testing.T, rOpts RequestOptions, p transport.Protocol) benchmarkUnaryMethod {
serializer, err := NewSerializer(Options{ROpts: rOpts}, resolvedProtocolEncoding{
protocol: p,
enc: encoding.Thrift,
})
require.NoError(t, err, "Failed to create Thrift serializer")
req, err := serializer.Request(nil)
require.NoError(t, err, "Failed to serialize Thrift body")
req.Timeout = time.Second
return benchmarkUnaryMethod{serializer, req}
}
func TestBenchmarkMethodWarmTransport(t *testing.T) {
s := newServer(t)
defer s.shutdown()
s.register(fooMethod, methods.echo())
tests := []struct {
peer string
method string
wantErr string
}{
{
peer: s.hostPort(),
},
// getTransport error
{
peer: testutils.GetClosedHostPort(t),
wantErr: "connection refused",
},
// makeRequest error
{
peer: s.hostPort(),
method: "Simple::unknown",
wantErr: "no handler for service",
},
}
for _, tt := range tests {
m := benchmarkMethodForTest(t, fooMethod, transport.TChannel)
if tt.method != "" {
m.req.Method = tt.method
}
tOpts := TransportOptions{
CallerName: "bar",
ServiceName: "foo",
Peers: []string{tt.peer},
}
transport, err := warmTransport(m, tOpts, resolvedProtocolEncoding{
protocol: transport.TChannel,
enc: encoding.JSON,
}, 1 /* warmupRequests */)
if tt.wantErr != "" {
if assert.Error(t, err, "WarmTransport should fail") {
assert.Contains(t, err.Error(), tt.wantErr, "Invalid error message")
}
continue
}
assert.NoError(t, err, "WarmTransport should succeed")
assert.NotNil(t, transport, "Failed to get transport")
}
}
func TestBenchmarkMethodCall(t *testing.T) {
s := newServer(t)
defer s.shutdown()
thriftExBytes := []byte{
12, /* struct */
0, 1, /* field ID */
0, /* STOP */
0, /* STOP */
}
s.register(fooMethod, methods.echo())
s.register("Simple::thriftEx", methods.customArg3(thriftExBytes))
tests := []struct {
method string
reqMethod string
wantErr string
}{
{
method: fooMethod,
},
{
method: "Simple::thriftEx",
wantErr: "ex ThriftException",
},
{
method: fooMethod,
reqMethod: "Simple::unknown",
wantErr: "no handler for service",
},
}
tOpts := TransportOptions{
CallerName: "bar",
ServiceName: "foo",
Peers: []string{s.hostPort()},
}
tp, err := getTransport(tOpts, _resolvedTChannelThrift, opentracing.NoopTracer{})
require.NoError(t, err, "Failed to get transport")
for _, tt := range tests {
m := benchmarkMethodForTest(t, tt.method, transport.TChannel)
if tt.reqMethod != "" {
m.req.Method = tt.reqMethod
}
res, err := m.Call(tp)
if tt.wantErr != "" {
if assert.Error(t, err, "call should fail") {
assert.Contains(t, err.Error(), tt.wantErr, "call should return 0 duration")
}
continue
}
assert.NoError(t, err, "call should not fail")
assert.True(t, res.Latency() > time.Microsecond, "duration was too short, got %v", res.Latency())
}
}
func TestPeerBalancer(t *testing.T) {
tests := []struct {
seed int64
peers []string
want []string
}{
{
seed: 1,
peers: []string{"1"},
want: []string{"1", "1", "1"},
},
{
seed: 1,
peers: []string{"1", "2"},
want: []string{"2", "1", "2"},
},
{
seed: 2,
peers: []string{"1", "2"},
want: []string{"1", "2", "1"},
},
{
seed: 1,
peers: []string{"1", "2", "3", "4", "5"},
want: []string{"2", "3", "4"},
},
}
for _, tt := range tests {
rand.Seed(tt.seed)
peerFor := peerBalancer(tt.peers)
for i, want := range tt.want {
got, index := peerFor(i)
assert.Equal(t, want, got, "peerBalancer(%v) seed %v i %v failed", tt.peers, tt.seed, i)
assert.Equal(t, want, tt.peers[index], "peerBalancer(%v) seed %v i %v unexpected index %v", tt.peers, tt.seed, i, index)
}
}
}
func TestBenchmarkMethodWarmTransportsSuccess(t *testing.T) {
const numServers = 5
m := benchmarkMethodForTest(t, fooMethod, transport.TChannel)
counters := make([]*atomic.Int32, numServers)
servers := make([]*server, numServers)
serverHPs := make([]string, numServers)
for i := range servers {
servers[i] = newServer(t)
defer servers[i].shutdown()
serverHPs[i] = servers[i].hostPort()
counter, handler := methods.counter()
counters[i] = counter
servers[i].register(fooMethod, handler)
}
tOpts := TransportOptions{
CallerName: "bar",
ServiceName: "foo",
Peers: serverHPs,
}
transports, err := warmTransports(m, numServers, tOpts, _resolvedTChannelThrift, 1 /* warmupRequests */)
assert.NoError(t, err, "WarmTransports should not fail")
assert.Equal(t, numServers, len(transports), "Got unexpected number of transports")
for i, transport := range transports {
assert.NotNil(t, transport, "transports[%v] should not be nil", i)
}
// Verify that each server has received one call.
for i, counter := range counters {
assert.EqualValues(t, 1, counter.Load(), "Server %v received unexpected number of calls", i)
}
}
func TestBenchmarkMethodWarmTransportsError(t *testing.T) {
m := benchmarkMethodForTest(t, fooMethod, transport.TChannel)
tests := []struct {
success int
warmup int
wantErr bool
}{
{
success: 0,
warmup: 0,
wantErr: false,
},
{
success: 0,
warmup: 1,
wantErr: true,
},
{
success: 90,
warmup: 9,
wantErr: false,
},
{
success: 90,
warmup: 10,
wantErr: true,
},
}
for _, tt := range tests {
s := newServer(t)
defer s.shutdown()
msg := fmt.Sprintf("success: %v warmup: %v", tt.success, tt.warmup)
// Simple::foo will succeed for tt requests, then start failing.
var counter atomic.Int32
s.register(fooMethod, methods.errorIf(func() bool {
return counter.Inc() > int32(tt.success)
}))
tOpts := TransportOptions{
CallerName: "bar",
ServiceName: "foo",
Peers: []string{s.hostPort()},
}
_, err := warmTransports(m, 10, tOpts, _resolvedTChannelThrift, tt.warmup)
if tt.wantErr {
assert.Error(t, err, "%v: WarmTransports should fail", msg)
} else {
assert.NoError(t, err, "%v: WarmTransports should succeed", msg)
}
}
}