Advertisement
Sander447

seleniumloadtest.py

Feb 7th, 2025
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.37 KB | Source Code | 0 0
  1. import time
  2. import csv
  3. import os
  4. import requests
  5. from selenium import webdriver
  6. from selenium.webdriver.common.by import By
  7. from selenium.webdriver.common.keys import Keys
  8. from selenium.webdriver.chrome.service import Service
  9. from selenium.webdriver.chrome.options import Options
  10. from datetime import datetime
  11. import matplotlib.pyplot as plt
  12. from datetime import datetime
  13.  
  14. class Measurement:
  15.     def __init__(self, measure_time: float, latency: float, file_size: float, variant: str):
  16.         self.measure_time = measure_time  # The exact time the measurement was made in milliseconds
  17.         self.latency = latency  # The latency in seconds
  18.         self.variant = variant  # Variant string (v0, v1, v2, v3)
  19.         self.file_size = file_size # File size in KB
  20.  
  21.     def __repr__(self):
  22.         return (f"Measurement(measure_time={self.measure_time:.2f}, "
  23.                 f"latency={self.latency:.2f} seconds, file_size={self.file_size} KB, variant='{self.variant}')")
  24.  
  25. def run():
  26.     # Setup Chrome options.
  27.     chrome_options = Options()
  28.     chrome_options.add_argument("--use-gl=desktop")
  29.     chrome_options.add_argument("--enable-gpu-rasterization")
  30.     chrome_options.add_argument("--ignore-gpu-blocklist")
  31.     chrome_options.add_argument("--enable-webgl")
  32.     chrome_options.add_argument("--enable-webgl2")
  33.     chrome_options.add_argument("--use-gl=angle")
  34.  
  35.     # Set up logging to capture console logs.
  36.     chrome_options.set_capability("goog:loggingPrefs", {"browser": "ALL"})
  37.  
  38.     # Initialize the web driver.
  39.     driver = webdriver.Chrome(options=chrome_options)
  40.  
  41.     request_timestamps = {}
  42.     measurements = []
  43.  
  44.     # pageUrl = 'http://chimay.science.uva.nl:8092/player/24b85f65-6b67-4657-8316-4f914d1a467f/f6baba31-1901-42e6-9b7d-3eeea1e91a75'
  45.     # pageUrl = 'http://34.90.61.78:8092/player/12ecbd47-169f-44b0-bf67-1d616dad19dc/06913a13-4c76-4c0b-97c2-f2e758738bfa'
  46.     pageUrl = 'http://ec2-18-202-178-55.eu-west-1.compute.amazonaws.com:8092/player/28ad1095-7272-45de-9da4-0ed81942b599/5a658725-1037-4ffe-b1d8-a4aa2e6491ca'
  47.     # pageUrl = 'http://20.82.114.202:8092/player/164007e1-20a0-4a12-9a3f-cfb6dfb9ed09/b211d327-e824-44a5-b886-b8b64c3aa17d'
  48.  
  49.     # Navigate to the page.
  50.     driver.get(pageUrl)
  51.  
  52.     # Log in with access code.
  53.     code_input = driver.find_element(By.ID, "Code")
  54.     code_input.send_keys("sander")
  55.     submit_button = driver.find_element(By.XPATH, '//button[text()="Submit"]')
  56.     submit_button.click()
  57.  
  58.     # Wait for the network to be idle.
  59.     driver.implicitly_wait(5)
  60.  
  61.     # Start experience.
  62.     start_button = driver.find_element(By.XPATH, '//button[text()="Start"]')
  63.     start_button.click()
  64.  
  65.     # Wait for the network to be idle.
  66.     driver.implicitly_wait(5)
  67.  
  68.     # Keep listening for requests and responses for a longer period to capture HLS segment requests.
  69.     time.sleep(5 * 60)
  70.  
  71.     job_id = os.getenv("SLURM_JOB_ID", "no_job_id")
  72.     task_id = os.getenv("SLURM_PROCID", "no_task_id")
  73.     node_id = os.getenv("SLURM_NODEID", "no_node_id")
  74.  
  75.     # Parse console logs captured by the browser.
  76.     for entry in driver.get_log('browser'):
  77.         message = entry['message']
  78.         print(f"{task_id} - {entry['level']} - {entry['message']}")
  79.         if '[hls]' in message and '.ts,' in message:
  80.             _, url, endTime, latency, fileSize = message.split(',')
  81.             variant = url[-8:-6]
  82.             measurement = Measurement(int(endTime), float(latency), float(fileSize[:-3]), variant)
  83.             measurements.append(measurement)
  84.  
  85.     variant_map = {'v0': 0, 'v1': 1, 'v2': 2, 'v3': 3}
  86.     y_labels = ['v0', 'v1', 'v2', 'v3']
  87.     x_values = [m.measure_time for m in measurements]
  88.     y_values = [variant_map[m.variant] for m in measurements]
  89.     z_values = [m.latency for m in measurements]
  90.     a_values = [m.file_size for m in measurements]
  91.  
  92.     # Get SLURM job ID, process ID and task ID to make unique filenames.
  93.     output_filename = f"measurement_data_{job_id}_{node_id}_{task_id}.csv"
  94.  
  95.     with open(output_filename, mode="w", newline="") as file:
  96.         writer = csv.writer(file)
  97.         # Write headers.
  98.         writer.writerow(["Time", "Variant", "Latency", "File_size"])
  99.         # Write each pair of x and y values to the CSV.
  100.         for x, y, z, a in zip(x_values, y_values, z_values, a_values):
  101.             writer.writerow([x, y, z, a])
  102.  
  103.     driver.quit()
  104.  
  105. if __name__ == "__main__":
  106.     run()
  107.  
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement