Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add more comprehensive runtime tests. #14

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ type: docker
name: python-minimal

steps:
- name: test
- name: mypy
image: python:3.8
commands:
- pip install --no-cache-dir -r requirements-dev.txt
- python3 -m mypy schulze_condorcet/ tests/
- name: test
image: python:3.8
commands:
- python3 -m unittest -v tests/*.py

---
Expand All @@ -17,9 +20,12 @@ type: docker
name: python-recent

steps:
- name: test
- name: mypy
image: python
commands:
- pip install --no-cache-dir -r requirements-dev.txt
- python3 -m mypy schulze_condorcet/ tests/
- name: test
image: python
commands:
- python3 -m unittest -v tests/*.py
44 changes: 24 additions & 20 deletions tests/test_schulze.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import datetime
import random
from typing import Dict, List, Optional, Tuple
from typing import Dict, List, Optional, Sequence, Tuple
import unittest

from schulze_condorcet import schulze_evaluate
Expand Down Expand Up @@ -96,25 +96,29 @@ def test_schulze_runtime(self) -> None:
# silly test, since I just realized, that the algorithm runtime is
# linear in the number of votes, but a bit more scary in the number
# of candidates
candidates = ('0', '1', '2', '3', '4')
votes = []
for _ in range(2000):
parts = list(candidates)
random.shuffle(parts)
relations = (random.choice(('=', '>'))
for _ in range(len(candidates)))
vote = ''.join(c + r for c, r in zip(candidates, relations))
votes.append(vote[:-1])
times = {}
for num in (10, 100, 1000, 2000):
start = datetime.datetime.utcnow()
for _ in range(10):
schulze_evaluate(votes[:num], candidates)
stop = datetime.datetime.utcnow()
times[num] = stop - start
reference = datetime.timedelta(milliseconds=5)
for num, delta in times.items():
self.assertGreater(num * reference, delta)
num_evaluation_runs = 3
reference = datetime.timedelta(microseconds=2)

def create_random_votes(candidates: Sequence[str], n: int) -> List[str]:
return [''.join(c + random.choice(("=", ">")) for c in random.sample(candidates, len(candidates)))[:-1]
for _ in range(n)]

for num_candidates in (3, 5, 10, 20):
candidates = tuple(map(str, range(num_candidates)))
for num_votes in (100, 1000, 2000):
votes = create_random_votes(candidates, num_votes)
# Evaluation time depends linearly on number of votes and quadratically on number of candidates.
time_limit = num_votes * num_candidates ** 2 * reference * num_evaluation_runs
for metric in (margin, winning_votes):
with self.subTest(c=num_candidates, v=num_votes, m=metric.__name__):
runtimes = []
for _ in range(num_evaluation_runs):
start = datetime.datetime.utcnow()
schulze_evaluate(votes, candidates, metric)
runtimes.append(datetime.datetime.utcnow() - start)
total_runtime = sum(runtimes, datetime.timedelta())
self.assertLess(total_runtime, time_limit)
self.assertGreater(10 * total_runtime, time_limit)

def test_schulze_candidates(self) -> None:
# superfluous candidates in votes
Expand Down