Debian Codegraph @

docs/core/benchmarks/linereceiver.py

raw
1 import time
2
3 from twisted.protocols import basic
4 from twisted.python.compat import range
5
6
7 class CollectingLineReceiver(basic.LineReceiver):
8 def __init__(self):
9 self.lines = []
10 self.lineReceived = self.lines.append
11
12
13 def deliver(proto, chunks):
14 return [proto.dataReceived(chunk) for chunk in chunks]
15
16
17 def benchmark(chunkSize, lineLength, numLines):
18 bytes = (b"x" * lineLength + b"\r\n") * numLines
19 chunkCount = len(bytes) // chunkSize + 1
20 chunks = []
21 for n in range(chunkCount):
22 chunks.append(bytes[n * chunkSize : (n + 1) * chunkSize])
23 assert b"".join(chunks) == bytes, (chunks, bytes)
24 p = CollectingLineReceiver()
25
26 before = time.clock()
27 deliver(p, chunks)
28 after = time.clock()
29
30 assert bytes.splitlines() == p.lines, (bytes.splitlines(), p.lines)
31
32 print("chunkSize:", chunkSize, end=" ")
33 print("lineLength:", lineLength, end=" ")
34 print("numLines:", numLines, end=" ")
35 print("CPU Time: ", after - before)
36
37
38 def main():
39 for numLines in 100, 1000:
40 for lineLength in (10, 100, 1000):
41 for chunkSize in (1, 500, 5000):
42 benchmark(chunkSize, lineLength, numLines)
43
44 for numLines in 10000, 50000:
45 for lineLength in (1000, 2000):
46 for chunkSize in (51, 500, 5000):
47 benchmark(chunkSize, lineLength, numLines)
48
49
50 if __name__ == "__main__":
51 main()