-
Notifications
You must be signed in to change notification settings - Fork 1
/
read.go
81 lines (68 loc) · 1.74 KB
/
read.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
package wt
import (
"io"
"log"
"strconv"
"time"
"go.k6.io/k6/metrics"
)
func (c *Connection) ReadAll() []byte {
rsp, err := io.ReadAll(c.activeStream)
defer c.logReadMetrics(len(rsp))
if err != nil {
log.Println("Read error: " + err.Error())
}
return rsp
}
func (c *Connection) ReadFull(expectedReadLength int) []byte {
rsp := make([]byte, expectedReadLength)
n, err := io.ReadFull(c.activeStream, rsp)
defer c.logReadMetrics(n)
if err != nil {
log.Println("Read error: " + err.Error())
if n != expectedReadLength {
log.Println("Read n: " + strconv.Itoa(n) + " does not match the expected length of: " + strconv.Itoa(expectedReadLength))
}
}
return rsp
}
func (c *Connection) ReadAtLeast(maxReadLength int, minReadLength int) []byte {
rsp := make([]byte, maxReadLength)
n, err := io.ReadAtLeast(c.activeStream, rsp, minReadLength)
defer c.logReadMetrics(n)
if err != nil {
log.Println("Read error: " + err.Error())
if n < minReadLength {
log.Println("Read n: " + strconv.Itoa(n) + " is smaller than expected minimum: " + strconv.Itoa(minReadLength))
}
}
return rsp
}
func (c *Connection) logReadMetrics(n int) {
state := c.vu.State()
ctx := c.vu.Context()
if state == nil || ctx == nil {
return
}
now := time.Now()
metrics.PushIfNotDone(ctx, state.Samples, metrics.ConnectedSamples{
Samples: []metrics.Sample{
{
Time: now,
TimeSeries: metrics.TimeSeries{Metric: c.metrics.StreamsReadCount},
Value: 1,
},
{
Time: now,
TimeSeries: metrics.TimeSeries{Metric: c.metrics.StreamsReadBytes},
Value: float64(n),
},
{
Time: now,
TimeSeries: metrics.TimeSeries{Metric: c.metrics.StreamsReadSize},
Value: float64(n),
},
},
Time: now,
})
}