3
3
This is used for running mypy tests.
4
4
"""
5
5
6
- from typing import Dict , List , Optional , Set , Tuple
6
+ from typing import Dict , List , Optional , Set , Tuple , Any
7
7
8
8
import os
9
9
from multiprocessing import cpu_count
@@ -110,8 +110,8 @@ class Waiter:
110
110
if not waiter.run():
111
111
print('error')
112
112
"""
113
-
114
- TIMING_FILENAME = '.runtest_timing .json'
113
+ LOGSIZE = 50
114
+ FULL_LOG_FILENAME = '.runtest_log .json'
115
115
116
116
def __init__ (self , limit : int = 0 , * , verbosity : int = 0 , xfail : List [str ] = []) -> None :
117
117
self .verbosity = verbosity
@@ -135,18 +135,19 @@ def __init__(self, limit: int = 0, *, verbosity: int = 0, xfail: List[str] = [])
135
135
self ._note = None # type: Noter
136
136
self .times1 = {} # type: Dict[str, float]
137
137
self .times2 = {} # type: Dict[str, float]
138
+ self .new_log = defaultdict (dict ) # type: Dict[str, Dict[str, Any]]
138
139
139
- # we likely add only a few tasks, so it's ok to put them in front even if they fast
140
- # much worse if we put them in the back, and one of them turns out to be slow
141
- self .times = defaultdict (lambda : float ('inf' )) # type: Dict[str, float]
140
+ def load_log_file (self ) -> Optional [List [Dict [str , Dict [str , Any ]]]]:
142
141
try :
143
- with open (self .TIMING_FILENAME ) as fp :
144
- times = json .load (fp )
145
- self .times .update (times )
146
- self .found_timing_file = True
147
- except Exception :
148
- print ('cannot find runtest timing file' )
149
- self .found_timing_file = False
142
+ # get the last log
143
+ with open (self .FULL_LOG_FILENAME ) as fp :
144
+ test_log = json .load (fp )
145
+ except FileNotFoundError :
146
+ test_log = []
147
+ except json .JSONDecodeError :
148
+ print ('corrupt test log file {}' .format (self .FULL_LOG_FILENAME ))
149
+ test_log = []
150
+ return test_log
150
151
151
152
def add (self , cmd : LazySubprocess ) -> int :
152
153
rv = len (self .queue )
@@ -186,7 +187,8 @@ def _poll_current(self) -> Tuple[int, int]:
186
187
code = cmd .process .poll ()
187
188
if code is not None :
188
189
cmd .end_time = time .perf_counter ()
189
- self .times [cmd .name ] = cmd .end_time - cmd .start_time
190
+ self .new_log [cmd .name ]['exit_code' ] = code
191
+ self .new_log [cmd .name ]['runtime' ] = cmd .end_time - cmd .start_time
190
192
return pid , code
191
193
192
194
def _wait_next (self ) -> Tuple [List [str ], int , int ]:
@@ -260,8 +262,16 @@ def run(self) -> int:
260
262
self ._note = Noter (len (self .queue ))
261
263
print ('SUMMARY %d tasks selected' % len (self .queue ))
262
264
263
- self .queue = sorted (self .queue , key = lambda c : self .times [c .name ], reverse = True )
264
- print ([t .name for t in self .queue ][:5 ])
265
+ if self .limit > 1 :
266
+ logs = self .load_log_file ()
267
+ if logs :
268
+ # we don't know how long a new task takes
269
+ # better err by putting it in front in case it is slow:
270
+ # a fast task in front hurts performance less than a slow task in the back
271
+ default = float ('inf' )
272
+ times = {cmd .name : sum (log [cmd .name ].get ('runtime' , default ) for log in logs )
273
+ / len (logs ) for cmd in self .queue }
274
+ self .queue = sorted (self .queue , key = lambda cmd : times [cmd .name ], reverse = True )
265
275
266
276
sys .stdout .flush ()
267
277
# Failed tasks.
@@ -296,12 +306,14 @@ def run(self) -> int:
296
306
print ('*** OK ***' )
297
307
sys .stdout .flush ()
298
308
299
- if not self .found_timing_file :
309
+ if self .limit > 1 :
310
+ # log only LOGSIZE most recent tests
311
+ test_log = (self .load_log_file () + [self .new_log ])[:self .LOGSIZE ]
300
312
try :
301
- with open (self .TIMING_FILENAME , 'w' ) as fp :
302
- json .dump (self . times , fp , sort_keys = True , indent = 4 )
303
- except Exception :
304
- print ('cannot save runtest timing file' )
313
+ with open (self .FULL_LOG_FILENAME , 'w' ) as fp :
314
+ json .dump (test_log , fp , sort_keys = True , indent = 4 )
315
+ except Exception as e :
316
+ print ('cannot save test log file:' , e )
305
317
306
318
return 0
307
319
0 commit comments