156
class FTPPerformanceTest(object):
157
"""Provides file transfer rate based information while
158
using the FTP protocol and sending a file (DEFAULT=1GB)
159
over the local or public network using a specified network
160
interface on the host."""
169
file2send="ftp_performance_test"):
172
self.username = username
173
self.password = password
174
self.iface = Interface(interface)
175
self.binary_size = binary_size
176
self.file2send = file2send
178
def _make_file2send(self):
180
Makes binary file to send over FTP.
181
Size defaults to 1GB if not supplied.
184
logging.debug("Creating %sGB file", self.binary_size)
186
file_size = (1024 * 1024 * 1024) * self.binary_size
187
with open(self.file2send, "wb") as out:
188
out.seek((file_size) - 1)
189
out.write('\0'.encode())
191
def send_file(self, filename=None):
193
Sends file over the network using FTP and returns the
194
amount of bytes sent and delay between send and completed.
198
file = open(self.file2send, 'rb')
199
filename = self.file2send
201
send_time = time.time()
204
logging.debug("Sending file")
205
self.remote.storbinary("STOR " + filename, file, 1024)
206
except (ftplib.all_errors) as send_failure:
207
logging.error("Failed to send file to %s", self.target)
208
logging.error("Reason: %s", send_failure)
213
time_lapse = time.time() - send_time
214
bytes_sent = os.stat(filename).st_size
216
return bytes_sent, time_lapse
218
def close_connection(self):
220
Close connection to remote FTP target
226
Connects to FTP target and set the current directory as /
229
logging.debug("Connecting to %s", self.target)
231
self.remote = FTP(self.target)
232
self.remote.set_debuglevel(2)
233
self.remote.set_pasv(True)
234
except socket.error as connect_exception:
235
logging.error("Failed to connect to: %s: %s", self.target,
239
logging.debug("Logging in")
240
logging.debug("{USER:%s, PASS:%s}", self.username, self.password)
243
self.remote.login(self.username, self.password)
244
except ftplib.error_perm as login_exception:
245
logging.error("failed to log into target: %s: %s", self.target,
250
self.remote.cwd(default_out_dir)
256
"Interface": self.iface.interface,
257
"HWAddress": self.iface.macaddress,
258
"Duplex": self.iface.duplex_mode,
259
"Speed": self.iface.max_speed,
260
"Status": self.iface.status
265
if not os.path.isfile(self.file2send):
266
self._make_file2send()
268
# Connect to FTP target and send file
269
connected = self.connect()
271
if connected is False:
274
datasize, delay = self.send_file()
276
# Remove created binary
278
os.remove(self.file2send)
279
except (IOError, OSError) as file_delete_error:
280
logging.error("Could not remove previous ftp file")
281
logging.error(file_delete_error)
283
if connected and datasize > 0:
285
logging.debug("Bytes sent (%s): %.2f seconds", datasize, delay)
287
# Calculate transfer rate and determine pass/fail status
288
mbs_speed = float(datasize / 131072) / float(delay)
289
percent = (mbs_speed / int(info["Speed"])) * 100
290
print("Transfer speed:")
291
print("%3.2f%% of" % percent)
292
print("theoretical max %smbs" % int(info["Speed"]))
295
logging.warn("Poor network performance detected")
298
logging.debug("Passed benchmark")
300
print("Failed sending file via ftp")
304
155
class StressPerformanceTest:
306
157
def __init__(self, interface, target):
558
387
logging.error("Can't ping test server on %s", args.interface)
561
# Execute FTP transfer benchmarking test
562
if args.test_type.lower() == "ftp":
563
ftp_benchmark = FTPPerformanceTest(
564
test_target, test_user, test_pass, args.interface)
567
ftp_benchmark.binary_size = int(args.datasize)
568
result = ftp_benchmark.run()
570
elif args.test_type.lower() == "iperf":
390
# Execute requested networking test
391
if args.test_type.lower() == "iperf":
571
392
iperf_benchmark = IPerfPerformanceTest(args.interface, test_target,
572
393
args.fail_threshold)
573
394
if args.datasize:
688
498
'-i', '--interface', type=str, required=True)
689
499
test_parser.add_argument(
690
500
'-t', '--test_type', type=str,
691
choices=("ftp", "iperf", "stress"), default="ftp",
692
help=("[FTP *Default*]"))
501
choices=("iperf", "stress"), default="iperf",
502
help=("[iperf *Default*]"))
693
503
test_parser.add_argument('--target', type=str)
694
504
test_parser.add_argument(
695
'--username', type=str, help=("For FTP test only"))
696
test_parser.add_argument(
697
'--password', type=str, help=("For FTP test only"))
698
test_parser.add_argument(
699
505
'--datasize', type=str,
701
507
help=("Amount of data to send. For iperf tests this will direct "
702
"iperf to send DATASIZE GB of data. For FTP tests, this "
703
"will create a file that is DATASIZE GB large to be transmitted "
508
"iperf to send DATASIZE GB of data to the target."))
705
509
test_parser.add_argument(
706
510
'--config', type=str,
707
511
default="/etc/checkbox.d/network.cfg",