From cbb249b96de271c0b201284074e1aeb1c339ebc2 Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Thu, 8 Aug 2013 09:52:54 -0500 Subject: [PATCH] Don't use urandom for the upload data source Using urandom limits the upload speed due to the generation speed of random characters and causes high CPU usage. See #13 Fixes #26 --- setup.py | 2 +- speedtest_cli.py | 22 +++++++++------------- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/setup.py b/setup.py index c460143..af14034 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ import setuptools setuptools.setup( name='speedtest-cli', - version='0.2', + version='0.2.1', description=('Command line interface for testing internet bandwidth using ' 'speedtest.net'), author='Matt Martz', diff --git a/speedtest_cli.py b/speedtest_cli.py index e380603..84659a0 100755 --- a/speedtest_cli.py +++ b/speedtest_cli.py @@ -25,7 +25,6 @@ import time import os import sys import threading -import binascii import re from xml.dom import minidom as DOM @@ -143,21 +142,17 @@ class FileGetter(threading.Thread): return self.result def run(self): + self.result = [0] try: if (time.time() - self.starttime) <= 10: f = urlopen(self.url) - self.result = 0 while 1: - contents = f.read(10240) - if contents: - self.result += len(contents) - else: + self.result.append(len(f.read(10240))) + if self.result[-1] == 0: break f.close() - else: - self.result = 0 except IOError: - self.result = 0 + pass def downloadSpeed(files, quiet=False): @@ -178,8 +173,8 @@ def downloadSpeed(files, quiet=False): while len(finished) < total_files: thread = q.get(True) thread.join() - finished.append(thread.result) - thread.result = 0 + finished.append(sum(thread.result)) + del thread q = Queue(6) start = time.time() @@ -195,7 +190,8 @@ def downloadSpeed(files, quiet=False): class FilePutter(threading.Thread): def __init__(self, url, start, size): self.url = url - data = binascii.hexlify(os.urandom(int(size)-9)).decode() + chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' + data = chars * (int(round(int(size) / 36.0))) self.data = ('content1=%s' % data[0:int(size)-9]).encode() del data self.result = None @@ -237,7 +233,7 @@ def uploadSpeed(url, sizes, quiet=False): thread = q.get(True) thread.join() finished.append(thread.result) - thread.result = 0 + del thread q = Queue(6) start = time.time()