|
| 1 | +import sys |
| 2 | +import time |
| 3 | +import subprocess |
| 4 | + |
| 5 | +if len(sys.argv) != 6: |
| 6 | + print("Usage: python fixed_interval_curl.py NUM_META_ITER NUM_ITER FIXED_INTERVAL_SEC SLEEP_BETWEEN_META_ITER URL", file=sys.stderr) |
| 7 | + sys.exit(1) |
| 8 | + |
| 9 | + |
| 10 | +try: |
| 11 | + num_meta_iter = int(sys.argv[1]) |
| 12 | + num_iter = int(sys.argv[2]) |
| 13 | + fixed_interval = float(sys.argv[3]) |
| 14 | + sleep_between_iterations = float(sys.argv[4]) |
| 15 | + url = sys.argv[5] |
| 16 | +except ValueError as e: |
| 17 | + print(f"Error parsing numerical arguments: {e}", file=sys.stderr) |
| 18 | + sys.exit(1) |
| 19 | + |
| 20 | + |
| 21 | +def format_timestamp(timestamp: float) -> str: |
| 22 | + time_str = time.strftime("%H:%M:%S", time.localtime(timestamp)) |
| 23 | + milliseconds = f"{timestamp % 1:.3f}"[2:] |
| 24 | + return f"{time_str}.{milliseconds}" |
| 25 | + |
| 26 | + |
| 27 | +def run_curl(target_url: str): |
| 28 | + start_time = time.time() |
| 29 | + try: |
| 30 | + subprocess.run(['curl', '-I', '-s', target_url], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) |
| 31 | + except subprocess.CalledProcessError as e: |
| 32 | + print(f"Curl failed with exit code {e.returncode}: {e}", file=sys.stderr) |
| 33 | + sys.exit(e.returncode) |
| 34 | + end_time = time.time() |
| 35 | + duration = end_time - start_time |
| 36 | + |
| 37 | + print(f"Curl timing: start=[{format_timestamp(start_time)}], end=[{format_timestamp(end_time)}], duration={duration:.3f}s", flush=True) |
| 38 | + |
| 39 | + |
| 40 | +i = 0 |
| 41 | +while i < num_meta_iter: |
| 42 | + j = 0 |
| 43 | + start_time = time.time() |
| 44 | + while j < num_iter: |
| 45 | + next_execution_time = start_time + (j * fixed_interval) |
| 46 | + current_time = time.time() |
| 47 | + sleep_duration = next_execution_time - current_time |
| 48 | + if sleep_duration > 0: |
| 49 | + time.sleep(sleep_duration) |
| 50 | + else: |
| 51 | + pass |
| 52 | + execution_time = time.time() |
| 53 | + time_str = time.strftime("%H:%M:%S", time.localtime(execution_time)) |
| 54 | + milliseconds = f"{execution_time % 1:.3f}"[2:] |
| 55 | + print(f"[{time_str}.{milliseconds}] Executing curl (Meta: {i + 1}/{num_meta_iter}, Iter: {j + 1}/{num_iter})") |
| 56 | + run_curl(url) |
| 57 | + j += 1 |
| 58 | + if sleep_between_iterations > 0: |
| 59 | + time.sleep(sleep_between_iterations) |
| 60 | + i += 1 |
| 61 | +print("Script finished successfully.") |
| 62 | +print("Sleeping for an additional 300s") |
| 63 | +time.sleep(300) |
0 commit comments