-
Notifications
You must be signed in to change notification settings - Fork 1
/
test-append.go
355 lines (271 loc) · 10.4 KB
/
test-append.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
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
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"log"
"os"
"strings"
"time"
"crypto/tls"
"github.com/numbleroot/pluto-evaluation/config"
"github.com/numbleroot/pluto-evaluation/messages"
"github.com/numbleroot/pluto-evaluation/utils"
"github.com/numbleroot/pluto/imap"
)
// Functions
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 APPEND command on pluto and Dovecot...\n")
// Read configuration from file.
config, err := config.LoadConfig(*configFlag)
if err != nil {
log.Fatalf("Error loading config: %s\n", err.Error())
}
// 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)
log.Printf("Connecting to pluto...\n")
// 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("appendA LOGIN %s %s", config.Pluto.AppendTest.Name, config.Pluto.AppendTest.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.AppendTest.Name, err.Error())
}
if strings.HasPrefix(answer, "appendA OK") != true {
log.Fatalf("Server responded unexpectedly to LOGIN: %s\n", answer)
}
log.Printf("Logged in as '%s'.\n", config.Pluto.AppendTest.Name)
// Take current time stamp and create log file name.
logFileTime := time.Now()
plutoLogFileName := fmt.Sprintf("results/pluto-append-%s.log", logFileTime.Format("2006-01-02-15-04-05"))
// 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: APPEND\nPlatform: pluto\nDate: %s\n-----\n", logFileTime.Format("2006-01-02-15-04-05")))
// Prepare buffer to append individual results to.
results := make([]int64, runs)
// Prepare message to append.
appendMsg := bytes.NewBufferString(messages.Msg01)
appendMsgSize := appendMsg.Len()
log.Printf("Running tests on pluto...\n")
for num := 1; num <= runs; num++ {
i := num - 1
// Prepare command to send.
command := fmt.Sprintf("append%d APPEND INBOX {%d}", num, appendMsgSize)
// Take current time stamp.
timeStart := time.Now().UnixNano()
// Send APPEND commmand to server.
err := plutoC.Send(false, command)
if err != nil {
log.Fatalf("%d: Failed during sending APPEND command: %s\n", num, err.Error())
}
// Receive answer to APPEND request.
answer, err := plutoC.Receive(false)
if err != nil {
log.Fatalf("%d: Error receiving response to APPEND: %s\n", num, err.Error())
}
if answer != "+ Ready for literal data" {
log.Fatalf("%d: Did not receive continuation command from server: %s\n", num, answer)
}
// Send mail message without additional newline.
_, err = fmt.Fprintf(plutoC.OutConn, "%s", appendMsg)
if err != nil {
log.Fatalf("%d: Sending mail message to server failed with: %s\n", num, err.Error())
}
// Receive answer to message transfer.
answer, err = plutoC.Receive(false)
if err != nil {
log.Fatalf("%d: Error during receiving response to APPEND: %s\n", num, err.Error())
}
// Take time stamp after function execution.
timeEnd := time.Now().UnixNano()
if strings.Contains(answer, "APPEND completed") != true {
log.Fatalf("%d: Server responded unexpectedly to APPEND command: %s\n", num, answer)
}
// Calculate round-trip time.
rtt := timeEnd - timeStart
// Store result in buffer.
results[i] = rtt
// Append log line to file.
plutoLogFile.WriteString(fmt.Sprintf("%d, %d\n", num, rtt))
}
// Log out.
err = plutoC.Send(false, "appendZ 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)
}
// Calculate statistics and print them.
var sum int64 = 0
for _, result := range results {
sum += result
}
msAvg := (float64(sum) / float64(runs)) / float64(time.Millisecond)
log.Printf("Done on pluto, sent %d messages, each took %f ms on average.\n\n", runs, msAvg)
// Run tests on Dovecot.
log.Printf("Connecting to Dovecot...\n")
// 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("appendA LOGIN %s %s", config.Dovecot.AppendTest.Name, config.Dovecot.AppendTest.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.AppendTest.Name, err.Error())
}
if strings.HasPrefix(answer, "appendA OK") != true {
log.Fatalf("Server responded unexpectedly to LOGIN: %s\n", answer)
}
log.Printf("Logged in as '%s'.\n", config.Dovecot.AppendTest.Name)
// Prepare log file name for Dovecot.
dovecotLogFileName := fmt.Sprintf("results/dovecot-append-%s.log", logFileTime.Format("2006-01-02-15-04-05"))
// 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: APPEND\nPlatform: Dovecot\nDate: %s\n-----\n", logFileTime.Format("2006-01-02-15-04-05")))
log.Printf("Running tests on Dovecot...\n")
// Reset results slice.
results = make([]int64, runs)
for num := 1; num <= runs; num++ {
i := num - 1
// Prepare command to send.
command := fmt.Sprintf("append%d APPEND INBOX {%d}", num, appendMsgSize)
// Take current time stamp.
timeStart := time.Now().UnixNano()
// Send APPEND commmand to server.
err := dovecotC.Send(false, command)
if err != nil {
log.Fatalf("%d: Failed during sending APPEND command: %s\n", num, err.Error())
}
// Receive answer to APPEND request.
answer, err := dovecotC.Receive(false)
if err != nil {
log.Fatalf("%d: Error receiving response to APPEND: %s\n", num, err.Error())
}
if answer != "+ OK" {
log.Fatalf("%d: Did not receive continuation command from server: %s\n", num, answer)
}
// Send mail message.
_, err = fmt.Fprintf(dovecotC.OutConn, "%s\n", appendMsg)
if err != nil {
log.Fatalf("%d: Sending mail message to server failed with: %s\n", num, err.Error())
}
// Receive answer to message transfer.
answer, err = dovecotC.Receive(false)
if err != nil {
log.Fatalf("%d: Error during receiving response to APPEND: %s\n", num, err.Error())
}
// Take time stamp after function execution.
timeEnd := time.Now().UnixNano()
if strings.Contains(answer, "Append completed") != true {
log.Fatalf("%d: Server responded unexpectedly to APPEND command: %s\n", num, answer)
}
// Calculate round-trip time.
rtt := timeEnd - timeStart
// Store result in buffer.
results[i] = rtt
// Append log line to file.
dovecotLogFile.WriteString(fmt.Sprintf("%d, %d\n", num, rtt))
}
// Log out.
err = dovecotC.Send(false, "appendZ 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)
}
// Calculate statistics and print them.
sum = 0
for _, result := range results {
sum += result
}
msAvg = 0
msAvg = (float64(sum) / float64(runs)) / float64(time.Millisecond)
log.Printf("Done on Dovecot, sent %d messages, each took %f ms on average.", runs, msAvg)
}