Skip to content

Instantly share code, notes, and snippets.

@juga0
Created Apr 20, 2019
Embed
What would you like to do?
def chunk_duration(session, url, chunk_length=1048576):
"""Check when a request stabilizes."""
response = session.get(url, stream=True, verify=False)
# length = int(response.headers['Content-Length'])
durations = []
chunk_count = 1
chunks = response.iter_content(chunk_length)
start_time = time.monotonic()
# discard first chunk
next(chunks)
end_time = time.monotonic()
duration = end_time - start_time
durations.append(duration)
for content in chunks:
# response.read(chunk_length)
end_time = time.monotonic()
duration = end_time - start_time
# keeping just for all the play below
chunk_count += 1
delta_duration = abs(durations[-1] - duration)
start_time = end_time
durations.append(duration)
# Less than 5 milliseconds of duration differnce on downloading the
# same amount of bytes
if delta_duration < 0.008:
break
# take 1 more chunk
end_time = time.monotonic()
duration = end_time - start_time
# keeping to check how much durations changed
durations.append(duration)
chunk_count += 1
# and return the median of the last 3
bytes = chunk_length * 3
duration = sum(durations[-3:])
return (durations, bytes, duration)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment