annotate Tools/ccbench/ccbench.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 0b457f149761
children 9712586fa42f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
58411
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2 # This file should be kept compatible with both Python 2.6 and Python >= 3.0.
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
3
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
4 from __future__ import division
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
5 from __future__ import print_function
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
6
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
7 """
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
8 ccbench, a Python concurrency benchmark.
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
9 """
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
10
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
11 import time
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
12 import os
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
13 import sys
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
14 import functools
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
15 import itertools
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
16 import threading
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
17 import subprocess
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
18 import socket
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
19 from optparse import OptionParser, SUPPRESS_HELP
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
20 import platform
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
21
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
22 # Compatibility
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
23 try:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
24 xrange
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
25 except NameError:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
26 xrange = range
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
27
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
28 try:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
29 map = itertools.imap
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
30 except AttributeError:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
31 pass
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
32
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
33
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
34 THROUGHPUT_DURATION = 2.0
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
35
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
36 LATENCY_PING_INTERVAL = 0.1
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
37 LATENCY_DURATION = 2.0
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
38
59598
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
39 BANDWIDTH_PACKET_SIZE = 1024
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
40 BANDWIDTH_DURATION = 2.0
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
41
58411
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
42
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
43 def task_pidigits():
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
44 """Pi calculation (Python)"""
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
45 _map = map
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
46 _count = itertools.count
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
47 _islice = itertools.islice
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
48
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
49 def calc_ndigits(n):
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
50 # From http://shootout.alioth.debian.org/
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
51 def gen_x():
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
52 return _map(lambda k: (k, 4*k + 2, 0, 2*k + 1), _count(1))
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
53
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
54 def compose(a, b):
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
55 aq, ar, as_, at = a
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
56 bq, br, bs, bt = b
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
57 return (aq * bq,
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
58 aq * br + ar * bt,
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
59 as_ * bq + at * bs,
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
60 as_ * br + at * bt)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
61
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
62 def extract(z, j):
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
63 q, r, s, t = z
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
64 return (q*j + r) // (s*j + t)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
65
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
66 def pi_digits():
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
67 z = (1, 0, 0, 1)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
68 x = gen_x()
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
69 while 1:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
70 y = extract(z, 3)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
71 while y != extract(z, 4):
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
72 z = compose(z, next(x))
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
73 y = extract(z, 3)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
74 z = compose((10, -10*y, 0, 1), z)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
75 yield y
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
76
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
77 return list(_islice(pi_digits(), n))
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
78
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
79 return calc_ndigits, (50, )
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
80
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
81 def task_regex():
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
82 """regular expression (C)"""
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
83 # XXX this task gives horrendous latency results.
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
84 import re
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
85 # Taken from the `inspect` module
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
86 pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)', re.MULTILINE)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
87 with open(__file__, "r") as f:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
88 arg = f.read(2000)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
89
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
90 def findall(s):
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
91 t = time.time()
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
92 try:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
93 return pat.findall(s)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
94 finally:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
95 print(time.time() - t)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
96 return pat.findall, (arg, )
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
97
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
98 def task_sort():
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
99 """list sorting (C)"""
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
100 def list_sort(l):
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
101 l = l[::-1]
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
102 l.sort()
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
103
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
104 return list_sort, (list(range(1000)), )
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
105
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
106 def task_compress_zlib():
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
107 """zlib compression (C)"""
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
108 import zlib
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
109 with open(__file__, "rb") as f:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
110 arg = f.read(5000) * 3
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
111
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
112 def compress(s):
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
113 zlib.decompress(zlib.compress(s, 5))
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
114 return compress, (arg, )
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
115
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
116 def task_compress_bz2():
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
117 """bz2 compression (C)"""
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
118 import bz2
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
119 with open(__file__, "rb") as f:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
120 arg = f.read(3000) * 2
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
121
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
122 def compress(s):
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
123 bz2.compress(s)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
124 return compress, (arg, )
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
125
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
126 def task_hashing():
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
127 """SHA1 hashing (C)"""
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
128 import hashlib
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
129 with open(__file__, "rb") as f:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
130 arg = f.read(5000) * 30
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
131
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
132 def compute(s):
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
133 hashlib.sha1(s).digest()
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
134 return compute, (arg, )
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
135
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
136
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
137 throughput_tasks = [task_pidigits, task_regex]
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
138 for mod in 'bz2', 'hashlib':
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
139 try:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
140 globals()[mod] = __import__(mod)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
141 except ImportError:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
142 globals()[mod] = None
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
143
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
144 # For whatever reasons, zlib gives irregular results, so we prefer bz2 or
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
145 # hashlib if available.
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
146 # (NOTE: hashlib releases the GIL from 2.7 and 3.1 onwards)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
147 if bz2 is not None:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
148 throughput_tasks.append(task_compress_bz2)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
149 elif hashlib is not None:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
150 throughput_tasks.append(task_hashing)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
151 else:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
152 throughput_tasks.append(task_compress_zlib)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
153
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
154 latency_tasks = throughput_tasks
59598
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
155 bandwidth_tasks = [task_pidigits]
58411
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
156
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
157
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
158 class TimedLoop:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
159 def __init__(self, func, args):
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
160 self.func = func
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
161 self.args = args
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
162
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
163 def __call__(self, start_time, min_duration, end_event, do_yield=False):
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
164 step = 20
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
165 niters = 0
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
166 duration = 0.0
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
167 _time = time.time
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
168 _sleep = time.sleep
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
169 _func = self.func
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
170 _args = self.args
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
171 t1 = start_time
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
172 while True:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
173 for i in range(step):
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
174 _func(*_args)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
175 t2 = _time()
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
176 # If another thread terminated, the current measurement is invalid
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
177 # => return the previous one.
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
178 if end_event:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
179 return niters, duration
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
180 niters += step
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
181 duration = t2 - start_time
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
182 if duration >= min_duration:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
183 end_event.append(None)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
184 return niters, duration
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
185 if t2 - t1 < 0.01:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
186 # Minimize interference of measurement on overall runtime
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
187 step = step * 3 // 2
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
188 elif do_yield:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
189 # OS scheduling of Python threads is sometimes so bad that we
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
190 # have to force thread switching ourselves, otherwise we get
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
191 # completely useless results.
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
192 _sleep(0.0001)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
193 t1 = t2
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
194
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
195
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
196 def run_throughput_test(func, args, nthreads):
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
197 assert nthreads >= 1
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
198
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
199 # Warm up
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
200 func(*args)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
201
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
202 results = []
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
203 loop = TimedLoop(func, args)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
204 end_event = []
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
205
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
206 if nthreads == 1:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
207 # Pure single-threaded performance, without any switching or
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
208 # synchronization overhead.
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
209 start_time = time.time()
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
210 results.append(loop(start_time, THROUGHPUT_DURATION,
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
211 end_event, do_yield=False))
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
212 return results
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
213
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
214 started = False
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
215 ready_cond = threading.Condition()
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
216 start_cond = threading.Condition()
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
217 ready = []
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
218
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
219 def run():
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
220 with ready_cond:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
221 ready.append(None)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
222 ready_cond.notify()
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
223 with start_cond:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
224 while not started:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
225 start_cond.wait()
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
226 results.append(loop(start_time, THROUGHPUT_DURATION,
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
227 end_event, do_yield=True))
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
228
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
229 threads = []
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
230 for i in range(nthreads):
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
231 threads.append(threading.Thread(target=run))
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
232 for t in threads:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
233 t.setDaemon(True)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
234 t.start()
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
235 # We don't want measurements to include thread startup overhead,
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
236 # so we arrange for timing to start after all threads are ready.
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
237 with ready_cond:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
238 while len(ready) < nthreads:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
239 ready_cond.wait()
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
240 with start_cond:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
241 start_time = time.time()
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
242 started = True
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
243 start_cond.notify(nthreads)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
244 for t in threads:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
245 t.join()
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
246
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
247 return results
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
248
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
249 def run_throughput_tests(max_threads):
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
250 for task in throughput_tasks:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
251 print(task.__doc__)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
252 print()
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
253 func, args = task()
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
254 nthreads = 1
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
255 baseline_speed = None
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
256 while nthreads <= max_threads:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
257 results = run_throughput_test(func, args, nthreads)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
258 # Taking the max duration rather than average gives pessimistic
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
259 # results rather than optimistic.
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
260 speed = sum(r[0] for r in results) / max(r[1] for r in results)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
261 print("threads=%d: %d" % (nthreads, speed), end="")
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
262 if baseline_speed is None:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
263 print(" iterations/s.")
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
264 baseline_speed = speed
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
265 else:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
266 print(" ( %d %%)" % (speed / baseline_speed * 100))
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
267 nthreads += 1
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
268 print()
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
269
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
270
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
271 LAT_END = "END"
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
272
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
273 def _sendto(sock, s, addr):
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
274 sock.sendto(s.encode('ascii'), addr)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
275
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
276 def _recv(sock, n):
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
277 return sock.recv(n).decode('ascii')
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
278
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
279 def latency_client(addr, nb_pings, interval):
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
280 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
281 _time = time.time
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
282 _sleep = time.sleep
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
283 def _ping():
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
284 _sendto(sock, "%r\n" % _time(), addr)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
285 # The first ping signals the parent process that we are ready.
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
286 _ping()
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
287 # We give the parent a bit of time to notice.
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
288 _sleep(1.0)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
289 for i in range(nb_pings):
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
290 _sleep(interval)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
291 _ping()
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
292 _sendto(sock, LAT_END + "\n", addr)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
293
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
294 def run_latency_client(**kwargs):
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
295 cmd_line = [sys.executable, '-E', os.path.abspath(__file__)]
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
296 cmd_line.extend(['--latclient', repr(kwargs)])
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
297 return subprocess.Popen(cmd_line) #, stdin=subprocess.PIPE,
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
298 #stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
299
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
300 def run_latency_test(func, args, nthreads):
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
301 # Create a listening socket to receive the pings. We use UDP which should
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
302 # be painlessly cross-platform.
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
303 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
304 sock.bind(("127.0.0.1", 0))
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
305 addr = sock.getsockname()
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
306
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
307 interval = LATENCY_PING_INTERVAL
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
308 duration = LATENCY_DURATION
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
309 nb_pings = int(duration / interval)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
310
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
311 results = []
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
312 threads = []
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
313 end_event = []
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
314 start_cond = threading.Condition()
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
315 started = False
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
316 if nthreads > 0:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
317 # Warm up
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
318 func(*args)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
319
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
320 results = []
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
321 loop = TimedLoop(func, args)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
322 ready = []
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
323 ready_cond = threading.Condition()
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
324
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
325 def run():
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
326 with ready_cond:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
327 ready.append(None)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
328 ready_cond.notify()
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
329 with start_cond:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
330 while not started:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
331 start_cond.wait()
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
332 loop(start_time, duration * 1.5, end_event, do_yield=False)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
333
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
334 for i in range(nthreads):
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
335 threads.append(threading.Thread(target=run))
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
336 for t in threads:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
337 t.setDaemon(True)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
338 t.start()
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
339 # Wait for threads to be ready
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
340 with ready_cond:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
341 while len(ready) < nthreads:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
342 ready_cond.wait()
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
343
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
344 # Run the client and wait for the first ping(s) to arrive before
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
345 # unblocking the background threads.
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
346 chunks = []
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
347 process = run_latency_client(addr=sock.getsockname(),
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
348 nb_pings=nb_pings, interval=interval)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
349 s = _recv(sock, 4096)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
350 _time = time.time
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
351
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
352 with start_cond:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
353 start_time = _time()
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
354 started = True
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
355 start_cond.notify(nthreads)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
356
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
357 while LAT_END not in s:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
358 s = _recv(sock, 4096)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
359 t = _time()
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
360 chunks.append((t, s))
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
361
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
362 # Tell the background threads to stop.
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
363 end_event.append(None)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
364 for t in threads:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
365 t.join()
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
366 process.wait()
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
367
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
368 for recv_time, chunk in chunks:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
369 # NOTE: it is assumed that a line sent by a client wasn't received
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
370 # in two chunks because the lines are very small.
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
371 for line in chunk.splitlines():
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
372 line = line.strip()
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
373 if line and line != LAT_END:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
374 send_time = eval(line)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
375 assert isinstance(send_time, float)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
376 results.append((send_time, recv_time))
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
377
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
378 return results
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
379
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
380 def run_latency_tests(max_threads):
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
381 for task in latency_tasks:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
382 print("Background CPU task:", task.__doc__)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
383 print()
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
384 func, args = task()
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
385 nthreads = 0
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
386 while nthreads <= max_threads:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
387 results = run_latency_test(func, args, nthreads)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
388 n = len(results)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
389 # We print out milliseconds
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
390 lats = [1000 * (t2 - t1) for (t1, t2) in results]
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
391 #print(list(map(int, lats)))
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
392 avg = sum(lats) / n
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
393 dev = (sum((x - avg) ** 2 for x in lats) / n) ** 0.5
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
394 print("CPU threads=%d: %d ms. (std dev: %d ms.)" % (nthreads, avg, dev), end="")
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
395 print()
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
396 #print(" [... from %d samples]" % n)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
397 nthreads += 1
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
398 print()
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
399
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
400
59598
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
401 BW_END = "END"
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
402
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
403 def bandwidth_client(addr, packet_size, duration):
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
404 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
405 sock.bind(("127.0.0.1", 0))
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
406 local_addr = sock.getsockname()
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
407 _time = time.time
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
408 _sleep = time.sleep
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
409 def _send_chunk(msg):
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
410 _sendto(sock, ("%r#%s\n" % (local_addr, msg)).rjust(packet_size), addr)
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
411 # We give the parent some time to be ready.
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
412 _sleep(1.0)
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
413 try:
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
414 start_time = _time()
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
415 end_time = start_time + duration * 2.0
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
416 i = 0
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
417 while _time() < end_time:
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
418 _send_chunk(str(i))
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
419 s = _recv(sock, packet_size)
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
420 assert len(s) == packet_size
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
421 i += 1
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
422 _send_chunk(BW_END)
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
423 finally:
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
424 sock.close()
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
425
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
426 def run_bandwidth_client(**kwargs):
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
427 cmd_line = [sys.executable, '-E', os.path.abspath(__file__)]
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
428 cmd_line.extend(['--bwclient', repr(kwargs)])
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
429 return subprocess.Popen(cmd_line) #, stdin=subprocess.PIPE,
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
430 #stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
431
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
432 def run_bandwidth_test(func, args, nthreads):
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
433 # Create a listening socket to receive the packets. We use UDP which should
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
434 # be painlessly cross-platform.
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
435 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
436 sock.bind(("127.0.0.1", 0))
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
437 addr = sock.getsockname()
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
438
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
439 duration = BANDWIDTH_DURATION
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
440 packet_size = BANDWIDTH_PACKET_SIZE
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
441
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
442 results = []
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
443 threads = []
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
444 end_event = []
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
445 start_cond = threading.Condition()
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
446 started = False
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
447 if nthreads > 0:
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
448 # Warm up
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
449 func(*args)
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
450
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
451 results = []
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
452 loop = TimedLoop(func, args)
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
453 ready = []
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
454 ready_cond = threading.Condition()
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
455
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
456 def run():
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
457 with ready_cond:
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
458 ready.append(None)
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
459 ready_cond.notify()
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
460 with start_cond:
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
461 while not started:
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
462 start_cond.wait()
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
463 loop(start_time, duration * 1.5, end_event, do_yield=False)
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
464
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
465 for i in range(nthreads):
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
466 threads.append(threading.Thread(target=run))
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
467 for t in threads:
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
468 t.setDaemon(True)
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
469 t.start()
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
470 # Wait for threads to be ready
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
471 with ready_cond:
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
472 while len(ready) < nthreads:
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
473 ready_cond.wait()
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
474
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
475 # Run the client and wait for the first packet to arrive before
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
476 # unblocking the background threads.
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
477 process = run_bandwidth_client(addr=addr,
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
478 packet_size=packet_size,
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
479 duration=duration)
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
480 _time = time.time
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
481 # This will also wait for the parent to be ready
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
482 s = _recv(sock, packet_size)
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
483 remote_addr = eval(s.partition('#')[0])
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
484
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
485 with start_cond:
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
486 start_time = _time()
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
487 started = True
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
488 start_cond.notify(nthreads)
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
489
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
490 n = 0
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
491 first_time = None
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
492 while not end_event and BW_END not in s:
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
493 _sendto(sock, s, remote_addr)
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
494 s = _recv(sock, packet_size)
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
495 if first_time is None:
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
496 first_time = _time()
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
497 n += 1
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
498 end_time = _time()
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
499
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
500 end_event.append(None)
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
501 for t in threads:
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
502 t.join()
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
503 process.kill()
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
504
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
505 return (n - 1) / (end_time - first_time)
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
506
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
507 def run_bandwidth_tests(max_threads):
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
508 for task in bandwidth_tasks:
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
509 print("Background CPU task:", task.__doc__)
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
510 print()
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
511 func, args = task()
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
512 nthreads = 0
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
513 baseline_speed = None
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
514 while nthreads <= max_threads:
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
515 results = run_bandwidth_test(func, args, nthreads)
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
516 speed = results
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
517 #speed = len(results) * 1.0 / results[-1][0]
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
518 print("CPU threads=%d: %.1f" % (nthreads, speed), end="")
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
519 if baseline_speed is None:
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
520 print(" packets/s.")
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
521 baseline_speed = speed
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
522 else:
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
523 print(" ( %d %%)" % (speed / baseline_speed * 100))
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
524 nthreads += 1
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
525 print()
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
526
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
527
58411
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
528 def main():
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
529 usage = "usage: %prog [-h|--help] [options]"
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
530 parser = OptionParser(usage=usage)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
531 parser.add_option("-t", "--throughput",
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
532 action="store_true", dest="throughput", default=False,
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
533 help="run throughput tests")
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
534 parser.add_option("-l", "--latency",
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
535 action="store_true", dest="latency", default=False,
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
536 help="run latency tests")
59598
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
537 parser.add_option("-b", "--bandwidth",
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
538 action="store_true", dest="bandwidth", default=False,
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
539 help="run I/O bandwidth tests")
58411
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
540 parser.add_option("-i", "--interval",
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
541 action="store", type="int", dest="check_interval", default=None,
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
542 help="sys.setcheckinterval() value")
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
543 parser.add_option("-I", "--switch-interval",
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
544 action="store", type="float", dest="switch_interval", default=None,
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
545 help="sys.setswitchinterval() value")
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
546 parser.add_option("-n", "--num-threads",
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
547 action="store", type="int", dest="nthreads", default=4,
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
548 help="max number of threads in tests")
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
549
59598
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
550 # Hidden option to run the pinging and bandwidth clients
58411
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
551 parser.add_option("", "--latclient",
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
552 action="store", dest="latclient", default=None,
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
553 help=SUPPRESS_HELP)
59598
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
554 parser.add_option("", "--bwclient",
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
555 action="store", dest="bwclient", default=None,
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
556 help=SUPPRESS_HELP)
58411
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
557
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
558 options, args = parser.parse_args()
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
559 if args:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
560 parser.error("unexpected arguments")
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
561
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
562 if options.latclient:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
563 kwargs = eval(options.latclient)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
564 latency_client(**kwargs)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
565 return
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
566
59598
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
567 if options.bwclient:
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
568 kwargs = eval(options.bwclient)
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
569 bandwidth_client(**kwargs)
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
570 return
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
571
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
572 if not options.throughput and not options.latency and not options.bandwidth:
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
573 options.throughput = options.latency = options.bandwidth = True
58411
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
574 if options.check_interval:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
575 sys.setcheckinterval(options.check_interval)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
576 if options.switch_interval:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
577 sys.setswitchinterval(options.switch_interval)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
578
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
579 print("== %s %s (%s) ==" % (
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
580 platform.python_implementation(),
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
581 platform.python_version(),
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
582 platform.python_build()[0],
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
583 ))
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
584 # Processor identification often has repeated spaces
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
585 cpu = ' '.join(platform.processor().split())
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
586 print("== %s %s on '%s' ==" % (
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
587 platform.machine(),
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
588 platform.system(),
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
589 cpu,
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
590 ))
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
591 print()
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
592
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
593 if options.throughput:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
594 print("--- Throughput ---")
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
595 print()
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
596 run_throughput_tests(options.nthreads)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
597
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
598 if options.latency:
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
599 print("--- Latency ---")
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
600 print()
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
601 run_latency_tests(options.nthreads)
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
602
59598
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
603 if options.bandwidth:
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
604 print("--- I/O bandwidth ---")
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
605 print()
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
606 run_bandwidth_tests(options.nthreads)
0b457f149761 Issue #7993: Add a test of IO packet processing bandwidth to ccbench.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58411
diff changeset
607
58411
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
608 if __name__ == "__main__":
86a35e59b6b1 Add ccbench to the Tools directory
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
609 main()