-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-create-concurrent.go
More file actions
363 lines (276 loc) · 11.1 KB
/
test-create-concurrent.go
File metadata and controls
363 lines (276 loc) · 11.1 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
"strings"
"time"
"crypto/tls"
"github.com/numbleroot/pluto-evaluation/config"
"github.com/numbleroot/pluto-evaluation/utils"
"github.com/numbleroot/pluto/imap"
)
// Functions
func PlutoTester(start chan struct{}, done chan struct{}, plutoC *imap.Connection, connNum int, runs int, plutoLogFolder string, logFileTime time.Time) {
// Define an individual test log file name.
plutoLogFileName := fmt.Sprintf("%s/conn-%03d.log", plutoLogFolder, connNum)
// Attempt to create a test log file containing
// measured test times for pluto system.
plutoLogFile, err := os.Create(plutoLogFileName)
if err != nil {
log.Fatalf("Failed to create test log file '%s': %s\n", plutoLogFileName, err.Error())
}
// Sync to storage and close on any exit.
defer plutoLogFile.Close()
defer plutoLogFile.Sync()
// Prepend file with meta information about this test.
plutoLogFile.WriteString(fmt.Sprintf("Subject: Concurrent CREATE\nPlatform: pluto\nDate: %s\n-----\n", logFileTime.Format("2006-01-02-15-04-05")))
// Wait for signal to start test.
<-start
// Execute supplied amount of tests agains pluto.
for num := 1; num <= runs; num++ {
// Prepare command to send.
command := fmt.Sprintf("create%d CREATE evaluation-mailbox-%d", num, num)
// Take current time stamp.
timeStart := time.Now().UnixNano()
// Send CREATE commmand to server.
err := plutoC.Send(false, command)
if err != nil {
log.Fatalf("%d: Failed during sending CREATE command: %s\n", num, err.Error())
}
// Receive answer to CREATE request.
answer, err := plutoC.Receive(false)
if err != nil {
log.Fatalf("%d: Error receiving response to CREATE: %s\n", num, err.Error())
}
// Take time stamp after function execution.
timeEnd := time.Now().UnixNano()
if strings.Contains(answer, "CREATE completed") != true {
log.Fatalf("%d: Server responded unexpectedly to CREATE command: %s\n", num, answer)
}
// Calculate round-trip time.
rtt := timeEnd - timeStart
// Append log line to file.
plutoLogFile.WriteString(fmt.Sprintf("%d, %d\n", num, rtt))
}
// Log out.
err = plutoC.Send(false, "createZ LOGOUT")
if err != nil {
log.Fatalf("Error during LOGOUT: %s\n", err.Error())
}
// Receive first part of answer.
answer, err := plutoC.Receive(false)
if err != nil {
log.Fatalf("Error receiving first part of LOGOUT response: %s\n", err.Error())
}
// Receive next line from server.
nextAnswer, err := plutoC.Receive(false)
if err != nil {
log.Fatalf("Error receiving second part of LOGOUT response: %s\n", err.Error())
}
answer = fmt.Sprintf("%s\r\n%s", answer, nextAnswer)
if strings.Contains(answer, "LOGOUT completed") != true {
log.Fatalf("Server responded unexpectedly to LOGOUT: %s\n", answer)
}
// Send done signal back.
done <- struct{}{}
}
func DovecotTester(start chan struct{}, done chan struct{}, dovecotC *imap.Connection, connNum int, runs int, dovecotLogFolder string, logFileTime time.Time) {
// Define an individual test log file name.
dovecotLogFileName := fmt.Sprintf("%s/conn-%03d.log", dovecotLogFolder, connNum)
// Attempt to create a test log file containing
// measured test times for Dovecot system.
dovecotLogFile, err := os.Create(dovecotLogFileName)
if err != nil {
log.Fatalf("Failed to create test log file '%s': %s\n", dovecotLogFileName, err.Error())
}
// Sync to storage and close on any exit.
defer dovecotLogFile.Close()
defer dovecotLogFile.Sync()
// Prepend file with meta information about this test.
dovecotLogFile.WriteString(fmt.Sprintf("Subject: Concurrent CREATE\nPlatform: Dovecot\nDate: %s\n-----\n", logFileTime.Format("2006-01-02-15-04-05")))
// Wait for signal to start test.
<-start
for num := 1; num <= runs; num++ {
// Prepare command to send.
command := fmt.Sprintf("create%d CREATE evaluation-mailbox-%d", num, num)
// Take current time stamp.
timeStart := time.Now().UnixNano()
// Send CREATE commmand to server.
err := dovecotC.Send(false, command)
if err != nil {
log.Fatalf("%d: Failed during sending CREATE command: %s\n", num, err.Error())
}
// Receive answer to CREATE request.
answer, err := dovecotC.Receive(false)
if err != nil {
log.Fatalf("%d: Error receiving response to CREATE: %s\n", num, err.Error())
}
// Take time stamp after function execution.
timeEnd := time.Now().UnixNano()
if strings.Contains(answer, "Create completed") != true {
log.Fatalf("%d: Server responded unexpectedly to CREATE command: %s\n", num, answer)
}
// Calculate round-trip time.
rtt := timeEnd - timeStart
// Append log line to file.
dovecotLogFile.WriteString(fmt.Sprintf("%d, %d\n", num, rtt))
}
// Log out.
err = dovecotC.Send(false, "createZ LOGOUT")
if err != nil {
log.Fatalf("Error during LOGOUT: %s\n", err.Error())
}
// Receive first part of answer.
answer, err := dovecotC.Receive(false)
if err != nil {
log.Fatalf("Error receiving first part of LOGOUT response: %s\n", err.Error())
}
// Receive next line from server.
nextAnswer, err := dovecotC.Receive(false)
if err != nil {
log.Fatalf("Error receiving second part of LOGOUT response: %s\n", err.Error())
}
answer = fmt.Sprintf("%s\r\n%s", answer, nextAnswer)
if strings.Contains(answer, "Logging out") != true {
log.Fatalf("Server responded unexpectedly to LOGOUT: %s\n", answer)
}
// Send done signal back.
done <- struct{}{}
}
func main() {
// Make test config file location and number of messages
// to send per test configurable.
configFlag := flag.String("config", "test-config.toml", "Specify location of config file that describes test setup configuration.")
runsFlag := flag.Int("runs", 100, "Specify how many times the command of this test is to be sent to server.")
flag.Parse()
runs := *runsFlag
log.Printf("Testing CREATE command concurrently on pluto and Dovecot...\n\n")
// Read configuration from file.
config, err := config.LoadConfig(*configFlag)
if err != nil {
log.Fatalf("Error loading config: %s\n", err.Error())
}
// Save number of concurrent tests for later use.
numTests := len(config.Pluto.ConcurrentTest.User)
// Check that same amount of users is configured for
// both systems, pluto and Dovecot.
if numTests != len(config.Dovecot.ConcurrentTest.User) {
log.Fatalf("Please configure an equal number of concurrent test users for pluto and Dovecot.\n")
}
// Prepare buffered channels to signal start and
// finish over to involved testing routines.
start := make(chan struct{}, numTests)
done := make(chan struct{}, numTests)
// Create needed TLS configs with correct certificates.
plutoTLSConfig, dovecotTLSConfig, err := utils.InitTLSConfigs(config)
if err != nil {
log.Fatalf("Error loading TLS configs for pluto and Dovecot: %s\n", err.Error())
}
// Create connection string to connect to pluto and Dovecot.
plutoIMAPAddr := fmt.Sprintf("%s:%s", config.Pluto.IP, config.Pluto.Port)
dovecotIMAPAddr := fmt.Sprintf("%s:%s", config.Dovecot.IP, config.Dovecot.Port)
// Take current time.
logFileTime := time.Now()
// Define a log folder for pluto and create it.
plutoLogFolder := fmt.Sprintf("results/pluto-create-concurrent-%s", logFileTime.Format("2006-01-02-15-04-05"))
err = os.Mkdir(plutoLogFolder, (os.ModeDir | 0700))
if err != nil {
log.Fatalf("Failed to create folder: %s\n", err.Error())
}
// Define a log folder for Dovecot and create it.
dovecotLogFolder := fmt.Sprintf("results/dovecot-create-concurrent-%s", logFileTime.Format("2006-01-02-15-04-05"))
err = os.Mkdir(dovecotLogFolder, (os.ModeDir | 0700))
if err != nil {
log.Fatalf("Failed to create folder: %s\n", err.Error())
}
log.Printf("Connecting %d times to pluto...\n", numTests)
for connNum := 0; connNum < numTests; connNum++ {
// Connect to remote pluto system.
plutoConn, err := tls.Dial("tcp", plutoIMAPAddr, plutoTLSConfig)
if err != nil {
log.Fatalf("Was unable to connect to remote pluto server: %s\n", err.Error())
}
// Create a new connection struct based on it.
plutoC := &imap.Connection{
OutConn: plutoConn,
OutReader: bufio.NewReader(plutoConn),
}
// Consume mandatory IMAP greeting.
_, err = plutoC.Receive(false)
if err != nil {
log.Fatalf("Error during receiving initial server greeting: %s\n", err.Error())
}
// Log in as first user.
err = plutoC.Send(false, fmt.Sprintf("createA LOGIN %s %s", config.Pluto.ConcurrentTest.User[connNum].Name, config.Pluto.ConcurrentTest.User[connNum].Password))
if err != nil {
log.Fatalf("Sending LOGIN to server failed with: %s\n", err.Error())
}
// Wait for success message.
answer, err := plutoC.Receive(false)
if err != nil {
log.Fatalf("Error during LOGIN as user %s: %s\n", config.Pluto.ConcurrentTest.User[connNum].Name, err.Error())
}
if strings.HasPrefix(answer, "createA OK") != true {
log.Fatalf("Server responded unexpectedly to LOGIN: %s\n", answer)
}
log.Printf("Logged in as '%s'.\n", config.Pluto.ConcurrentTest.User[connNum].Name)
// Dispatch to own goroutine.
go PlutoTester(start, done, plutoC, connNum, runs, plutoLogFolder, logFileTime)
}
// Send start signal to ready routines.
for signal := 0; signal < numTests; signal++ {
start <- struct{}{}
}
// Wait for all done signals to come in.
for signal := 0; signal < numTests; signal++ {
<-done
}
log.Printf("Done on pluto, sent %d * %d = %d messages.\n\n", numTests, runs, (numTests * runs))
// Run tests on Dovecot.
log.Printf("Connecting %d times to Dovecot...\n", numTests)
for connNum := 0; connNum < numTests; connNum++ {
// Connect to remote Dovecot system.
dovecotConn, err := tls.Dial("tcp", dovecotIMAPAddr, dovecotTLSConfig)
if err != nil {
log.Fatalf("Was unable to connect to remote Dovecot server: %s\n", err.Error())
}
// Create a new connection struct based on it.
dovecotC := &imap.Connection{
OutConn: dovecotConn,
OutReader: bufio.NewReader(dovecotConn),
}
// Consume mandatory IMAP greeting.
_, err = dovecotC.Receive(false)
if err != nil {
log.Fatalf("Error during receiving initial server greeting: %s\n", err.Error())
}
// Log in as first user.
err = dovecotC.Send(false, fmt.Sprintf("createA LOGIN %s %s", config.Dovecot.ConcurrentTest.User[connNum].Name, config.Dovecot.ConcurrentTest.User[connNum].Password))
if err != nil {
log.Fatalf("Sending LOGIN to server failed with: %s\n", err.Error())
}
// Wait for success message.
answer, err := dovecotC.Receive(false)
if err != nil {
log.Fatalf("Error during LOGIN as user %s: %s\n", config.Dovecot.ConcurrentTest.User[connNum].Name, err.Error())
}
if strings.HasPrefix(answer, "createA OK") != true {
log.Fatalf("Server responded unexpectedly to LOGIN: %s\n", answer)
}
log.Printf("Logged in as '%s'.\n", config.Dovecot.ConcurrentTest.User[connNum].Name)
// Dispatch into own goroutine.
go DovecotTester(start, done, dovecotC, connNum, runs, dovecotLogFolder, logFileTime)
}
// Send start signal to ready routines.
for signal := 0; signal < numTests; signal++ {
start <- struct{}{}
}
// Wait for all done signals to come in.
for signal := 0; signal < numTests; signal++ {
<-done
}
log.Printf("Done on Dovecot, sent %d * %d = %d messages.\n\n", numTests, runs, (numTests * runs))
}