/
reqres.go
193 lines (177 loc) · 4.23 KB
/
reqres.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
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"encoding/pem"
"errors"
"flag"
"fmt"
"io"
"math/big"
"os"
"strconv"
"strings"
"sync"
"time"
quic "github.com/ZHJBOBO/multipath-quic-go"
"github.com/ZHJBOBO/multipath-quic-go/internal/utils"
)
const (
intervalTime = 400 * time.Millisecond
maxID = 100
)
var (
addr = "localhost:4242"
counter int
counterLock sync.Mutex
delays = make([]time.Duration, 0)
messageID int
missed int
printChan = make(chan struct{}, 1)
querySize = 750
resSize = 750
runTime = 30 * time.Second
sentTime = make(map[int]time.Time)
startTime time.Time
stream quic.Stream
)
// We start a server echoing data on the first stream the client opens,
// then connect with a client, send the message, and wait for its receipt.
func main() {
addrF := flag.String("addr", "localhost:4242", "Address to dial")
runTimeF := flag.Duration("runTime", 30*time.Second, "Running time of test")
multipath := flag.Bool("m", false, "multipath")
verbose := flag.Bool("v", false, "Verbose mode")
flag.Parse()
addr = *addrF
runTime = *runTimeF + 200*time.Millisecond
if *verbose {
utils.SetLogLevel(utils.LogLevelDebug)
} else {
utils.SetLogLevel(utils.LogLevelInfo)
}
printChan <- struct{}{}
err := clientMain(*multipath)
fmt.Printf("Exiting client main with error %v\n", err)
printer()
}
func max(a int, b int) int {
if a > b {
return a
}
return b
}
func printer() {
<-printChan
fmt.Printf("Missed: %d\n", missed)
for _, d := range delays {
fmt.Println(int64(d / time.Millisecond))
}
time.Sleep(time.Second)
os.Exit(0)
}
func sendMessage() error {
if stream == nil {
return errors.New("Closed stream")
}
sentTime[messageID] = time.Now()
startString := strconv.Itoa(messageID) + "&" + strconv.Itoa(querySize) + "&" + strconv.Itoa(resSize) + "&" + "0" + "&"
messageID = (messageID + 1) % maxID
msg := startString + strings.Repeat("0", querySize-len(startString))
_, err := stream.Write([]byte(msg))
return err
}
func clientSender() {
sendLoop:
for {
if stream == nil {
break sendLoop
}
time.Sleep(intervalTime)
if time.Since(startTime) >= runTime {
stream.Close()
break sendLoop
} else {
err := sendMessage()
if err != nil {
stream.Close()
break sendLoop
}
}
}
printer()
}
func clientMain(multipath bool) error {
cfgClient := &quic.Config{
CreatePaths: multipath,
}
tlsConfig := &tls.Config{InsecureSkipVerify: true}
fmt.Println("Trying to connect...")
// TODO: specify address
session, err := quic.DialAddr(addr, tlsConfig, cfgClient)
if err != nil {
panic(err)
}
fmt.Println("Connected")
startTime = time.Now()
stream, err = session.OpenStreamSync()
if err != nil {
panic(err)
}
go clientSender()
buf := make([]byte, resSize)
listenLoop:
for {
if stream == nil {
break listenLoop
}
read, err := io.ReadFull(stream, buf)
receivedTime := time.Now()
if err != nil {
return err
}
if read != resSize {
return errors.New("Read does not match resSize")
}
msg := string(buf)
splitMsg := strings.Split(msg, "&")
msgID, _ := strconv.Atoi(splitMsg[0])
sent, ok := sentTime[msgID]
if !ok {
continue
}
delays = append(delays, receivedTime.Sub(sent))
delete(sentTime, msgID)
counterLock.Lock()
counter -= querySize
counterLock.Unlock()
}
return nil
}
// A wrapper for io.Writer that also logs the message.
type loggingWriter struct{ io.Writer }
func (w loggingWriter) Write(b []byte) (int, error) {
fmt.Printf("Server: Got '%s'\n", string(b))
return w.Writer.Write(b)
}
// Setup a bare-bones TLS config for the server
func generateTLSConfig() *tls.Config {
key, err := rsa.GenerateKey(rand.Reader, 1024)
if err != nil {
panic(err)
}
template := x509.Certificate{SerialNumber: big.NewInt(1)}
certDER, err := x509.CreateCertificate(rand.Reader, &template, &template, &key.PublicKey, key)
if err != nil {
panic(err)
}
keyPEM := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)})
certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certDER})
tlsCert, err := tls.X509KeyPair(certPEM, keyPEM)
if err != nil {
panic(err)
}
return &tls.Config{Certificates: []tls.Certificate{tlsCert}}
}