Skip to content

Instantly share code, notes, and snippets.

@ihaid
Created March 6, 2024 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ihaid/f23df98b09cb7e86f4a19512748086c5 to your computer and use it in GitHub Desktop.
Save ihaid/f23df98b09cb7e86f4a19512748086c5 to your computer and use it in GitHub Desktop.
import os
import subprocess
import time
import matplotlib.pyplot as plt
import datetime
import random
def set_and_print_env_vars():
env_vars = {
"STORJ_EXP_UPLINK_DOWNLOAD_PREFETCH_FORCE_READS": "false",
"STORJ_EXP_UPLINK_DOWNLOAD_PREFETCH_BYTES_REMAINING": "2621440"
}
for var in env_vars:
print(f"Before setting, {var}: {os.getenv(var, 'Not set')}")
os.environ.update(env_vars)
for var in env_vars:
print(f"After setting, {var}: {os.getenv(var)}")
def download_file(file_path, destination):
start_time = time.time()
log_file_name = f"log{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}.txt"
try:
subprocess.run([
"uplink", "cp", "-p", "11", f"sj://perf/{file_path}", destination,
"--upload-log-file", log_file_name+str(random.randint(0,100))
], check=True)
end_time = time.time()
# Remove the downloaded file
os.remove(destination)
return end_time - start_time
except subprocess.CalledProcessError as e:
print(f"An error occurred during file download: {e}")
return None
except OSError as e:
print(f"An error occurred when removing the file: {e}")
return None
def plot_download_times(download_times):
# Creating a sequence of run numbers starting from 1
run_numbers = range(1, len(download_times) + 1)
# Plotting download times against run numbers
plt.plot(run_numbers, download_times, marker='o', linestyle='-', color='b')
plt.title('Download Time Variation Over Runs')
plt.xlabel('Run Number') # X-axis representing the run number
plt.ylabel('Download Time (s)') # Y-axis representing the download time in seconds
plt.xticks(run_numbers) # Setting x-axis ticks to match run numbers
plt.grid(True) # Adding a grid for better readability
plt.show()
def main():
set_and_print_env_vars()
number_of_downloads = 10
file_path = "test.AVI"
destination_path = "Downloads/{}".format(file_path)
download_times = []
for _ in range(number_of_downloads):
download_time = download_file(file_path, destination_path)
if download_time is not None:
download_times.append(download_time)
print(f"Download took {download_time} seconds.")
else:
print("Download failed.")
if download_times:
plot_download_times(download_times)
else:
print("No successful downloads to plot.")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment