|
| 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