Skip to content

Commit 9c0c533

Browse files
author
Mike Kozicki
authored
Merge pull request #644 from HTTPArchive/throttle
Improve stability of benchmark-based CPU throttle scaling
2 parents 0521863 + 020b4ec commit 9c0c533

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

internal/webpagetest.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

wptagent.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,8 @@ def main():
11521152
parser.add_argument('--healthcheckport', type=int, default=8889, help='Run a HTTP health check server on the given port.')
11531153
parser.add_argument('--har', action='store_true', default=False,
11541154
help="Generate a per-run HAR file as part of the test result (defaults to False).")
1155+
parser.add_argument('--maxcpuscale', type=int, default=2,
1156+
help='Maximum scaling to apply to CPU throttle based on host benchmark (defaults to 2).')
11551157

11561158
# Video capture/display settings
11571159
parser.add_argument('--xvfb', action='store_true', default=False,

0 commit comments

Comments
 (0)