Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
103 changes: 103 additions & 0 deletions benchmark.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
"""Benchmark test

Use this on an ad hoc basis to test the performance of library changes.

To run:
`pip install pytest-benchmark`
`pytest benchmark.py`
"""

import axelrod as axl
from axelrod.graph import Graph


def few_players_no_mutations():
edges = [(0, 1), (1, 2), (2, 3), (3, 1)]
graph = Graph(edges)
players = [
axl.Cooperator(),
axl.Cooperator(),
axl.Cooperator(),
axl.Defector(),
]
mp = axl.MoranProcess(players, interaction_graph=graph, seed=40)
_ = mp.play()


def few_players_mutations():
edges = [(0, 1), (1, 2), (2, 3), (3, 1)]
graph = Graph(edges)
players = [
axl.Cooperator(),
axl.Cooperator(),
axl.Cooperator(),
axl.Defector(),
]
mp = axl.MoranProcess(
players, interaction_graph=graph, mutation_rate=0.5, seed=40
)
for _ in range(100):
try:
mp.__next__()
except StopIteration:
break


def four_distinct_players_no_mutations():
players = [axl.Cooperator(), axl.Defector(), axl.TitForTat(), axl.Grudger()]
mp = axl.MoranProcess(players, seed=40)
_ = mp.play()


def four_distinct_players_mutations():
players = [axl.Cooperator(), axl.Defector(), axl.TitForTat(), axl.Grudger()]
mp = axl.MoranProcess(players, mutation_rate=0.5, seed=40)
for _ in range(50):
try:
mp.__next__()
except StopIteration:
break


def more_players_no_mutations():
players = [axl.Cooperator() for _ in range(9)] + [
axl.Defector() for _ in range(3)
]
mp = axl.MoranProcess(players, seed=40)
_ = mp.play()


def more_players_mutations():
players = [axl.Cooperator() for _ in range(9)] + [
axl.Defector() for _ in range(3)
]
mp = axl.MoranProcess(players, mutation_rate=0.5, seed=40)
for _ in range(50):
try:
mp.__next__()
except StopIteration:
break


def test_few_players_no_mutations(benchmark):
benchmark(few_players_no_mutations)


def test_few_players_mutations(benchmark):
benchmark(few_players_mutations)


def test_four_distinct_players_no_mutations(benchmark):
benchmark(four_distinct_players_no_mutations)


def test_four_distinct_players_mutations(benchmark):
benchmark(four_distinct_players_mutations)


def test_more_players_no_mutations(benchmark):
benchmark(more_players_no_mutations)


def test_more_players_mutations(benchmark):
benchmark(more_players_mutations)
18 changes: 18 additions & 0 deletions docs/how-to/contributing/running_tests.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,21 @@ You can also run the doctests on any given file. For example, to run the
doctests for the :code:`docs/tutorials/getting_started/match.rst` file::

$ python -m doctest docs/tutorials/getting_started/match.rst

Performance testing
-------------------

Performance is not automatically tested as of yet. To check the performance
of a change, first install pytest-benchmark::

$ pip install pytest-benchmark

Then run benchmark.py before and after the change::

$ pytest benchmark.py

Record the before and after report on the pull request.

benchmark.py only tests the Moran process, which in turn tests a lot of other
functionality. If the function you're changing is not covered by the file,
add another function.
Loading