summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2012-01-04 00:05:49 +0000
committerGiampaolo Rodola <g.rodola@gmail.com>2012-01-04 00:05:49 +0000
commit160ffc5e4cce98df93fa780fbab98b412dff37e3 (patch)
tree39b9336113af9b013d718584ca107965b673e661
parent58cc0bcb443d8c6cd7c5b9c2f52da4241d21fa25 (diff)
downloadpysendfile-160ffc5e4cce98df93fa780fbab98b412dff37e3.tar.gz
make benchmark script work on python 3
-rw-r--r--test/benchmark.py35
-rw-r--r--test/test_sendfile.py1
2 files changed, 20 insertions, 16 deletions
diff --git a/test/benchmark.py b/test/benchmark.py
index 5376cde..681bbbe 100644
--- a/test/benchmark.py
+++ b/test/benchmark.py
@@ -15,8 +15,7 @@ speed: 1527.67 Mb/sec
cpu: 3.47 usec/pass
speed: 2882.97 Mb/sec
-
-Working with python 2.x only.
+Works with both python 2.X and 3.X.
"""
import socket
@@ -25,6 +24,7 @@ import errno
import timeit
import time
import atexit
+import sys
from multiprocessing import Process
from sendfile import sendfile
@@ -33,7 +33,7 @@ from sendfile import sendfile
HOST = "127.0.0.1"
PORT = 8022
BIGFILE = "$testfile1"
-BIGFILE_SIZE = 1024 * 1024 * 1024 # 1 GB
+BIGFILE_SIZE = 1024 * 1024 * 1024 # 1 GB 1073741824 # 1 GB
BUFFER_SIZE = 65536
@@ -48,6 +48,12 @@ def create_file(filename, size):
break
f.close()
+# python 3 compatibility layer
+def print_(s):
+ sys.stdout.write(s + "\n")
+ sys.stdout.flush()
+
+
class Client:
def __init__(self):
@@ -71,7 +77,6 @@ class Client:
bytes_recv += len(chunk)
return bytes_recv
-
def start_server(use_sendfile, keep_sending=False):
"""A simple test server which sends a file once a client connects.
use_sendfile decides whether using sendfile() or plain sendall()
@@ -103,7 +108,8 @@ def start_server(use_sendfile, keep_sending=False):
while 1:
try:
sent = sendfile(sockno, fileno, offset, BUFFER_SIZE)
- except OSError, err:
+ except OSError:
+ err = sys.exc_info()[1]
if err.errno in (errno.EAGAIN, errno.EBUSY):
continue
raise
@@ -123,9 +129,9 @@ def start_server(use_sendfile, keep_sending=False):
def main():
if not os.path.exists(BIGFILE) or os.path.getsize(BIGFILE) < BIGFILE_SIZE:
- print "creating big file . . ."
+ print_("creating big file . . .")
create_file(BIGFILE, BIGFILE_SIZE)
- print "starting benchmark . . .\n"
+ print_("starting benchmark . . .")
atexit.register(lambda: os.remove(BIGFILE))
# CPU time: use sendfile()
@@ -166,14 +172,13 @@ def main():
server.terminate()
server.join()
- print "=== send() ==="
- print "cpu: %.2f usec/pass" % (1000000 * t2 / 100000)
- print "speed: %s Mb/sec" % round(bytes2 / 1024.0 / 1024.0, 2)
- print
- print "=== sendfile() ==="
- print "cpu: %.2f usec/pass" % (1000000 * t1 / 100000)
- print "speed: %s Mb/sec" % round(bytes1 / 1024.0 / 1024.0, 2)
+ print_("=== send() ===")
+ print_("cpu: %.2f usec/pass" % (1000000 * t2 / 100000))
+ print_("speed: %s MB/sec" % round(bytes2 / 1024.0 / 1024.0, 2))
+ print_("")
+ print_("=== sendfile() ===")
+ print_("cpu: %.2f usec/pass" % (1000000 * t1 / 100000))
+ print_("speed: %s MB/sec" % round(bytes1 / 1024.0 / 1024.0, 2))
if __name__ == '__main__':
main()
-
diff --git a/test/test_sendfile.py b/test/test_sendfile.py
index b5299fa..56609a3 100644
--- a/test/test_sendfile.py
+++ b/test/test_sendfile.py
@@ -380,7 +380,6 @@ class TestSendfile(unittest.TestCase):
self.assertEqual(hash(data), hash(DATA))
-
class RepeatedTimer:
def __init__(self, timeout, fun):