forked from NAlexandrov/xk6-tcp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp.go
162 lines (136 loc) · 3.52 KB
/
tcp.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
package tcp
import (
"net"
"time"
k6modules "go.k6.io/k6/js/modules"
k6metrics "go.k6.io/k6/metrics"
// "io"
// "bufio"
)
func init() {
k6modules.Register("k6/x/tcpx", New())
}
type (
// RootModule is the global module instance that will create module
// instances for each VU.
RootModule struct{}
// ModuleInstance represents an instance of the JS module.
ModuleInstance struct {
// comparator is the exported type
tcp *TCP
}
)
// Ensure the interfaces are implemented correctly.
var (
_ k6modules.Instance = &ModuleInstance{}
_ k6modules.Module = &RootModule{}
)
// New returns a pointer to a new RootModule instance.
func New() *RootModule {
return &RootModule{}
}
// NewModuleInstance implements the modules.Module interface returning a new instance for each VU.
func (*RootModule) NewModuleInstance(vu k6modules.VU) k6modules.Instance {
return &ModuleInstance{
tcp: &TCP{vu: vu},
}
}
// Exports implements the modules.Instance interface and returns the exported types for the JS module.
func (mi *ModuleInstance) Exports() k6modules.Exports {
return k6modules.Exports{
Default: mi.tcp,
}
}
type TCP struct {
vu k6modules.VU
}
// Viable values for the tcp type are the ones in golang net.Tcp
// "tcp", "tcp4", "tcp6", "" => will fallback to "tcp" then
func (tcp *TCP) Connect(tcpType string, address string) (*net.TCPConn, error) {
addr, err := net.ResolveTCPAddr(tcpType, address)
if err != nil {
return nil, err
}
conn, err := net.DialTCP(tcpType, nil, addr)
if err != nil {
return nil, err
}
return conn, nil
}
func (tcp *TCP) SetDeadlineToInfinite(conn *net.TCPConn) error {
return conn.SetDeadline(time.Time{})
}
func (tcp *TCP) SetKeepAlive(conn *net.TCPConn, shouldSendKeepAlive bool) error {
return conn.SetKeepAlive(shouldSendKeepAlive)
}
func (tcp *TCP) Write(conn *net.TCPConn, data []byte) error {
byteCount := len(data)
_, err := conn.Write(data)
if err != nil {
return err
}
state := tcp.vu.State()
dataSendMetric := state.BuiltinMetrics.DataSent
k6metrics.PushIfNotDone(tcp.vu.Context(), state.Samples, k6metrics.ConnectedSamples{
Samples: []k6metrics.Sample{
{
TimeSeries: k6metrics.TimeSeries{
Metric: dataSendMetric,
Tags: state.Tags.GetCurrentValues().Tags,
},
Value: float64(byteCount),
Time: time.Now().UTC(),
},
},
})
return nil
}
func (tcp *TCP) Read(conn *net.TCPConn, size int) ([]byte, error) {
buf := make([]byte, size)
_, err := conn.Read(buf)
if err != nil {
return nil, err
}
state := tcp.vu.State()
dataReceiveMetric := state.BuiltinMetrics.DataReceived
k6metrics.PushIfNotDone(tcp.vu.Context(), state.Samples, k6metrics.ConnectedSamples{
Samples: []k6metrics.Sample{
{
TimeSeries: k6metrics.TimeSeries{
Metric: dataReceiveMetric,
Tags: state.Tags.GetCurrentValues().Tags,
},
Value: float64(size),
Time: time.Now().UTC(),
},
},
})
return buf, nil
}
func (tcp *TCP) WriteBytesLn(conn *net.TCPConn, data []byte, delim []byte) error {
return tcp.Write(conn, append(data, delim...))
}
func (tcp *TCP) WriteLn(conn *net.TCPConn, data string, delim string) error {
return tcp.Write(conn, append([]byte(data), []byte(delim)...))
}
func (tcp *TCP) Close(conn *net.TCPConn) error {
err := conn.Close()
if err != nil {
return err
}
return nil
}
func (tcp *TCP) CloseWrite(conn *net.TCPConn) error {
err := conn.CloseWrite()
if err != nil {
return err
}
return nil
}
func (tcp *TCP) CloseRead(conn *net.TCPConn) error {
err := conn.CloseRead()
if err != nil {
return err
}
return nil
}