-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathalgorithms.py
More file actions
175 lines (141 loc) · 6.25 KB
/
Copy pathalgorithms.py
File metadata and controls
175 lines (141 loc) · 6.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import enum
import heapq
from abc import ABC, abstractmethod
from typing import TYPE_CHECKING, NamedTuple
if TYPE_CHECKING:
from _pytest import nodes
class TestGroup(NamedTuple):
selected: "list[nodes.Item]"
deselected: "list[nodes.Item]"
duration: float
class AlgorithmBase(ABC):
"""Abstract base class for the algorithm implementations."""
@abstractmethod
def __call__(
self, splits: int, durations: "dict[nodes.Item, float]"
) -> "list[TestGroup]":
pass
def __hash__(self) -> int:
return hash(self.__class__.__name__)
def __eq__(self, other: object) -> bool:
if not isinstance(other, AlgorithmBase):
return NotImplemented
return self.__class__.__name__ == other.__class__.__name__
class LeastDurationAlgorithm(AlgorithmBase):
"""
Split tests into groups by runtime.
It walks the test items, starting with the test with largest duration.
It assigns the test with the largest runtime to the group with the smallest duration sum.
The algorithm sorts the items by their duration. Since the sorting algorithm is stable, ties will be broken by
maintaining the original order of items. It is therefore important that the order of items be identical on all nodes
that use this plugin. Due to issue #25 this might not always be the case.
:param splits: How many groups we're splitting in.
:param durations: Mapping from each test item to its duration. Build it with :func:`compute_durations`.
:return:
List of groups
"""
def __call__(
self, splits: int, durations: "dict[nodes.Item, float]"
) -> "list[TestGroup]":
# add index of item in list
items_with_durations_indexed = [
(item, dur, i) for i, (item, dur) in enumerate(durations.items())
]
# Sort by name to ensure it's always the same order
items_with_durations_indexed = sorted(
items_with_durations_indexed, key=lambda tup: tup[0].nodeid
)
# sort in ascending order
sorted_items_with_durations = sorted(
items_with_durations_indexed, key=lambda tup: tup[1], reverse=True
)
selected: list[list[tuple[nodes.Item, int]]] = [[] for _ in range(splits)]
deselected: list[list[nodes.Item]] = [[] for _ in range(splits)]
duration: list[float] = [0 for _ in range(splits)]
# create a heap of the form (summed_durations, group_index)
heap: list[tuple[float, int]] = [(0, i) for i in range(splits)]
heapq.heapify(heap)
for item, item_duration, original_index in sorted_items_with_durations:
# get group with smallest sum
summed_durations, group_idx = heapq.heappop(heap)
new_group_durations = summed_durations + item_duration
# store assignment
selected[group_idx].append((item, original_index))
duration[group_idx] = new_group_durations
for i in range(splits):
if i != group_idx:
deselected[i].append(item)
# store new duration - in case of ties it sorts by the group_idx
heapq.heappush(heap, (new_group_durations, group_idx))
groups = []
for i in range(splits):
# sort the items by their original index to maintain relative ordering
# we don't care about the order of deselected items
s = [
item
for item, original_index in sorted(selected[i], key=lambda tup: tup[1])
]
group = TestGroup(
selected=s, deselected=deselected[i], duration=duration[i]
)
groups.append(group)
return groups
class DurationBasedChunksAlgorithm(AlgorithmBase):
"""
Split tests into groups by runtime.
Ensures tests are split into non-overlapping groups.
The original list of test items is split into groups by finding boundary indices i_0, i_1, i_2
and creating group_1 = items[0:i_0], group_2 = items[i_0, i_1], group_3 = items[i_1, i_2], ...
:param splits: How many groups we're splitting in.
:param durations: Mapping from each test item to its duration. Build it with :func:`compute_durations`.
:return: List of TestGroup
"""
def __call__(
self, splits: int, durations: "dict[nodes.Item, float]"
) -> "list[TestGroup]":
time_per_group = sum(durations.values()) / splits
selected: list[list[nodes.Item]] = [[] for i in range(splits)]
deselected: list[list[nodes.Item]] = [[] for i in range(splits)]
duration: list[float] = [0 for i in range(splits)]
group_idx = 0
for item, item_duration in durations.items():
if duration[group_idx] >= time_per_group:
group_idx += 1
selected[group_idx].append(item)
for i in range(splits):
if i != group_idx:
deselected[i].append(item)
duration[group_idx] += item_duration
return [
TestGroup(
selected=selected[i], deselected=deselected[i], duration=duration[i]
)
for i in range(splits)
]
def compute_durations(
items: "list[nodes.Item]", cached_durations: "dict[str, float]"
) -> "dict[nodes.Item, float]":
"""
Build the splitting input from collected items and their cached durations.
Items missing from ``cached_durations`` get the average duration of the
cached entries that are relevant to this suite; with no cached data at
all, every item gets ``1`` as a placeholder.
"""
# Filtering down durations to relevant ones ensures the avg isn't skewed by irrelevant data
relevant = {
item.nodeid: cached_durations[item.nodeid]
for item in items
if item.nodeid in cached_durations
}
if relevant:
avg = sum(relevant.values()) / len(relevant)
else:
# If there are no durations, give every test the same arbitrary value
avg = 1
return {item: relevant.get(item.nodeid, avg) for item in items}
class Algorithms(enum.Enum):
duration_based_chunks = DurationBasedChunksAlgorithm()
least_duration = LeastDurationAlgorithm()
@staticmethod
def names() -> "list[str]":
return [x.name for x in Algorithms]