Ticket #33865: bench_limitedstream.py

File bench_limitedstream.py, 1.8 KB (added by Nick Pope, 2 years ago)
Line 
1from itertools import repeat
2from io import BytesIO
3
4import pyperf
5from django.conf import global_settings as settings
6from django.core.handlers.wsgi import LimitedStream
7
8
9def bench_limitedstream_read(stream, size):
10 while stream.read(size):
11 pass
12
13
14def bench_limitedstream_readline(stream, size):
15 while stream.readline(size):
16 pass
17
18
19def 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
28runner = pyperf.Runner()
29
30runner.bench_func(
31 "LimitedStream.read() (single line)",
32 bench_limitedstream_read,
33 prepare_stream(lines=1),
34 None,
35)
36runner.bench_func(
37 "LimitedStream.readline() (single line)",
38 bench_limitedstream_readline,
39 prepare_stream(lines=1),
40 None,
41)
42runner.bench_func(
43 "LimitedStream.read(8192) (single line)",
44 bench_limitedstream_read,
45 prepare_stream(lines=1),
46 8192,
47)
48runner.bench_func(
49 "LimitedStream.readline(8192) (single line)",
50 bench_limitedstream_readline,
51 prepare_stream(lines=1),
52 8192,
53)
54runner.bench_func(
55 "LimitedStream.read() (multiple lines)",
56 bench_limitedstream_read,
57 prepare_stream(lines=20),
58 None,
59)
60runner.bench_func(
61 "LimitedStream.readline() (multiple lines)",
62 bench_limitedstream_readline,
63 prepare_stream(lines=20),
64 None,
65)
66runner.bench_func(
67 "LimitedStream.read(8192) (multiple lines)",
68 bench_limitedstream_read,
69 prepare_stream(lines=20),
70 8192,
71)
72runner.bench_func(
73 "LimitedStream.readline(8192) (multiple lines)",
74 bench_limitedstream_readline,
75 prepare_stream(lines=20),
76 8192,
77)
Back to Top