|
| 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) |
0 commit comments