forked from mtusi2026/mtusi2026lab7
test(pki): measure container CPU and memory during lifecycle load
This commit is contained in:
@@ -6,6 +6,7 @@ import json
|
||||
import math
|
||||
import socket
|
||||
import ssl
|
||||
import threading
|
||||
import time
|
||||
from collections import Counter
|
||||
from datetime import UTC, datetime
|
||||
@@ -50,6 +51,8 @@ def main():
|
||||
}
|
||||
started = time.time()
|
||||
load = None
|
||||
resource_stop = threading.Event()
|
||||
resource_thread = None
|
||||
lb_stopped = False
|
||||
bao_sealed = False
|
||||
with httpx.Client(
|
||||
@@ -93,6 +96,66 @@ def main():
|
||||
chat = r.json()["id"]
|
||||
load = Workload(client, headers, chat)
|
||||
load.start()
|
||||
resource_ids = compose(
|
||||
"ps", "-q", "api", "worker", "otel", "openbao"
|
||||
).split()
|
||||
report["resource_limits"] = [
|
||||
{
|
||||
"role": info["Config"]["Labels"][
|
||||
"com.docker.compose.service"
|
||||
],
|
||||
"container": info["Name"],
|
||||
"cpu_nano": info["HostConfig"]["NanoCpus"],
|
||||
"memory_bytes": info["HostConfig"]["Memory"],
|
||||
}
|
||||
for info in json.loads(
|
||||
command(["docker", "inspect", *resource_ids])
|
||||
)
|
||||
]
|
||||
report["resource_measurement"] = (
|
||||
"Docker stats snapshots: 15-second delay plus command duration; "
|
||||
"CPU 100% is one core; memory is sampled usage, not an absolute peak"
|
||||
)
|
||||
report["resource_samples"] = []
|
||||
|
||||
def sample_resources():
|
||||
while not resource_stop.is_set():
|
||||
began = time.time()
|
||||
try:
|
||||
raw = command(
|
||||
[
|
||||
"docker",
|
||||
"stats",
|
||||
"--no-stream",
|
||||
"--format",
|
||||
"json",
|
||||
*resource_ids,
|
||||
]
|
||||
)
|
||||
observations = [
|
||||
json.loads(line) for line in raw.splitlines()
|
||||
]
|
||||
report["resource_samples"].append(
|
||||
{
|
||||
"started_at": began,
|
||||
"ended_at": time.time(),
|
||||
"phase": load.phase,
|
||||
"containers": observations,
|
||||
}
|
||||
)
|
||||
except Exception as error:
|
||||
report.setdefault(
|
||||
"resource_sampling_errors", []
|
||||
).append(
|
||||
{
|
||||
"at": began,
|
||||
"error_type": type(error).__name__,
|
||||
}
|
||||
)
|
||||
resource_stop.wait(15)
|
||||
|
||||
resource_thread = threading.Thread(target=sample_resources)
|
||||
resource_thread.start()
|
||||
|
||||
def renewal():
|
||||
before = pki.metadata()
|
||||
@@ -308,6 +371,11 @@ def main():
|
||||
report["failure_type"] = type(error).__name__
|
||||
raise
|
||||
finally:
|
||||
resource_stop.set()
|
||||
if resource_thread is not None:
|
||||
resource_thread.join(15)
|
||||
if resource_thread.is_alive():
|
||||
report["resource_sampler_did_not_stop"] = True
|
||||
if load is not None:
|
||||
load.close()
|
||||
report["request_statuses"] = dict(
|
||||
|
||||
Reference in New Issue
Block a user