Skip to content

Commit 3fe99d8

Browse files
committed
Build moran benchmark
1 parent 42ecb93 commit 3fe99d8

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

benchmark.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
"""Benchmark test
2+
3+
Use this on an ad hoc basis to test the performance of library changes.
4+
5+
To run:
6+
`pip install pytest-benchmark`
7+
`pytest benchmark.py`
8+
"""
9+
10+
import axelrod as axl
11+
from axelrod.graph import Graph
12+
13+
def few_players_no_mutations():
14+
edges = [(0, 1), (1, 2), (2, 3), (3, 1)]
15+
graph = Graph(edges)
16+
players = [axl.Cooperator(), axl.Cooperator(), axl.Cooperator(), axl.Defector()]
17+
mp = axl.MoranProcess(players, interaction_graph=graph, seed=40)
18+
_ = mp.play()
19+
20+
def few_players_mutations():
21+
edges = [(0, 1), (1, 2), (2, 3), (3, 1)]
22+
graph = Graph(edges)
23+
players = [axl.Cooperator(), axl.Cooperator(), axl.Cooperator(), axl.Defector()]
24+
mp = axl.MoranProcess(players, interaction_graph=graph, mutation_rate=0.5, seed=40)
25+
for _ in range(100):
26+
try:
27+
mp.__next__()
28+
except StopIteration:
29+
break
30+
31+
def four_distinct_players_no_mutations():
32+
players = [axl.Cooperator(), axl.Defector(), axl.TitForTat(), axl.Grudger()]
33+
mp = axl.MoranProcess(players, seed=40)
34+
_ = mp.play()
35+
36+
def four_distinct_players_mutations():
37+
players = [axl.Cooperator(), axl.Defector(), axl.TitForTat(), axl.Grudger()]
38+
mp = axl.MoranProcess(players, mutation_rate=0.5, seed=40)
39+
for _ in range(50):
40+
try:
41+
mp.__next__()
42+
except StopIteration:
43+
break
44+
45+
def more_players_no_mutations():
46+
players = [axl.Cooperator() for _ in range(9)] + [axl.Defector() for _ in range(3)]
47+
mp = axl.MoranProcess(players, seed=40)
48+
_ = mp.play()
49+
50+
def more_players_mutations():
51+
players = [axl.Cooperator() for _ in range(9)] + [axl.Defector() for _ in range(3)]
52+
mp = axl.MoranProcess(players, mutation_rate=0.5, seed=40)
53+
for _ in range(50):
54+
try:
55+
mp.__next__()
56+
except StopIteration:
57+
break
58+
59+
def test_few_players_no_mutations(benchmark):
60+
benchmark(few_players_no_mutations)
61+
62+
def test_few_players_mutations(benchmark):
63+
benchmark(few_players_mutations)
64+
65+
def test_four_distinct_players_no_mutations(benchmark):
66+
benchmark(four_distinct_players_no_mutations)
67+
68+
def test_four_distinct_players_mutations(benchmark):
69+
benchmark(four_distinct_players_mutations)
70+
71+
def test_more_players_no_mutations(benchmark):
72+
benchmark(more_players_no_mutations)
73+
74+
def test_more_players_mutations(benchmark):
75+
benchmark(more_players_mutations)

docs/how-to/contributing/running_tests.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,21 @@ You can also run the doctests on any given file. For example, to run the
5858
doctests for the :code:`docs/tutorials/getting_started/match.rst` file::
5959

6060
$ python -m doctest docs/tutorials/getting_started/match.rst
61+
62+
Performance testing
63+
-------------------
64+
65+
Performance is not automatically tested as of yet. To check the performance
66+
of a change, first install pytest-benchmark::
67+
68+
$ pip install pytest-benchmark
69+
70+
Then run benchmark.py before and after the change::
71+
72+
$ pytest benchmark.py
73+
74+
Record the before and after report on the pull request.
75+
76+
benchmark.py only tests the Moran process, which in turn tests a lot of other
77+
functionality. If the function you're changing is not covered by the file,
78+
add another function.

0 commit comments

Comments
 (0)