Skip to content

Commit 8b808f5

Browse files
committed
move performance tests to test_larger_random_cases
1 parent e5b41e4 commit 8b808f5

File tree

2 files changed

+59
-53
lines changed

2 files changed

+59
-53
lines changed

tests/test_large_case.py

-53
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
import pytest
2-
import random
3-
import time
4-
from multiprocessing import Process, Queue
5-
62

73
from allocation import allocating
84

@@ -65,52 +61,3 @@ def test_finishes_alternate(large_one_to_one_alternate):
6561
for e in large_one_to_one_alternate.sources.collection:
6662
if e is not None:
6763
assert allocation[e] == {str(e)}
68-
69-
70-
def large_random(sources_number, targets_number, choices, limit_denominator):
71-
sources = {str(s): 1 for s in range(sources_number)}
72-
targets = {str(t): 1 for t in range(targets_number)}
73-
74-
wmap_list = []
75-
for source in sources:
76-
stargets = set(random.choices(list(targets), k=choices))
77-
for t in stargets:
78-
wmap_list.append({'from': source, 'to': t,
79-
'weight': random.uniform(0, 1)})
80-
wmap = allocating.DictWeightedMap(wmap_list)
81-
82-
return allocating.Allocator(sources, wmap, targets, limit_denominator)
83-
84-
85-
testdata = [
86-
(40, 50, 5, 0, 15),
87-
(40, 50, 5, 100, 25),
88-
(60, 70, 5, 0, 15),
89-
(50, 100, 5, 0, 45),
90-
]
91-
92-
93-
@pytest.mark.parametrize('sources_number,targets_number,choices,limit_denominator,expected_time', testdata)
94-
def test_finishes_random(sources_number, targets_number, choices, limit_denominator, expected_time):
95-
random.seed(1234)
96-
allocator = large_random(sources_number, targets_number, choices, limit_denominator)
97-
98-
def do_alloc(queue):
99-
allocation = allocator.get_best()
100-
queue.put(allocation)
101-
102-
queue = Queue()
103-
process = Process(target=do_alloc, args=(queue,))
104-
start = time.monotonic()
105-
process.start()
106-
process.join(expected_time * 2)
107-
108-
if process.is_alive():
109-
process.terminate()
110-
raise AssertionError((f'random case with {sources_number} sources, {targets_number} targets, '
111-
f'{choices} choices and {limit_denominator} limit_denominator '
112-
f'took more than {expected_time * 2} seconds to finish'))
113-
else:
114-
print(f'allocation took {time.monotonic() - start:.3f} seconds')
115-
116-
assert len(queue.get()) >= min(sources_number, targets_number) / 2

tests/test_larger_random_cases.py

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import pytest
2+
3+
import os
4+
import random
5+
import time
6+
from multiprocessing import Process, Queue
7+
8+
from allocation import allocating
9+
10+
11+
def large_random(sources_number, targets_number, choices, limit_denominator, wmclass):
12+
sources = {str(s): 1 for s in range(sources_number)}
13+
targets = {str(t): 1 for t in range(targets_number)}
14+
15+
wmap_list = []
16+
for source in sources:
17+
stargets = set(random.choices(list(targets), k=choices))
18+
for t in stargets:
19+
wmap_list.append({'from': source, 'to': t,
20+
'weight': random.uniform(0, 1)})
21+
wmap = wmclass(wmap_list)
22+
23+
return allocating.Allocator(sources, wmap, targets, limit_denominator)
24+
25+
26+
testdata = [
27+
(40, 50, 5, 0, 15),
28+
(40, 50, 5, 100, 25),
29+
(60, 70, 5, 0, 15),
30+
(50, 100, 5, 0, 45),
31+
]
32+
33+
34+
@pytest.mark.skipif(os.environ.get("TEST_TYPE", None) != "performance", reason="Not doing performance testing")
35+
@pytest.mark.parametrize('sources_number,targets_number,choices,limit_denominator,expected_time', testdata)
36+
@pytest.mark.parametrize('wmclass', [allocating.ListWeightedMap, allocating.DictWeightedMap])
37+
def test_finishes_random(sources_number, targets_number, choices, limit_denominator, expected_time, wmclass):
38+
random.seed(1234)
39+
allocator = large_random(sources_number, targets_number, choices, limit_denominator, wmclass)
40+
41+
def do_alloc(queue):
42+
allocation = allocator.get_best()
43+
queue.put(allocation)
44+
45+
queue = Queue()
46+
process = Process(target=do_alloc, args=(queue,))
47+
start = time.monotonic()
48+
process.start()
49+
process.join(expected_time * 2)
50+
51+
if process.is_alive():
52+
process.terminate()
53+
raise AssertionError((f'random case with {sources_number} sources, {targets_number} targets, '
54+
f'{choices} choices and {limit_denominator} limit_denominator '
55+
f'took more than {expected_time * 2} seconds to finish'))
56+
else:
57+
print(f'allocation took {time.monotonic() - start:.3f} seconds')
58+
59+
assert len(queue.get()) >= min(sources_number, targets_number) / 2

0 commit comments

Comments
 (0)