Code formating correction (tabs) on queue manager
[fmail.git] / tools / benchsmtp.py
blob956f16830020e5ba52551a442bc939535c33f21b
1 #!/usr/bin/env python
3 import smtplib
4 import time
5 import threading
7 SMTP_HOST = 'localhost'
8 SMTP_PORT = 12000
9 MSG_SIZE = 256
10 MSG_COUNT = 100 #Messages to Send per Test
11 CONN_COUNT = 20 #Messages to send on Connection test, beware it could be slow
12 THREAD_COUNT = [2, 5, 10]
14 MSG_FROM = 'testmail@mymail.com'
15 MSG_TO = 'testuser@localhost'
16 MSG_DATA = str().zfill(MSG_SIZE)
18 def SingleSession():
19 print 'Benchmarking Single Session'
21 #Initialize Measuring Variables
22 total_time = 0.0
24 #Connect to Server
25 server = smtplib.SMTP(SMTP_HOST, SMTP_PORT)
27 #Send Messages
28 for i in xrange(MSG_COUNT):
29 time_start = time.time()
30 server.sendmail(MSG_FROM, MSG_TO, MSG_DATA)
31 time_end = time.time()
32 total_time += time_end - time_start
34 #Disconnect from Server
35 server.quit()
37 #Calculate Avg time per msg and Messages Per Second
38 msg_avg = total_time / MSG_COUNT
39 mps = 1.0 / msg_avg
41 print ' * Total time:', total_time
42 print ' * Avg. Time per message:', msg_avg
43 print ' * Messages Per second:', mps
44 print ' * Total Message Data Transmitted:', MSG_SIZE * MSG_COUNT, 'bytes'
45 print ' * Data Rate:', (MSG_SIZE * MSG_COUNT) / total_time, 'bytes/second'
47 def ConnectionBench():
48 print 'Benchmarking Connection Speed'
49 print ' - Connection Count:', CONN_COUNT
50 #Initialize Measuring Variables
51 total_time = 0.0
53 #Send Messages
54 for i in xrange(CONN_COUNT):
55 time_start = time.time()
56 #Connect to Server
57 server = smtplib.SMTP(SMTP_HOST, SMTP_PORT)
58 server.sendmail(MSG_FROM, MSG_TO, MSG_DATA)
59 server.quit()
60 time_end = time.time()
61 total_time += time_end - time_start
63 #Calculate Avg time per msg and Messages Per Second
64 msg_avg = total_time / CONN_COUNT
65 mps = 1.0 / msg_avg
67 print ' * Total time:', total_time
68 print ' * Avg. Time per message:', msg_avg
69 print ' * Messages Per second:', mps
70 print ' * Total Message Data Transmitted:', MSG_SIZE * CONN_COUNT, 'bytes'
71 print ' * Data Rate:', (MSG_SIZE * CONN_COUNT) / total_time, 'bytes/second'
73 class ThreadBench(threading.Thread):
74 def __init__(self, msgcount):
75 threading.Thread.__init__(self)
76 self.msgcount = msgcount
77 def run(self):
78 conn = smtplib.SMTP(SMTP_HOST, SMTP_PORT)
79 self.total_time = 0.0
80 for i in xrange(self.msgcount):
81 time_start = time.time()
82 conn.sendmail(MSG_FROM, MSG_TO, MSG_DATA);
83 time_end = time.time()
84 self.total_time += time_end - time_start
85 conn.quit()
86 self.msg_avg = self.total_time / self.msgcount
87 self.mps = 1.0 / self.msg_avg
89 def ThreadBenchmark(thread_count):
90 print 'Benchmarking Multiple Sessions (Threaded)'
91 print ' - Thread Count:', thread_count
93 threads = []
94 thread_msgcount = int(MSG_COUNT / thread_count)
96 print ' - Per Thread Message Count:', thread_msgcount
97 print ' - Total Message Count:', thread_msgcount * thread_count
99 for i in xrange(thread_count):
100 threads.append(ThreadBench(thread_msgcount))
102 time_start = time.time()
103 for t in threads:
104 t.start()
105 for t in threads:
106 t.join()
107 time_end = time.time()
109 total_time = time_end - time_start
111 accum_time = 0.0
112 avg_mps = 0.0
113 avg_msg = 0.0
114 for t in threads:
115 accum_time += t.total_time
116 avg_mps += t.mps
117 avg_msg += t.msg_avg
118 avg_mps = avg_mps / thread_count
119 avg_msg = avg_msg / thread_count
120 msg_time = (thread_msgcount * thread_count) / total_time
121 mps = 1.0 / msg_time
122 total_data = (thread_msgcount * thread_count) * MSG_SIZE
123 print ' * Total Time:', total_time
124 print ' * Accumulated Time:', accum_time
125 print ' * TT/AT Ratio:', total_time / accum_time
126 print ' * Time per Message:', msg_time
127 print ' * Messages per Second:', mps
128 print ' * Avg. Thread Time per Message:', avg_msg
129 print ' * Avg. Thread Message per Second:', avg_mps
130 print ' * Total Message Data Transmited:', total_data, 'bytes'
131 print ' * Data Rate:', total_data / total_time, 'bytes/second'
132 print ' * Per Thread Data:'
133 for i in xrange(thread_count):
134 t = threads[i]
135 print ' - Thread', i
136 print ' * Total Time:', t.total_time
137 print ' * Avg. Time per message:', t.msg_avg
138 print ' * Messages per Second:', t.mps
140 print 'FancyMail Simple SMTP Benchmark Suite'
141 print ' Params:'
142 print ' * SMTP Port:', SMTP_PORT
143 print ' * SMTP Host:', SMTP_HOST
144 print ' * Message Count:', MSG_COUNT
145 print ' * Message Size:', MSG_SIZE
147 print ''
149 SingleSession()
151 print ''
153 ConnectionBench()
155 print ''
157 for i in THREAD_COUNT:
158 ThreadBenchmark(i)
159 print ''