-
Notifications
You must be signed in to change notification settings - Fork 1
/
single_node.go
81 lines (69 loc) · 1.37 KB
/
single_node.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 main
import (
"bytes"
"encoding/binary"
"encoding/hex"
"log"
"sync"
)
func SingleNode(toCall string) []byte {
var (
req []byte
err error
responseChannel = make(chan *Response, *totalCalls*2)
)
if *requestBodyHex { //hex
req, err = hex.DecodeString(*requestBody)
if err != nil {
log.Fatalf("requestBody is invalid err= %v \n hex=%s\n", err, *requestBody)
}
} else {
req = []byte(*requestBody)
}
if *addLenField {
req = add2ByteLen(req)
}
benchTime := NewTimer()
benchTime.Reset()
//TODO check ulimit
wg := &sync.WaitGroup{}
for i := 0; i < *numConnections; i++ {
go StartClient(
toCall,
req,
*disableKeepAlives,
responseChannel,
wg,
*totalCalls,
)
wg.Add(1)
}
wg.Wait()
result := CalcStats(
responseChannel,
benchTime.Duration(),
)
return result
}
//为byte添加2字节长度头
func add2ByteLen(in []byte) []byte {
b1 := intTo2Bytes(len(in))
buffer := bytes.NewBuffer([]byte{})
buffer.Write(b1)
buffer.Write(in)
return buffer.Bytes()
}
//intTo2Bytes 转换int为2字节byte
func intTo2Bytes(i int) []byte {
buf := bytes.NewBuffer([]byte{})
tmp := uint16(i)
binary.Write(buf, binary.BigEndian, tmp)
return buf.Bytes()
}
// bytes to int 16
func bytesTo16Int(b []byte) int {
buf := bytes.NewBuffer(b)
var tmp uint16
binary.Read(buf, binary.BigEndian, &tmp)
return int(tmp)
}