Skip to content

Commit d7d90c0

Browse files
committed
Schedule longer tasks first; other minor tweaks
1 parent 1396815 commit d7d90c0

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

mypy/waiter.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import sys
1313
import tempfile
1414
import time
15+
import json
16+
from collections import defaultdict
1517

1618

1719
class WaiterError(Exception):
@@ -32,7 +34,7 @@ def __init__(self, name: str, args: List[str], *, cwd: str = None,
3234

3335
def start(self) -> None:
3436
self.outfile = tempfile.TemporaryFile()
35-
self.start_time = time.time()
37+
self.start_time = time.perf_counter()
3638
self.process = Popen(self.args, cwd=self.cwd, env=self.env,
3739
stdout=self.outfile, stderr=STDOUT)
3840
self.pid = self.process.pid
@@ -107,6 +109,9 @@ class Waiter:
107109
if not waiter.run():
108110
print('error')
109111
"""
112+
113+
TIMING_FILENAME = 'runtest_timing.json'
114+
110115
def __init__(self, limit: int = 0, *, verbosity: int = 0, xfail: List[str] = []) -> None:
111116
self.verbosity = verbosity
112117
self.queue = [] # type: List[LazySubprocess]
@@ -129,6 +134,16 @@ def __init__(self, limit: int = 0, *, verbosity: int = 0, xfail: List[str] = [])
129134
self.times1 = {} # type: Dict[str, float]
130135
self.times2 = {} # type: Dict[str, float]
131136

137+
# we likely add only a few tasks, so it's ok to put them in front even if they fast
138+
# much worse if we put them in the back, and one of them turns out to be slow
139+
self.times: Dict[str, float] = defaultdict(lambda: float('inf'))
140+
try:
141+
with open(self.TIMING_FILENAME) as fp:
142+
times = json.load(fp)
143+
self.times.update(times)
144+
except Exception:
145+
print('cannot find runtest timing file')
146+
132147
def add(self, cmd: LazySubprocess) -> int:
133148
rv = len(self.queue)
134149
self.queue.append(cmd)
@@ -161,12 +176,13 @@ def _record_time(self, name: str, elapsed_time: float) -> None:
161176

162177
def _poll_current(self) -> Tuple[int, int]:
163178
while True:
164-
time.sleep(.05)
179+
time.sleep(.01)
165180
for pid in self.current:
166181
cmd = self.current[pid][1]
167182
code = cmd.process.poll()
168183
if code is not None:
169-
cmd.end_time = time.time()
184+
cmd.end_time = time.perf_counter()
185+
self.times[cmd.name] = cmd.end_time - cmd.start_time
170186
return pid, code
171187

172188
def _wait_next(self) -> Tuple[List[str], int, int]:
@@ -239,6 +255,10 @@ def run(self) -> int:
239255
if self.verbosity == 0:
240256
self._note = Noter(len(self.queue))
241257
print('SUMMARY %d tasks selected' % len(self.queue))
258+
259+
self.queue = sorted(self.queue, key=lambda c: self.times[c.name], reverse=True)
260+
print([t.name for t in self.queue][:5])
261+
242262
sys.stdout.flush()
243263
# Failed tasks.
244264
all_failures = [] # type: List[str]
@@ -272,6 +292,12 @@ def run(self) -> int:
272292
print('*** OK ***')
273293
sys.stdout.flush()
274294

295+
try:
296+
with open(self.TIMING_FILENAME, 'w') as fp:
297+
json.dump(self.times, fp)
298+
except Exception:
299+
print('cannot save runtest timing file')
300+
275301
return 0
276302

277303

0 commit comments

Comments
 (0)