summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorNir Soffer <nirsof@gmail.com>2017-11-06 21:16:37 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2017-11-06 21:16:37 (GMT)
commitad455cd9243319b896c86074ffeb3bf78a82f4ec (patch)
tree48a4989023b45958c9fa7dfe090936506998906d /Lib/test
parent30f4fa456ef626ad7a92759f492ec7a268f7af4e (diff)
downloadcpython-ad455cd9243319b896c86074ffeb3bf78a82f4ec.zip
cpython-ad455cd9243319b896c86074ffeb3bf78a82f4ec.tar.gz
cpython-ad455cd9243319b896c86074ffeb3bf78a82f4ec.tar.bz2
bpo-31945: Configurable blocksize in HTTP(S)Connection (#4279)
blocksize was hardcoded to 8192, preventing efficient upload when using file-like body. Add blocksize argument to __init__, so users can configure the blocksize to fit their needs. I tested this uploading data from /dev/zero to a web server dropping the received data, to test the overhead of the HTTPConnection.send() with a file-like object. Here is an example 10g upload with the default buffer size (8192): $ time ~/src/cpython/release/python upload-httplib.py 10 https://localhost:8000/ Uploaded 10.00g in 17.53 seconds (584.00m/s) real 0m17.574s user 0m8.887s sys 0m5.971s Same with 512k blocksize: $ time ~/src/cpython/release/python upload-httplib.py 10 https://localhost:8000/ Uploaded 10.00g in 6.60 seconds (1551.15m/s) real 0m6.641s user 0m3.426s sys 0m2.162s In real world usage the difference will be smaller, depending on the local and remote storage and the network. See https://github.com/nirs/http-bench for more info.
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_httplib.py23
1 files changed, 23 insertions, 0 deletions
diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py
index 5591f1d..0d79cae 100644
--- a/Lib/test/test_httplib.py
+++ b/Lib/test/test_httplib.py
@@ -756,6 +756,29 @@ class BasicTest(TestCase):
conn.request('GET', '/foo', body(), {'Content-Length': '11'})
self.assertEqual(sock.data, expected)
+ def test_blocksize_request(self):
+ """Check that request() respects the configured block size."""
+ blocksize = 8 # For easy debugging.
+ conn = client.HTTPConnection('example.com', blocksize=blocksize)
+ sock = FakeSocket(None)
+ conn.sock = sock
+ expected = b"a" * blocksize + b"b"
+ conn.request("PUT", "/", io.BytesIO(expected), {"Content-Length": "9"})
+ self.assertEqual(sock.sendall_calls, 3)
+ body = sock.data.split(b"\r\n\r\n", 1)[1]
+ self.assertEqual(body, expected)
+
+ def test_blocksize_send(self):
+ """Check that send() respects the configured block size."""
+ blocksize = 8 # For easy debugging.
+ conn = client.HTTPConnection('example.com', blocksize=blocksize)
+ sock = FakeSocket(None)
+ conn.sock = sock
+ expected = b"a" * blocksize + b"b"
+ conn.send(io.BytesIO(expected))
+ self.assertEqual(sock.sendall_calls, 2)
+ self.assertEqual(sock.data, expected)
+
def test_send_type_error(self):
# See: Issue #12676
conn = client.HTTPConnection('example.com')