Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 81a8573e51 |
33
demo/client.py
Normal file
33
demo/client.py
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
"""TCP 单次发送客户端:发送 3 条测试消息,验证基本连通性"""
|
||||||
|
|
||||||
|
import socket
|
||||||
|
import time
|
||||||
|
|
||||||
|
HOST = "127.0.0.1"
|
||||||
|
PORT = 8888
|
||||||
|
MESSAGE_COUNT = 3
|
||||||
|
|
||||||
|
|
||||||
|
def send_once(index: int) -> bool:
|
||||||
|
try:
|
||||||
|
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
client.settimeout(5)
|
||||||
|
client.connect((HOST, PORT))
|
||||||
|
msg = f"test_message_{index}_{time.time()}"
|
||||||
|
client.send(msg.encode("utf-8"))
|
||||||
|
reply = client.recv(1024)
|
||||||
|
client.close()
|
||||||
|
print(f"消息 {index} 发送成功,回复: {reply.decode('utf-8', errors='replace')}")
|
||||||
|
return True
|
||||||
|
except Exception as exc:
|
||||||
|
print(f"消息 {index} 失败: {exc}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
print(f"开始连通性测试: 发送 {MESSAGE_COUNT} 条消息到 {HOST}:{PORT}")
|
||||||
|
success = 0
|
||||||
|
for i in range(MESSAGE_COUNT):
|
||||||
|
if send_once(i):
|
||||||
|
success += 1
|
||||||
|
print(f"完成: 成功 {success}/{MESSAGE_COUNT}")
|
||||||
48
demo/server.py
Normal file
48
demo/server.py
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
"""TCP 文本写入服务端(实验大纲示例应用)"""
|
||||||
|
|
||||||
|
import os
|
||||||
|
import socket
|
||||||
|
|
||||||
|
HOST = "0.0.0.0"
|
||||||
|
PORT = 8888
|
||||||
|
SAVE_PATH = "./data.txt"
|
||||||
|
|
||||||
|
if not os.path.exists(os.path.dirname(SAVE_PATH)) and os.path.dirname(SAVE_PATH):
|
||||||
|
os.makedirs(os.path.dirname(SAVE_PATH))
|
||||||
|
|
||||||
|
|
||||||
|
def save_to_file(content):
|
||||||
|
"""文本追加写入本地磁盘"""
|
||||||
|
with open(SAVE_PATH, "a", encoding="utf-8") as f:
|
||||||
|
f.write(content + "\n")
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||||
|
server_socket.bind((HOST, PORT))
|
||||||
|
server_socket.listen(5)
|
||||||
|
print(f"TCP 写入服务已启动,监听端口:{PORT}")
|
||||||
|
print(f"数据保存路径:{os.path.abspath(SAVE_PATH)}")
|
||||||
|
|
||||||
|
while True:
|
||||||
|
conn, addr = server_socket.accept()
|
||||||
|
print(f"\n客户端已连接:{addr}")
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
data = conn.recv(1024)
|
||||||
|
if not data:
|
||||||
|
break
|
||||||
|
text = data.decode("utf-8").strip()
|
||||||
|
print(f"收到数据:{text}")
|
||||||
|
save_to_file(text)
|
||||||
|
conn.send("写入成功,数据已保存到磁盘".encode("utf-8"))
|
||||||
|
except Exception as e:
|
||||||
|
print(f"连接异常:{e}")
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
print("客户端连接断开")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
47
demo/stress_client.py
Normal file
47
demo/stress_client.py
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
"""TCP 压力测试客户端:模拟峰值负载,触发大量系统调用"""
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import socket
|
||||||
|
import time
|
||||||
|
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||||
|
|
||||||
|
HOST = "127.0.0.1"
|
||||||
|
PORT = 8888
|
||||||
|
|
||||||
|
|
||||||
|
def send_once(index: int) -> bool:
|
||||||
|
try:
|
||||||
|
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
client.settimeout(5)
|
||||||
|
client.connect((HOST, PORT))
|
||||||
|
msg = f"stress_test_message_{index}_{time.time()}"
|
||||||
|
client.send(msg.encode("utf-8"))
|
||||||
|
client.recv(1024)
|
||||||
|
client.close()
|
||||||
|
return True
|
||||||
|
except Exception as exc:
|
||||||
|
print(f"请求 {index} 失败: {exc}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def run_stress(total: int, workers: int):
|
||||||
|
print(f"开始压力测试: {total} 次请求, {workers} 并发")
|
||||||
|
start = time.time()
|
||||||
|
success = 0
|
||||||
|
|
||||||
|
with ThreadPoolExecutor(max_workers=workers) as pool:
|
||||||
|
futures = [pool.submit(send_once, i) for i in range(total)]
|
||||||
|
for future in as_completed(futures):
|
||||||
|
if future.result():
|
||||||
|
success += 1
|
||||||
|
|
||||||
|
elapsed = time.time() - start
|
||||||
|
print(f"完成: 成功 {success}/{total}, 耗时 {elapsed:.2f}s, QPS={success/elapsed:.1f}")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
parser = argparse.ArgumentParser(description="TCP 压力测试客户端")
|
||||||
|
parser.add_argument("-n", "--count", type=int, default=200, help="总请求数")
|
||||||
|
parser.add_argument("-w", "--workers", type=int, default=20, help="并发线程数")
|
||||||
|
args = parser.parse_args()
|
||||||
|
run_stress(args.count, args.workers)
|
||||||
22
hello.py
Normal file
22
hello.py
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
"""基础验证:eBPF kprobe Hello World 程序(实验大纲 基础验证部分)"""
|
||||||
|
|
||||||
|
from bcc import BPF
|
||||||
|
|
||||||
|
prog = """
|
||||||
|
int hello(void *ctx) {
|
||||||
|
bpf_trace_printk("Hello, World!\\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
|
b = BPF(text=prog)
|
||||||
|
execve_function = b.get_syscall_fnname("execve")
|
||||||
|
b.attach_kprobe(event=execve_function, fn_name="hello")
|
||||||
|
|
||||||
|
print("%-18s %-16s %-6s %s" % ("TIME(s)", "COMM", "PID", "MESSAGE"))
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
task, pid, cpu, flags, ts, msg = b.trace_fields()
|
||||||
|
except ValueError:
|
||||||
|
continue
|
||||||
|
print("%-18.9f %-16s %-6d %s" % (ts, task, pid, msg))
|
||||||
Reference in New Issue
Block a user