-
Notifications
You must be signed in to change notification settings - Fork 1
/
test-delete-gmail.go
182 lines (137 loc) · 5.04 KB
/
test-delete-gmail.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
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
"strings"
"time"
"crypto/tls"
"github.com/numbleroot/pluto-evaluation/config"
"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 DELETE command on gmail...\n")
// Read configuration from file.
config, err := config.LoadConfig(*configFlag)
if err != nil {
log.Fatalf("Error loading config: %s\n", err.Error())
}
// Create connection string to connect to gmail.
gmailIMAPAddr := fmt.Sprintf("%s:%s", config.Gmail.Server, config.Gmail.Port)
log.Printf("Connecting to gmail...\n")
// Connect to remote gmail system.
gmailConn, err := tls.Dial("tcp", gmailIMAPAddr, nil)
if err != nil {
log.Fatalf("Was unable to connect to remote gmail server: %s\n", err.Error())
}
// Create a new connection struct based on it.
gmailC := &imap.Connection{
OutConn: gmailConn,
OutReader: bufio.NewReader(gmailConn),
}
// Consume mandatory IMAP greeting.
_, err = gmailC.Receive(false)
if err != nil {
log.Fatalf("Error during receiving initial server greeting: %s\n", err.Error())
}
// Log in as first user.
err = gmailC.Send(false, fmt.Sprintf("deleteA LOGIN %s %s", config.Gmail.DeleteTest.Name, config.Gmail.DeleteTest.Password))
if err != nil {
log.Fatalf("Sending LOGIN to server failed with: %s\n", err.Error())
}
// Wait for success message.
answer, err := gmailC.Receive(false)
if err != nil {
log.Fatalf("Error during LOGIN as user %s: %s\n", config.Gmail.DeleteTest.Name, err.Error())
}
// Receive next line from server.
nextAnswer, err := gmailC.Receive(false)
if err != nil {
log.Fatalf("Error receiving second part of LOGIN response: %s\n", err.Error())
}
answer = fmt.Sprintf("%s\r\n%s", answer, nextAnswer)
if strings.Contains(answer, "deleteA OK") != true {
log.Fatalf("Server responded unexpectedly to LOGIN: %s\n", answer)
}
log.Printf("Logged in as '%s'.\n", config.Gmail.DeleteTest.Name)
// Take current time stamp and create log file name.
logFileTime := time.Now()
gmailLogFileName := fmt.Sprintf("results/gmail-delete-%s.log", logFileTime.Format("2006-01-02-15-04-05"))
// Attempt to create a test log file containing
// measured test times for gmail system.
gmailLogFile, err := os.Create(gmailLogFileName)
if err != nil {
log.Fatalf("Failed to create test log file '%s': %s\n", gmailLogFileName, err.Error())
}
// Sync to storage and close on any exit.
defer gmailLogFile.Close()
defer gmailLogFile.Sync()
// Prepend file with meta information about this test.
gmailLogFile.WriteString(fmt.Sprintf("Subject: DELETE\nPlatform: gmail\nDate: %s\n-----\n", logFileTime.Format("2006-01-02-15-04-05")))
// Prepare buffer to append individual results to.
results := make([]int64, runs)
log.Printf("Running tests on gmail...\n")
for num := 1; num <= runs; num++ {
i := num - 1
// Prepare command to send.
command := fmt.Sprintf("delete%d DELETE evaluation-mailbox-%d", num, num)
// Take current time stamp.
timeStart := time.Now().UnixNano()
// Send DELETE commmand to server.
err := gmailC.Send(false, command)
if err != nil {
log.Fatalf("%d: Failed during sending DELETE command: %s\n", num, err.Error())
}
// Receive answer to DELETE request.
answer, err := gmailC.Receive(false)
if err != nil {
log.Fatalf("%d: Error receiving response to DELETE: %s\n", num, err.Error())
}
// Take time stamp after function execution.
timeEnd := time.Now().UnixNano()
if strings.Contains(answer, "Success") != true {
log.Fatalf("%d: Server responded unexpectedly to DELETE command: %s\n", num, answer)
}
// Calculate round-trip time.
rtt := timeEnd - timeStart
// Store result in buffer.
results[i] = rtt
// Append log line to file.
gmailLogFile.WriteString(fmt.Sprintf("%d, %d\n", num, rtt))
}
// Log out.
err = gmailC.Send(false, "deleteZ LOGOUT")
if err != nil {
log.Fatalf("Error during LOGOUT: %s\n", err.Error())
}
// Receive first part of answer.
answer, err = gmailC.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 = gmailC.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, "(Success)") != 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 gmail, sent %d messages, each took %f ms on average.\n\n", runs, msAvg)
}