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
This commit is contained in:
Matt Martz 2013-08-08 09:52:54 -05:00
parent 4b06259a92
commit cbb249b96d
2 changed files with 10 additions and 14 deletions

View File

@ -4,7 +4,7 @@ import setuptools
setuptools.setup( setuptools.setup(
name='speedtest-cli', name='speedtest-cli',
version='0.2', version='0.2.1',
description=('Command line interface for testing internet bandwidth using ' description=('Command line interface for testing internet bandwidth using '
'speedtest.net'), 'speedtest.net'),
author='Matt Martz', author='Matt Martz',

View File

@ -25,7 +25,6 @@ import time
import os import os
import sys import sys
import threading import threading
import binascii
import re import re
from xml.dom import minidom as DOM from xml.dom import minidom as DOM
@ -143,21 +142,17 @@ class FileGetter(threading.Thread):
return self.result return self.result
def run(self): def run(self):
self.result = [0]
try: try:
if (time.time() - self.starttime) <= 10: if (time.time() - self.starttime) <= 10:
f = urlopen(self.url) f = urlopen(self.url)
self.result = 0
while 1: while 1:
contents = f.read(10240) self.result.append(len(f.read(10240)))
if contents: if self.result[-1] == 0:
self.result += len(contents)
else:
break break
f.close() f.close()
else:
self.result = 0
except IOError: except IOError:
self.result = 0 pass
def downloadSpeed(files, quiet=False): def downloadSpeed(files, quiet=False):
@ -178,8 +173,8 @@ def downloadSpeed(files, quiet=False):
while len(finished) < total_files: while len(finished) < total_files:
thread = q.get(True) thread = q.get(True)
thread.join() thread.join()
finished.append(thread.result) finished.append(sum(thread.result))
thread.result = 0 del thread
q = Queue(6) q = Queue(6)
start = time.time() start = time.time()
@ -195,7 +190,8 @@ def downloadSpeed(files, quiet=False):
class FilePutter(threading.Thread): class FilePutter(threading.Thread):
def __init__(self, url, start, size): def __init__(self, url, start, size):
self.url = url 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() self.data = ('content1=%s' % data[0:int(size)-9]).encode()
del data del data
self.result = None self.result = None
@ -237,7 +233,7 @@ def uploadSpeed(url, sizes, quiet=False):
thread = q.get(True) thread = q.get(True)
thread.join() thread.join()
finished.append(thread.result) finished.append(thread.result)
thread.result = 0 del thread
q = Queue(6) q = Queue(6)
start = time.time() start = time.time()