Skip to content

Commit d759b32

Browse files
Repeat curls at equal intervals (#2641)
1 parent 3850f0c commit d759b32

File tree

5 files changed

+71
-30
lines changed

5 files changed

+71
-30
lines changed

integration-tests/container/QA_TAG

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.0.3
1+
2.0.4
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
FROM alpine:3.8
22

33
RUN apk add --update curl && \
4+
apk add python3 && \
45
rm -rf /var/cache/apk/*
56

6-
COPY schedule-curls.sh /usr/bin/schedule-curls.sh
7+
COPY schedule-curls.py /usr/bin/schedule-curls.py
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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)

integration-tests/container/schedule-curls/schedule-curls.sh

Lines changed: 0 additions & 22 deletions
This file was deleted.

integration-tests/suites/repeated_network_flow.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,6 @@ func (s *RepeatedNetworkFlowTestSuite) SetupSuite() {
8282
s.Require().NoError(err)
8383
s.ServerContainer = containerID[0:12]
8484

85-
// invokes another container
86-
containerID, err = s.Executor().StartContainer(config.ContainerStartConfig{Name: "nginx-curl", Image: scheduled_curls_image, Command: []string{"sleep", "300"}})
87-
s.Require().NoError(err)
88-
s.ClientContainer = containerID[0:12]
89-
9085
s.ServerIP, err = s.getIPAddress("nginx")
9186
s.Require().NoError(err)
9287

@@ -100,7 +95,11 @@ func (s *RepeatedNetworkFlowTestSuite) SetupSuite() {
10095
numIter := strconv.Itoa(s.NumIter)
10196
sleepBetweenCurlTime := strconv.Itoa(s.SleepBetweenCurlTime)
10297
sleepBetweenIterations := strconv.Itoa(s.SleepBetweenIterations)
103-
_, err = s.execContainer("nginx-curl", []string{"/usr/bin/schedule-curls.sh", numMetaIter, numIter, sleepBetweenCurlTime, sleepBetweenIterations, serverAddress}, false)
98+
99+
// Invokes the client container and runs the curls
100+
containerID, err = s.Executor().StartContainer(config.ContainerStartConfig{Name: "nginx-curl", Image: scheduled_curls_image, Command: []string{"python3", "/usr/bin/schedule-curls.py", numMetaIter, numIter, sleepBetweenCurlTime, sleepBetweenIterations, serverAddress}})
101+
s.Require().NoError(err)
102+
s.ClientContainer = containerID[0:12]
104103

105104
s.ClientIP, err = s.getIPAddress("nginx-curl")
106105
s.Require().NoError(err)

0 commit comments

Comments
 (0)