@@ -234,10 +234,32 @@ def benchmark_cpu(self):
234234 hash_val .update (hash_data )
235235 iteration += 1
236236 elapsed = monotonic () - start
237- self .cpu_scale_multiplier = 1.0 / elapsed
237+ self .cpu_scale_multiplier = min ( 1.0 / elapsed , float ( self . options . maxcpuscale ))
238238 logging .debug ('CPU Benchmark elapsed time: %0.3f, multiplier: %0.3f' ,
239239 elapsed , self .cpu_scale_multiplier )
240240
241+ # Get the median scale value from the last 9 benchmarks on this machine
242+ try :
243+ cpu_scale = []
244+ scale_file = os .path .join (self .persistent_dir , 'cpu_scale.json' )
245+ if os .path .isfile (scale_file ):
246+ with open (scale_file , 'r' ) as f_in :
247+ cpu_scale = json .load (f_in )
248+ if type (cpu_scale ) is list :
249+ if len (cpu_scale ) >= 9 :
250+ cpu_scale .pop (0 )
251+ cpu_scale .append (self .cpu_scale_multiplier )
252+ if not os .path .isdir (self .persistent_dir ):
253+ os .makedirs (self .persistent_dir )
254+ with open (scale_file , 'w' ) as f_out :
255+ json .dump (cpu_scale , f_out )
256+ cpu_scale .sort ()
257+ median_index = int ((len (cpu_scale ) - 1 ) / 2 )
258+ self .cpu_scale_multiplier = cpu_scale [median_index ]
259+ logging .debug ('CPU Benchmark selected multiplier: %0.3f at index %d of %d values' , self .cpu_scale_multiplier , median_index , len (cpu_scale ))
260+ except Exception :
261+ logging .exception ('Error processing benchmark history' )
262+
241263 def get_persistent_dir (self ):
242264 """Return the path to the persistent cache directory"""
243265 return self .persistent_dir
0 commit comments