This repository was archived by the owner on Mar 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 634
Expand file tree
/
Copy pathconcurrent.py
More file actions
69 lines (54 loc) · 1.66 KB
/
concurrent.py
File metadata and controls
69 lines (54 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Copyright 2016 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Benchmarks for simple parallel calculations."""
import threading
import weetest
def Arithmetic(n):
return n * 3 + 2
def Fib(n):
if n < 2:
return 1
return Fib(n - 1) + Fib(n - 2)
_WORKLOADS = [
(Arithmetic, 1001),
(Fib, 10),
]
def _MakeParallelBenchmark(p, work_func, *args):
"""Create and return a benchmark that runs work_func p times in parallel."""
def Benchmark(b): # pylint: disable=missing-docstring
e = threading.Event()
def Target():
e.wait()
for _ in xrange(b.N / p):
work_func(*args)
threads = []
for _ in xrange(p):
t = threading.Thread(target=Target)
t.start()
threads.append(t)
b.ResetTimer()
e.set()
for t in threads:
t.join()
return Benchmark
def _RegisterBenchmarks():
for p in xrange(1, 13):
for work_func, arg in _WORKLOADS:
name = 'Benchmark' + work_func.__name__
if p > 1:
name += 'Parallel%s' % p
globals()[name] = _MakeParallelBenchmark(p, work_func, arg)
_RegisterBenchmarks()
if __name__ == '__main__':
weetest.RunBenchmarks()