-
Notifications
You must be signed in to change notification settings - Fork 159
/
dialer.go
181 lines (159 loc) · 5.04 KB
/
dialer.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
/*
Copyright NetFoundry, Inc.
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
https://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 loop2
import (
"fmt"
"github.com/michaelquigley/pfxlog"
"github.com/openziti/ziti/ziti-fabric-test/subcmd/loop2/pb"
"github.com/openziti/fabric/router/xgress_transport"
"github.com/openziti/foundation/identity/dotziti"
"github.com/openziti/foundation/identity/identity"
"github.com/openziti/foundation/transport"
"github.com/openziti/sdk-golang/ziti"
"github.com/spf13/cobra"
"net"
"strings"
"time"
)
func init() {
dialerCmd.Flags().StringVarP(&dialerCmdIdentity, "identity", "i", "default", ".ziti/identities.yml name")
dialerCmd.Flags().StringVarP(&dialerCmdEndpoint, "endpoint", "e", "tls:127.0.0.1:7001", "Endpoint address")
dialerCmd.Flags().BoolVarP(&dialerCmdDirect, "direct", "d", false, "Transmit direct (no ingress)")
dialerCmd.Flags().StringVarP(&dialerCmdService, "service", "s", "loop", "Service name for ingress")
loop2Cmd.AddCommand(dialerCmd)
}
var dialerCmd = &cobra.Command{
Use: "dialer <scenarioFile>",
Short: "Start loop2 dialer",
Args: cobra.ExactArgs(1),
Run: dialer,
}
var dialerCmdIdentity string
var dialerCmdEndpoint string
var dialerCmdDirect bool
var dialerCmdService string
func dialer(_ *cobra.Command, args []string) {
log := pfxlog.Logger()
scenario, err := LoadScenario(args[0])
if err != nil {
panic(err)
}
log.Debug(scenario)
resultChs := make(map[string]chan *loop2_pb.Result)
for _, workload := range scenario.Workloads {
log.Infof("executing workload [%s] with concurrency [%d]", workload.Name, workload.Concurrency)
for i := 0; i < int(workload.Concurrency); i++ {
name := fmt.Sprintf("%s:%d", workload.Name, i)
resultCh := make(chan *loop2_pb.Result, 1)
resultChs[name] = resultCh
go func() {
var conn net.Conn
if strings.HasPrefix(dialerCmdEndpoint, "edge:") {
context := ziti.NewContext()
service := strings.TrimPrefix(dialerCmdEndpoint, "edge:")
conn, err = context.Dial(service)
if err != nil {
panic(err)
}
} else {
endpoint, err := transport.ParseAddress(dialerCmdEndpoint)
if err != nil {
panic(err)
}
_, id, err := dotziti.LoadIdentity(dialerCmdIdentity)
if err != nil {
panic(err)
}
if dialerCmdDirect {
if conn, err = dialDirect(endpoint, id); err != nil {
panic(err)
}
} else {
serviceId := &identity.TokenId{Token: dialerCmdService}
if conn, err = dialIngress(endpoint, id, serviceId); err != nil {
panic(err)
}
}
}
workload := scenario.Workloads[0]
local := &loop2_pb.Test{
Name: name,
TxRequests: workload.Dialer.TxRequests,
TxPacing: workload.Dialer.TxPacing,
TxMaxJitter: workload.Dialer.TxMaxJitter,
RxRequests: workload.Listener.TxRequests,
RxTimeout: workload.Dialer.RxTimeout,
PayloadMinBytes: workload.Dialer.PayloadMinBytes,
PayloadMaxBytes: workload.Dialer.PayloadMaxBytes,
}
remote := &loop2_pb.Test{
Name: name,
TxRequests: workload.Listener.TxRequests,
TxPacing: workload.Listener.TxPacing,
TxMaxJitter: workload.Listener.TxMaxJitter,
RxRequests: workload.Dialer.TxRequests,
RxTimeout: workload.Listener.RxTimeout,
PayloadMinBytes: workload.Listener.PayloadMinBytes,
PayloadMaxBytes: workload.Listener.PayloadMaxBytes,
}
if proto, err := newProtocol(conn); err == nil {
if err := proto.txTest(remote); err == nil {
if err := proto.run(local); err == nil {
if result, err := proto.rxResult(); err == nil {
resultCh <- result
} else {
panic(err)
}
} else {
panic(err)
}
} else {
panic(err)
}
} else {
panic(err)
}
}()
time.Sleep(time.Duration(scenario.ConnectionDelay) * time.Millisecond)
}
}
failed := false
for name, resultCh := range resultChs {
result := <-resultCh
if !result.Success {
failed = true
log.Errorf("[%s] -> %s", name, result.Message)
} else {
log.Infof("[%s] -> success", name)
}
}
if failed {
panic("failures detected")
} else {
log.Info("success")
}
}
func dialDirect(endpoint transport.Address, id *identity.TokenId) (net.Conn, error) {
peer, err := endpoint.Dial("loop", id)
if err != nil {
return nil, err
}
return peer.Conn(), nil
}
func dialIngress(endpoint transport.Address, id, serviceId *identity.TokenId) (net.Conn, error) {
peer, err := xgress_transport.ClientDial(endpoint, id, serviceId)
if err != nil {
return nil, err
}
return peer.Conn(), nil
}