| 1 | from itertools import repeat
|
|---|
| 2 | from io import BytesIO
|
|---|
| 3 |
|
|---|
| 4 | import pyperf
|
|---|
| 5 | from django.conf import global_settings as settings
|
|---|
| 6 | from django.core.handlers.wsgi import LimitedStream
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 | def bench_limitedstream_read(stream, size):
|
|---|
| 10 | while stream.read(size):
|
|---|
| 11 | pass
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 | def bench_limitedstream_readline(stream, size):
|
|---|
| 15 | while stream.readline(size):
|
|---|
| 16 | pass
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 | def prepare_stream(lines=1):
|
|---|
| 20 | part = b"a=1"
|
|---|
| 21 | length = settings.DATA_UPLOAD_MAX_MEMORY_SIZE
|
|---|
| 22 | chunk_size = length // lines // (1 + len(part))
|
|---|
| 23 | generator = (repeat(part, chunk_size) for _ in range(lines))
|
|---|
| 24 | data = b"\n".join(b"&".join(x) for x in generator)
|
|---|
| 25 | return LimitedStream(BytesIO(data), length)
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 | runner = pyperf.Runner()
|
|---|
| 29 |
|
|---|
| 30 | runner.bench_func(
|
|---|
| 31 | "LimitedStream.read() (single line)",
|
|---|
| 32 | bench_limitedstream_read,
|
|---|
| 33 | prepare_stream(lines=1),
|
|---|
| 34 | None,
|
|---|
| 35 | )
|
|---|
| 36 | runner.bench_func(
|
|---|
| 37 | "LimitedStream.readline() (single line)",
|
|---|
| 38 | bench_limitedstream_readline,
|
|---|
| 39 | prepare_stream(lines=1),
|
|---|
| 40 | None,
|
|---|
| 41 | )
|
|---|
| 42 | runner.bench_func(
|
|---|
| 43 | "LimitedStream.read(8192) (single line)",
|
|---|
| 44 | bench_limitedstream_read,
|
|---|
| 45 | prepare_stream(lines=1),
|
|---|
| 46 | 8192,
|
|---|
| 47 | )
|
|---|
| 48 | runner.bench_func(
|
|---|
| 49 | "LimitedStream.readline(8192) (single line)",
|
|---|
| 50 | bench_limitedstream_readline,
|
|---|
| 51 | prepare_stream(lines=1),
|
|---|
| 52 | 8192,
|
|---|
| 53 | )
|
|---|
| 54 | runner.bench_func(
|
|---|
| 55 | "LimitedStream.read() (multiple lines)",
|
|---|
| 56 | bench_limitedstream_read,
|
|---|
| 57 | prepare_stream(lines=20),
|
|---|
| 58 | None,
|
|---|
| 59 | )
|
|---|
| 60 | runner.bench_func(
|
|---|
| 61 | "LimitedStream.readline() (multiple lines)",
|
|---|
| 62 | bench_limitedstream_readline,
|
|---|
| 63 | prepare_stream(lines=20),
|
|---|
| 64 | None,
|
|---|
| 65 | )
|
|---|
| 66 | runner.bench_func(
|
|---|
| 67 | "LimitedStream.read(8192) (multiple lines)",
|
|---|
| 68 | bench_limitedstream_read,
|
|---|
| 69 | prepare_stream(lines=20),
|
|---|
| 70 | 8192,
|
|---|
| 71 | )
|
|---|
| 72 | runner.bench_func(
|
|---|
| 73 | "LimitedStream.readline(8192) (multiple lines)",
|
|---|
| 74 | bench_limitedstream_readline,
|
|---|
| 75 | prepare_stream(lines=20),
|
|---|
| 76 | 8192,
|
|---|
| 77 | )
|
|---|