Skip to content

Commit

Permalink
Merge pull request #644 from HTTPArchive/throttle
Browse files Browse the repository at this point in the history
Improve stability of benchmark-based CPU throttle scaling
  • Loading branch information
mjkozicki committed Apr 17, 2024
2 parents 0521863 + 020b4ec commit 9c0c533
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
24 changes: 23 additions & 1 deletion internal/webpagetest.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,32 @@ def benchmark_cpu(self):
hash_val.update(hash_data)
iteration += 1
elapsed = monotonic() - start
self.cpu_scale_multiplier = 1.0 / elapsed
self.cpu_scale_multiplier = min(1.0 / elapsed, float(self.options.maxcpuscale))
logging.debug('CPU Benchmark elapsed time: %0.3f, multiplier: %0.3f',
elapsed, self.cpu_scale_multiplier)

# Get the median scale value from the last 9 benchmarks on this machine
try:
cpu_scale = []
scale_file = os.path.join(self.persistent_dir, 'cpu_scale.json')
if os.path.isfile(scale_file):
with open(scale_file, 'r') as f_in:
cpu_scale = json.load(f_in)
if type(cpu_scale) is list:
if len(cpu_scale) >= 9:
cpu_scale.pop(0)
cpu_scale.append(self.cpu_scale_multiplier)
if not os.path.isdir(self.persistent_dir):
os.makedirs(self.persistent_dir)
with open(scale_file, 'w') as f_out:
json.dump(cpu_scale, f_out)
cpu_scale.sort()
median_index = int((len(cpu_scale) - 1) / 2)
self.cpu_scale_multiplier = cpu_scale[median_index]
logging.debug('CPU Benchmark selected multiplier: %0.3f at index %d of %d values', self.cpu_scale_multiplier, median_index, len(cpu_scale))
except Exception:
logging.exception('Error processing benchmark history')

def get_persistent_dir(self):
"""Return the path to the persistent cache directory"""
return self.persistent_dir
Expand Down
2 changes: 2 additions & 0 deletions wptagent.py
Original file line number Diff line number Diff line change
Expand Up @@ -1152,6 +1152,8 @@ def main():
parser.add_argument('--healthcheckport', type=int, default=8889, help='Run a HTTP health check server on the given port.')
parser.add_argument('--har', action='store_true', default=False,
help="Generate a per-run HAR file as part of the test result (defaults to False).")
parser.add_argument('--maxcpuscale', type=int, default=2,
help='Maximum scaling to apply to CPU throttle based on host benchmark (defaults to 2).')

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

0 comments on commit 9c0c533

Please sign in to comment.