CCF/tests/vegeta_stress.py

117 строки
3.7 KiB
Python
Исходник Обычный вид История

2020-10-15 18:57:56 +03:00
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the Apache 2.0 License.
import infra.network
import infra.e2e_args
import subprocess
import threading
import time
2020-10-19 18:29:35 +03:00
import generate_vegeta_targets as TargetGenerator
2020-10-15 18:57:56 +03:00
from loguru import logger as LOG
VEGETA_BIN = "/opt/vegeta/vegeta"
def print_memory_stats(node, shutdown_event):
with node.client() as c:
while not shutdown_event.is_set():
r = c.get("/node/memory")
LOG.warning(r.body.json())
time.sleep(10)
def run(args, additional_attack_args):
# Test that vegeta is available
subprocess.run([VEGETA_BIN, "-version"], capture_output=True, check=True)
with infra.network.network(
2020-10-22 16:54:43 +03:00
args.nodes,
2020-10-15 18:57:56 +03:00
args.binary_dir,
args.debug_nodes,
args.perf_nodes,
pdb=args.pdb,
) as network:
network.start_and_open(args)
2020-10-15 18:57:56 +03:00
primary, _ = network.find_primary()
primary_hostname = (
f"{primary.get_public_rpc_host()}:{primary.get_public_rpc_port()}"
)
2020-10-15 18:57:56 +03:00
vegeta_targets = "vegeta_targets"
2021-08-23 12:59:42 +03:00
with open(vegeta_targets, "w", encoding="utf-8") as f:
2020-10-15 18:57:56 +03:00
for i in range(10):
2020-10-19 18:29:35 +03:00
TargetGenerator.write_vegeta_target_line(
2020-10-15 18:57:56 +03:00
f,
2020-10-19 18:29:35 +03:00
primary_hostname,
2020-10-15 18:57:56 +03:00
"/app/log/private",
body={"id": i, "msg": f"Private message: {i}"},
)
for i in range(10):
2020-10-19 18:29:35 +03:00
TargetGenerator.write_vegeta_target_line(
f, primary_hostname, f"/app/log/private?id={i}", method="GET"
2020-10-15 18:57:56 +03:00
)
for i in range(10):
2020-10-19 18:29:35 +03:00
TargetGenerator.write_vegeta_target_line(
2020-10-15 18:57:56 +03:00
f,
2020-10-19 18:29:35 +03:00
primary_hostname,
2020-10-15 18:57:56 +03:00
"/app/log/public",
body={"id": i, "msg": f"Public message: {i}"},
)
for i in range(10):
2020-10-19 18:29:35 +03:00
TargetGenerator.write_vegeta_target_line(
f, primary_hostname, f"/app/log/public?id={i}", method="GET"
2020-10-15 18:57:56 +03:00
)
attack_cmd = [VEGETA_BIN, "attack"]
attack_cmd += ["--targets", vegeta_targets]
attack_cmd += ["--format", "json"]
attack_cmd += ["--duration", "10s"]
sa = primary.session_auth("user0")
attack_cmd += ["--cert", sa["session_auth"].cert]
attack_cmd += ["--key", sa["session_auth"].key]
attack_cmd += ["--root-certs", primary.session_ca(False)["ca"]]
2020-10-15 18:57:56 +03:00
attack_cmd += additional_attack_args
attack_cmd_s = " ".join(attack_cmd)
LOG.warning(f"Starting: {attack_cmd_s}")
vegeta_run = subprocess.Popen(attack_cmd, stdout=subprocess.PIPE)
tee_split = subprocess.Popen(
["tee", "vegeta_results.bin"],
stdin=vegeta_run.stdout,
stdout=subprocess.PIPE,
)
report_cmd = [VEGETA_BIN, "report", "--every", "5s"]
vegeta_report = subprocess.Popen(report_cmd, stdin=tee_split.stdout)
# Start a second thread which will print the primary's memory stats at regular intervals
shutdown_event = threading.Event()
memory_thread = threading.Thread(
target=print_memory_stats, args=(primary, shutdown_event)
)
memory_thread.start()
LOG.info("Waiting for completion...")
vegeta_report.communicate()
LOG.info("Shutting down...")
shutdown_event.set()
memory_thread.join()
LOG.success("Done!")
if __name__ == "__main__":
def add(parser):
pass
args, unknown_args = infra.e2e_args.cli_args(add=add, accept_unknown=True)
2021-09-17 11:51:25 +03:00
args.package = "samples/apps/logging/liblogging"
2020-10-22 16:54:43 +03:00
args.nodes = infra.e2e_args.min_nodes(args, f=1)
2020-10-15 18:57:56 +03:00
run(args, unknown_args)