We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
iperf benchmark has race condition in saving process id
It's possible for iperf to save the wrong server id if two copies of the benchmark are running on the same machine.  Instead of using `pgrep -n`, use `$!` to get the process id.


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `perfkitbenchmarker/linux_benchmarks/iperf_benchmark.py`
Content:
```
1 # Copyright 2014 PerfKitBenchmarker Authors. All rights reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #   http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 
15 """Runs plain Iperf.
16 
17 Docs:
18 http://iperf.fr/
19 
20 Runs Iperf to collect network throughput.
21 """
22 
23 import logging
24 import re
25 
26 from perfkitbenchmarker import configs
27 from perfkitbenchmarker import flags
28 from perfkitbenchmarker import sample
29 from perfkitbenchmarker import vm_util
30 
31 flags.DEFINE_integer('iperf_sending_thread_count', 1,
32                      'Number of connections to make to the '
33                      'server for sending traffic.',
34                      lower_bound=1)
35 flags.DEFINE_integer('iperf_runtime_in_seconds', 60,
36                      'Number of seconds to run iperf.',
37                      lower_bound=1)
38 
39 FLAGS = flags.FLAGS
40 
41 BENCHMARK_NAME = 'iperf'
42 BENCHMARK_CONFIG = """
43 iperf:
44   description: Run iperf
45   vm_groups:
46     vm_1:
47       vm_spec: *default_single_core
48     vm_2:
49       vm_spec: *default_single_core
50 """
51 
52 IPERF_PORT = 20000
53 IPERF_RETRIES = 5
54 
55 
56 def GetConfig(user_config):
57   return configs.LoadConfig(BENCHMARK_CONFIG, user_config, BENCHMARK_NAME)
58 
59 
60 def Prepare(benchmark_spec):
61   """Install iperf and start the server on all machines.
62 
63   Args:
64     benchmark_spec: The benchmark specification. Contains all data that is
65         required to run the benchmark.
66   """
67   vms = benchmark_spec.vms
68   if len(vms) != 2:
69     raise ValueError(
70         'iperf benchmark requires exactly two machines, found {0}'.format(len(
71             vms)))
72 
73   for vm in vms:
74     vm.Install('iperf')
75     if vm_util.ShouldRunOnExternalIpAddress():
76       vm.AllowPort(IPERF_PORT)
77     vm.RemoteCommand('nohup iperf --server --port %s &> /dev/null &' %
78                      IPERF_PORT)
79     stdout, _ = vm.RemoteCommand('pgrep -n iperf')
80     # TODO store this in a better place once we have a better place
81     vm.iperf_server_pid = stdout.strip()
82 
83 
84 @vm_util.Retry(max_retries=IPERF_RETRIES)
85 def _RunIperf(sending_vm, receiving_vm, receiving_ip_address, ip_type):
86   """Run iperf using sending 'vm' to connect to 'ip_address'.
87 
88   Args:
89     sending_vm: The VM sending traffic.
90     receiving_vm: The VM receiving traffic.
91     receiving_ip_address: The IP address of the iperf server (ie the receiver).
92     ip_type: The IP type of 'ip_address' (e.g. 'internal', 'external')
93   Returns:
94     A Sample.
95   """
96   iperf_cmd = ('iperf --client %s --port %s --format m --time %s -P %s' %
97                (receiving_ip_address, IPERF_PORT,
98                 FLAGS.iperf_runtime_in_seconds,
99                 FLAGS.iperf_sending_thread_count))
100   # the additional time on top of the iperf runtime is to account for the
101   # time it takes for the iperf process to start and exit
102   timeout_buffer = 30 + FLAGS.iperf_sending_thread_count
103   stdout, _ = sending_vm.RemoteCommand(iperf_cmd, should_log=True,
104                                        timeout=FLAGS.iperf_runtime_in_seconds +
105                                        timeout_buffer)
106 
107   # Example output from iperf that needs to be parsed
108   # STDOUT: ------------------------------------------------------------
109   # Client connecting to 10.237.229.201, TCP port 5001
110   # TCP window size: 0.04 MByte (default)
111   # ------------------------------------------------------------
112   # [  6] local 10.76.234.115 port 53527 connected with 10.237.229.201 port 5001
113   # [  3] local 10.76.234.115 port 53524 connected with 10.237.229.201 port 5001
114   # [  4] local 10.76.234.115 port 53525 connected with 10.237.229.201 port 5001
115   # [  5] local 10.76.234.115 port 53526 connected with 10.237.229.201 port 5001
116   # [ ID] Interval       Transfer     Bandwidth
117   # [  4]  0.0-60.0 sec  3730 MBytes  521.1 Mbits/sec
118   # [  5]  0.0-60.0 sec  3499 MBytes   489 Mbits/sec
119   # [  6]  0.0-60.0 sec  3044 MBytes   425 Mbits/sec
120   # [  3]  0.0-60.0 sec  3738 MBytes   522 Mbits/sec
121   # [SUM]  0.0-60.0 sec  14010 MBytes  1957 Mbits/sec
122 
123   thread_values = re.findall(r'\[SUM].*\s+(\d+\.?\d*).Mbits/sec', stdout)
124   if not thread_values:
125     # If there is no sum you have try and figure out an estimate
126     # which happens when threads start at different times.  The code
127     # below will tend to overestimate a bit.
128     thread_values = re.findall('\[.*\d+\].*\s+(\d+\.?\d*).Mbits/sec', stdout)
129 
130     if len(thread_values) != FLAGS.iperf_sending_thread_count:
131       raise ValueError('Only %s out of %s iperf threads reported a'
132                        ' throughput value.' %
133                        (len(thread_values), FLAGS.iperf_sending_thread_count))
134 
135   total_throughput = 0.0
136   for value in thread_values:
137     total_throughput += float(value)
138 
139   metadata = {
140       # The meta data defining the environment
141       'receiving_machine_type': receiving_vm.machine_type,
142       'receiving_zone': receiving_vm.zone,
143       'sending_machine_type': sending_vm.machine_type,
144       'sending_thread_count': FLAGS.iperf_sending_thread_count,
145       'sending_zone': sending_vm.zone,
146       'runtime_in_seconds': FLAGS.iperf_runtime_in_seconds,
147       'ip_type': ip_type
148   }
149   return sample.Sample('Throughput', total_throughput, 'Mbits/sec', metadata)
150 
151 
152 def Run(benchmark_spec):
153   """Run iperf on the target vm.
154 
155   Args:
156     benchmark_spec: The benchmark specification. Contains all data that is
157         required to run the benchmark.
158 
159   Returns:
160     A list of sample.Sample objects.
161   """
162   vms = benchmark_spec.vms
163   results = []
164 
165   logging.info('Iperf Results:')
166 
167   # Send traffic in both directions
168   for sending_vm, receiving_vm in vms, reversed(vms):
169     # Send using external IP addresses
170     if vm_util.ShouldRunOnExternalIpAddress():
171       results.append(_RunIperf(sending_vm,
172                                receiving_vm,
173                                receiving_vm.ip_address,
174                                'external'))
175 
176     # Send using internal IP addresses
177     if vm_util.ShouldRunOnInternalIpAddress(sending_vm,
178                                             receiving_vm):
179       results.append(_RunIperf(sending_vm,
180                                receiving_vm,
181                                receiving_vm.internal_ip,
182                                'internal'))
183 
184   return results
185 
186 
187 def Cleanup(benchmark_spec):
188   """Cleanup iperf on the target vm (by uninstalling).
189 
190   Args:
191     benchmark_spec: The benchmark specification. Contains all data that is
192         required to run the benchmark.
193   """
194   vms = benchmark_spec.vms
195   for vm in vms:
196     vm.RemoteCommand('kill -9 ' + vm.iperf_server_pid)
197
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/perfkitbenchmarker/linux_benchmarks/iperf_benchmark.py b/perfkitbenchmarker/linux_benchmarks/iperf_benchmark.py
--- a/perfkitbenchmarker/linux_benchmarks/iperf_benchmark.py
+++ b/perfkitbenchmarker/linux_benchmarks/iperf_benchmark.py
@@ -74,9 +74,8 @@
     vm.Install('iperf')
     if vm_util.ShouldRunOnExternalIpAddress():
       vm.AllowPort(IPERF_PORT)
-    vm.RemoteCommand('nohup iperf --server --port %s &> /dev/null &' %
-                     IPERF_PORT)
-    stdout, _ = vm.RemoteCommand('pgrep -n iperf')
+    stdout, _ = vm.RemoteCommand(('nohup iperf --server --port %s &> /dev/null'
+                                  '& echo $!') % IPERF_PORT)
     # TODO store this in a better place once we have a better place
     vm.iperf_server_pid = stdout.strip()
 
@@ -193,4 +192,4 @@
   """
   vms = benchmark_spec.vms
   for vm in vms:
-    vm.RemoteCommand('kill -9 ' + vm.iperf_server_pid)
+    vm.RemoteCommand('kill -9 ' + vm.iperf_server_pid, ignore_failure=True)

{"golden_diff": "diff --git a/perfkitbenchmarker/linux_benchmarks/iperf_benchmark.py b/perfkitbenchmarker/linux_benchmarks/iperf_benchmark.py\n--- a/perfkitbenchmarker/linux_benchmarks/iperf_benchmark.py\n+++ b/perfkitbenchmarker/linux_benchmarks/iperf_benchmark.py\n@@ -74,9 +74,8 @@\n     vm.Install('iperf')\n     if vm_util.ShouldRunOnExternalIpAddress():\n       vm.AllowPort(IPERF_PORT)\n-    vm.RemoteCommand('nohup iperf --server --port %s &> /dev/null &' %\n-                     IPERF_PORT)\n-    stdout, _ = vm.RemoteCommand('pgrep -n iperf')\n+    stdout, _ = vm.RemoteCommand(('nohup iperf --server --port %s &> /dev/null'\n+                                  '& echo $!') % IPERF_PORT)\n     # TODO store this in a better place once we have a better place\n     vm.iperf_server_pid = stdout.strip()\n \n@@ -193,4 +192,4 @@\n   \"\"\"\n   vms = benchmark_spec.vms\n   for vm in vms:\n-    vm.RemoteCommand('kill -9 ' + vm.iperf_server_pid)\n+    vm.RemoteCommand('kill -9 ' + vm.iperf_server_pid, ignore_failure=True)\n", "issue": "iperf benchmark has race condition in saving process id\nIt's possible for iperf to save the wrong server id if two copies of the benchmark are running on the same machine.  Instead of using `pgrep -n`, use `$!` to get the process id.\n\n", "before_files": [{"content": "# Copyright 2014 PerfKitBenchmarker Authors. All rights reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#   http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\n\"\"\"Runs plain Iperf.\n\nDocs:\nhttp://iperf.fr/\n\nRuns Iperf to collect network throughput.\n\"\"\"\n\nimport logging\nimport re\n\nfrom perfkitbenchmarker import configs\nfrom perfkitbenchmarker import flags\nfrom perfkitbenchmarker import sample\nfrom perfkitbenchmarker import vm_util\n\nflags.DEFINE_integer('iperf_sending_thread_count', 1,\n                     'Number of connections to make to the '\n                     'server for sending traffic.',\n                     lower_bound=1)\nflags.DEFINE_integer('iperf_runtime_in_seconds', 60,\n                     'Number of seconds to run iperf.',\n                     lower_bound=1)\n\nFLAGS = flags.FLAGS\n\nBENCHMARK_NAME = 'iperf'\nBENCHMARK_CONFIG = \"\"\"\niperf:\n  description: Run iperf\n  vm_groups:\n    vm_1:\n      vm_spec: *default_single_core\n    vm_2:\n      vm_spec: *default_single_core\n\"\"\"\n\nIPERF_PORT = 20000\nIPERF_RETRIES = 5\n\n\ndef GetConfig(user_config):\n  return configs.LoadConfig(BENCHMARK_CONFIG, user_config, BENCHMARK_NAME)\n\n\ndef Prepare(benchmark_spec):\n  \"\"\"Install iperf and start the server on all machines.\n\n  Args:\n    benchmark_spec: The benchmark specification. Contains all data that is\n        required to run the benchmark.\n  \"\"\"\n  vms = benchmark_spec.vms\n  if len(vms) != 2:\n    raise ValueError(\n        'iperf benchmark requires exactly two machines, found {0}'.format(len(\n            vms)))\n\n  for vm in vms:\n    vm.Install('iperf')\n    if vm_util.ShouldRunOnExternalIpAddress():\n      vm.AllowPort(IPERF_PORT)\n    vm.RemoteCommand('nohup iperf --server --port %s &> /dev/null &' %\n                     IPERF_PORT)\n    stdout, _ = vm.RemoteCommand('pgrep -n iperf')\n    # TODO store this in a better place once we have a better place\n    vm.iperf_server_pid = stdout.strip()\n\n\n@vm_util.Retry(max_retries=IPERF_RETRIES)\ndef _RunIperf(sending_vm, receiving_vm, receiving_ip_address, ip_type):\n  \"\"\"Run iperf using sending 'vm' to connect to 'ip_address'.\n\n  Args:\n    sending_vm: The VM sending traffic.\n    receiving_vm: The VM receiving traffic.\n    receiving_ip_address: The IP address of the iperf server (ie the receiver).\n    ip_type: The IP type of 'ip_address' (e.g. 'internal', 'external')\n  Returns:\n    A Sample.\n  \"\"\"\n  iperf_cmd = ('iperf --client %s --port %s --format m --time %s -P %s' %\n               (receiving_ip_address, IPERF_PORT,\n                FLAGS.iperf_runtime_in_seconds,\n                FLAGS.iperf_sending_thread_count))\n  # the additional time on top of the iperf runtime is to account for the\n  # time it takes for the iperf process to start and exit\n  timeout_buffer = 30 + FLAGS.iperf_sending_thread_count\n  stdout, _ = sending_vm.RemoteCommand(iperf_cmd, should_log=True,\n                                       timeout=FLAGS.iperf_runtime_in_seconds +\n                                       timeout_buffer)\n\n  # Example output from iperf that needs to be parsed\n  # STDOUT: ------------------------------------------------------------\n  # Client connecting to 10.237.229.201, TCP port 5001\n  # TCP window size: 0.04 MByte (default)\n  # ------------------------------------------------------------\n  # [  6] local 10.76.234.115 port 53527 connected with 10.237.229.201 port 5001\n  # [  3] local 10.76.234.115 port 53524 connected with 10.237.229.201 port 5001\n  # [  4] local 10.76.234.115 port 53525 connected with 10.237.229.201 port 5001\n  # [  5] local 10.76.234.115 port 53526 connected with 10.237.229.201 port 5001\n  # [ ID] Interval       Transfer     Bandwidth\n  # [  4]  0.0-60.0 sec  3730 MBytes  521.1 Mbits/sec\n  # [  5]  0.0-60.0 sec  3499 MBytes   489 Mbits/sec\n  # [  6]  0.0-60.0 sec  3044 MBytes   425 Mbits/sec\n  # [  3]  0.0-60.0 sec  3738 MBytes   522 Mbits/sec\n  # [SUM]  0.0-60.0 sec  14010 MBytes  1957 Mbits/sec\n\n  thread_values = re.findall(r'\\[SUM].*\\s+(\\d+\\.?\\d*).Mbits/sec', stdout)\n  if not thread_values:\n    # If there is no sum you have try and figure out an estimate\n    # which happens when threads start at different times.  The code\n    # below will tend to overestimate a bit.\n    thread_values = re.findall('\\[.*\\d+\\].*\\s+(\\d+\\.?\\d*).Mbits/sec', stdout)\n\n    if len(thread_values) != FLAGS.iperf_sending_thread_count:\n      raise ValueError('Only %s out of %s iperf threads reported a'\n                       ' throughput value.' %\n                       (len(thread_values), FLAGS.iperf_sending_thread_count))\n\n  total_throughput = 0.0\n  for value in thread_values:\n    total_throughput += float(value)\n\n  metadata = {\n      # The meta data defining the environment\n      'receiving_machine_type': receiving_vm.machine_type,\n      'receiving_zone': receiving_vm.zone,\n      'sending_machine_type': sending_vm.machine_type,\n      'sending_thread_count': FLAGS.iperf_sending_thread_count,\n      'sending_zone': sending_vm.zone,\n      'runtime_in_seconds': FLAGS.iperf_runtime_in_seconds,\n      'ip_type': ip_type\n  }\n  return sample.Sample('Throughput', total_throughput, 'Mbits/sec', metadata)\n\n\ndef Run(benchmark_spec):\n  \"\"\"Run iperf on the target vm.\n\n  Args:\n    benchmark_spec: The benchmark specification. Contains all data that is\n        required to run the benchmark.\n\n  Returns:\n    A list of sample.Sample objects.\n  \"\"\"\n  vms = benchmark_spec.vms\n  results = []\n\n  logging.info('Iperf Results:')\n\n  # Send traffic in both directions\n  for sending_vm, receiving_vm in vms, reversed(vms):\n    # Send using external IP addresses\n    if vm_util.ShouldRunOnExternalIpAddress():\n      results.append(_RunIperf(sending_vm,\n                               receiving_vm,\n                               receiving_vm.ip_address,\n                               'external'))\n\n    # Send using internal IP addresses\n    if vm_util.ShouldRunOnInternalIpAddress(sending_vm,\n                                            receiving_vm):\n      results.append(_RunIperf(sending_vm,\n                               receiving_vm,\n                               receiving_vm.internal_ip,\n                               'internal'))\n\n  return results\n\n\ndef Cleanup(benchmark_spec):\n  \"\"\"Cleanup iperf on the target vm (by uninstalling).\n\n  Args:\n    benchmark_spec: The benchmark specification. Contains all data that is\n        required to run the benchmark.\n  \"\"\"\n  vms = benchmark_spec.vms\n  for vm in vms:\n    vm.RemoteCommand('kill -9 ' + vm.iperf_server_pid)\n", "path": "perfkitbenchmarker/linux_benchmarks/iperf_benchmark.py"}], "after_files": [{"content": "# Copyright 2014 PerfKitBenchmarker Authors. All rights reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#   http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\n\"\"\"Runs plain Iperf.\n\nDocs:\nhttp://iperf.fr/\n\nRuns Iperf to collect network throughput.\n\"\"\"\n\nimport logging\nimport re\n\nfrom perfkitbenchmarker import configs\nfrom perfkitbenchmarker import flags\nfrom perfkitbenchmarker import sample\nfrom perfkitbenchmarker import vm_util\n\nflags.DEFINE_integer('iperf_sending_thread_count', 1,\n                     'Number of connections to make to the '\n                     'server for sending traffic.',\n                     lower_bound=1)\nflags.DEFINE_integer('iperf_runtime_in_seconds', 60,\n                     'Number of seconds to run iperf.',\n                     lower_bound=1)\n\nFLAGS = flags.FLAGS\n\nBENCHMARK_NAME = 'iperf'\nBENCHMARK_CONFIG = \"\"\"\niperf:\n  description: Run iperf\n  vm_groups:\n    vm_1:\n      vm_spec: *default_single_core\n    vm_2:\n      vm_spec: *default_single_core\n\"\"\"\n\nIPERF_PORT = 20000\nIPERF_RETRIES = 5\n\n\ndef GetConfig(user_config):\n  return configs.LoadConfig(BENCHMARK_CONFIG, user_config, BENCHMARK_NAME)\n\n\ndef Prepare(benchmark_spec):\n  \"\"\"Install iperf and start the server on all machines.\n\n  Args:\n    benchmark_spec: The benchmark specification. Contains all data that is\n        required to run the benchmark.\n  \"\"\"\n  vms = benchmark_spec.vms\n  if len(vms) != 2:\n    raise ValueError(\n        'iperf benchmark requires exactly two machines, found {0}'.format(len(\n            vms)))\n\n  for vm in vms:\n    vm.Install('iperf')\n    if vm_util.ShouldRunOnExternalIpAddress():\n      vm.AllowPort(IPERF_PORT)\n    stdout, _ = vm.RemoteCommand(('nohup iperf --server --port %s &> /dev/null'\n                                  '& echo $!') % IPERF_PORT)\n    # TODO store this in a better place once we have a better place\n    vm.iperf_server_pid = stdout.strip()\n\n\n@vm_util.Retry(max_retries=IPERF_RETRIES)\ndef _RunIperf(sending_vm, receiving_vm, receiving_ip_address, ip_type):\n  \"\"\"Run iperf using sending 'vm' to connect to 'ip_address'.\n\n  Args:\n    sending_vm: The VM sending traffic.\n    receiving_vm: The VM receiving traffic.\n    receiving_ip_address: The IP address of the iperf server (ie the receiver).\n    ip_type: The IP type of 'ip_address' (e.g. 'internal', 'external')\n  Returns:\n    A Sample.\n  \"\"\"\n  iperf_cmd = ('iperf --client %s --port %s --format m --time %s -P %s' %\n               (receiving_ip_address, IPERF_PORT,\n                FLAGS.iperf_runtime_in_seconds,\n                FLAGS.iperf_sending_thread_count))\n  # the additional time on top of the iperf runtime is to account for the\n  # time it takes for the iperf process to start and exit\n  timeout_buffer = 30 + FLAGS.iperf_sending_thread_count\n  stdout, _ = sending_vm.RemoteCommand(iperf_cmd, should_log=True,\n                                       timeout=FLAGS.iperf_runtime_in_seconds +\n                                       timeout_buffer)\n\n  # Example output from iperf that needs to be parsed\n  # STDOUT: ------------------------------------------------------------\n  # Client connecting to 10.237.229.201, TCP port 5001\n  # TCP window size: 0.04 MByte (default)\n  # ------------------------------------------------------------\n  # [  6] local 10.76.234.115 port 53527 connected with 10.237.229.201 port 5001\n  # [  3] local 10.76.234.115 port 53524 connected with 10.237.229.201 port 5001\n  # [  4] local 10.76.234.115 port 53525 connected with 10.237.229.201 port 5001\n  # [  5] local 10.76.234.115 port 53526 connected with 10.237.229.201 port 5001\n  # [ ID] Interval       Transfer     Bandwidth\n  # [  4]  0.0-60.0 sec  3730 MBytes  521.1 Mbits/sec\n  # [  5]  0.0-60.0 sec  3499 MBytes   489 Mbits/sec\n  # [  6]  0.0-60.0 sec  3044 MBytes   425 Mbits/sec\n  # [  3]  0.0-60.0 sec  3738 MBytes   522 Mbits/sec\n  # [SUM]  0.0-60.0 sec  14010 MBytes  1957 Mbits/sec\n\n  thread_values = re.findall(r'\\[SUM].*\\s+(\\d+\\.?\\d*).Mbits/sec', stdout)\n  if not thread_values:\n    # If there is no sum you have try and figure out an estimate\n    # which happens when threads start at different times.  The code\n    # below will tend to overestimate a bit.\n    thread_values = re.findall('\\[.*\\d+\\].*\\s+(\\d+\\.?\\d*).Mbits/sec', stdout)\n\n    if len(thread_values) != FLAGS.iperf_sending_thread_count:\n      raise ValueError('Only %s out of %s iperf threads reported a'\n                       ' throughput value.' %\n                       (len(thread_values), FLAGS.iperf_sending_thread_count))\n\n  total_throughput = 0.0\n  for value in thread_values:\n    total_throughput += float(value)\n\n  metadata = {\n      # The meta data defining the environment\n      'receiving_machine_type': receiving_vm.machine_type,\n      'receiving_zone': receiving_vm.zone,\n      'sending_machine_type': sending_vm.machine_type,\n      'sending_thread_count': FLAGS.iperf_sending_thread_count,\n      'sending_zone': sending_vm.zone,\n      'runtime_in_seconds': FLAGS.iperf_runtime_in_seconds,\n      'ip_type': ip_type\n  }\n  return sample.Sample('Throughput', total_throughput, 'Mbits/sec', metadata)\n\n\ndef Run(benchmark_spec):\n  \"\"\"Run iperf on the target vm.\n\n  Args:\n    benchmark_spec: The benchmark specification. Contains all data that is\n        required to run the benchmark.\n\n  Returns:\n    A list of sample.Sample objects.\n  \"\"\"\n  vms = benchmark_spec.vms\n  results = []\n\n  logging.info('Iperf Results:')\n\n  # Send traffic in both directions\n  for sending_vm, receiving_vm in vms, reversed(vms):\n    # Send using external IP addresses\n    if vm_util.ShouldRunOnExternalIpAddress():\n      results.append(_RunIperf(sending_vm,\n                               receiving_vm,\n                               receiving_vm.ip_address,\n                               'external'))\n\n    # Send using internal IP addresses\n    if vm_util.ShouldRunOnInternalIpAddress(sending_vm,\n                                            receiving_vm):\n      results.append(_RunIperf(sending_vm,\n                               receiving_vm,\n                               receiving_vm.internal_ip,\n                               'internal'))\n\n  return results\n\n\ndef Cleanup(benchmark_spec):\n  \"\"\"Cleanup iperf on the target vm (by uninstalling).\n\n  Args:\n    benchmark_spec: The benchmark specification. Contains all data that is\n        required to run the benchmark.\n  \"\"\"\n  vms = benchmark_spec.vms\n  for vm in vms:\n    vm.RemoteCommand('kill -9 ' + vm.iperf_server_pid, ignore_failure=True)\n", "path": "perfkitbenchmarker/linux_benchmarks/iperf_benchmark.py"}]}