import time
from typing import List, Dict, Any
try:
import rat_quickmem_py as rat_quickmem
except ImportError:
print("❌ 请先编译并安装 rat_quickmem_py Python 模块")
print(" 运行: cd ../python && maturin develop --release")
exit(1)
def basic_example():
print("\n📝 基本编码解码示例:")
test_data = {
"id": 12345,
"name": "Python测试数据",
"values": [1.1, 2.2, 3.3, 4.4, 5.5],
"metadata": {
"type": "benchmark",
"version": "1.0",
"language": "python"
},
"active": True,
"tags": ["test", "benchmark", "python"]
}
start_time = time.time()
encoded = rat_quickmem.encode(test_data)
encode_time = time.time() - start_time
print(f"✅ bincode 编码完成: {len(encoded)} 字节, 耗时: {encode_time*1000:.3f}ms")
start_time = time.time()
decoded = rat_quickmem.decode(encoded)
decode_time = time.time() - start_time
print(f"✅ bincode 解码完成, 耗时: {decode_time*1000:.3f}ms")
print(f"✅ 数据验证: {'通过' if test_data == decoded else '失败'}")
def security_example():
print("\n🔒 安全防护功能演示:")
print("\n1️⃣ 测试批量数量限制:")
try:
large_batch = [f"item_{i}" for i in range(15000)]
rat_quickmem.encode_batch(large_batch)
print("❌ 应该触发批量数量限制错误")
except ValueError as e:
print(f"✅ 成功拦截批量数量攻击: {e}")
print("\n2️⃣ 测试数据大小限制:")
try:
huge_data = "x" * (200 * 1024 * 1024) rat_quickmem.encode(huge_data)
print("❌ 应该触发数据大小限制错误")
except ValueError as e:
print(f"✅ 成功拦截超大数据攻击: {e}")
print("\n3️⃣ 正常数据处理:")
try:
normal_data = {"message": "这是正常大小的数据", "count": 100}
encoded = rat_quickmem.encode(normal_data)
decoded = rat_quickmem.decode(encoded)
print(f"✅ 正常数据处理成功: {len(encoded)} 字节")
print(f"✅ 数据验证: {'通过' if normal_data == decoded else '失败'}")
except Exception as e:
print(f"❌ 正常数据处理失败: {e}")
print("\n4️⃣ 安全批量操作:")
try:
safe_batch = [{"id": i, "data": f"item_{i}"} for i in range(100)]
encoded_batch = rat_quickmem.encode_batch(safe_batch)
decoded_batch = rat_quickmem.decode_batch(encoded_batch)
print(f"✅ 安全批量操作成功: {len(safe_batch)} 项")
print(f"✅ 批量数据验证: {'通过' if safe_batch == decoded_batch else '失败'}")
except Exception as e:
print(f"❌ 批量操作失败: {e}")
return encoded, decoded
def codec_class_example():
print("\n🔧 编码解码器类示例:")
codec = rat_quickmem.QuickCodec()
test_data = {
"message": "Hello from Python!",
"timestamp": time.time(),
"data": list(range(100))
}
encoded = codec.encode(test_data)
print(f"📦 编码器 bincode 编码: {len(encoded)} 字节")
decoded = codec.decode(encoded)
print(f"📦 解码器 bincode 解码: {'成功' if decoded == test_data else '失败'}")
roundtrip_result = codec.roundtrip(test_data)
print(f"🔄 bincode 往返测试: {'通过' if roundtrip_result == test_data else '失败'}")
def batch_processing_example():
print("\n📦 批量处理示例:")
test_data_list = []
for i in range(1000):
data = {
"id": i,
"name": f"测试数据_{i}",
"values": [j * 0.1 for j in range(i % 10 + 1)],
"metadata": {
"index": i,
"category": f"cat_{i % 5}"
}
}
test_data_list.append(data)
start_time = time.time()
batch_encoded = rat_quickmem.encode_batch(test_data_list)
batch_encode_time = time.time() - start_time
print(f"📊 批量 bincode 编码 {len(test_data_list)} 个对象:")
print(f" 耗时: {batch_encode_time*1000:.3f}ms")
total_size = sum(len(encoded) for encoded in batch_encoded)
print(f" 大小: {total_size/1024:.2f} KB")
print(f" 速度: {total_size/1024/1024/batch_encode_time:.2f} MB/s")
start_time = time.time()
batch_decoded = rat_quickmem.decode_batch(batch_encoded)
batch_decode_time = time.time() - start_time
print(f"📊 批量 bincode 解码:")
print(f" 耗时: {batch_decode_time*1000:.3f}ms")
print(f" 速度: {total_size/1024/1024/batch_decode_time:.2f} MB/s")
print(f" 验证: {'通过' if test_data_list == batch_decoded else '失败'}")
def bincode_showcase():
print("\n🎯 RatQuickMem 核心优势展示:")
print(" 专为 Rust ↔ Python 高性能数据交换设计")
print(" 使用 bincode 二进制协议,内存池优化")
structured_data = {
"message_id": 12345,
"timestamp": int(time.time() * 1000), "payload": {
"sensor_readings": [
{"id": i, "value": i * 3.14159, "status": i % 3}
for i in range(1000) ],
"metadata": {
"device_id": "sensor_array_001",
"location": [116.3974, 39.9093], "batch_size": 1000
}
}
}
print(f"\n📦 测试数据: {len(structured_data['payload']['sensor_readings'])} 个传感器读数")
for _ in range(3):
rat_quickmem.encode({"warmup": True})
iterations = 20
batch_data = [structured_data for _ in range(10)]
print(f"\n🚀 批量处理测试 ({len(batch_data)} 个消息, 平均 {iterations} 次):")
encode_times = []
decode_times = []
total_data_size = 0
for _ in range(iterations):
start_time = time.time()
encoded_batch = rat_quickmem.encode_batch(batch_data)
encode_times.append(time.time() - start_time)
if total_data_size == 0:
total_data_size = sum(len(encoded) for encoded in encoded_batch)
start_time = time.time()
decoded_batch = rat_quickmem.decode_batch(encoded_batch)
decode_times.append(time.time() - start_time)
avg_encode = sum(encode_times) / iterations
avg_decode = sum(decode_times) / iterations
throughput_encode = total_data_size / avg_encode / 1024 / 1024 throughput_decode = total_data_size / avg_decode / 1024 / 1024
print(f"📊 批量编码性能:")
print(f" 平均耗时: {avg_encode*1000:.3f}ms")
print(f" 数据大小: {total_data_size/1024:.2f} KB ({len(batch_data)} 个复杂消息)")
print(f" 吞吐量: {throughput_encode:.2f} MB/s")
print(f"📊 批量解码性能:")
print(f" 平均耗时: {avg_decode*1000:.3f}ms")
print(f" 吞吐量: {throughput_decode:.2f} MB/s")
print(f"\n🏊 内存池效率测试:")
initial_stats = rat_quickmem.get_pool_stats()
small_messages = [{"id": i, "value": i} for i in range(1000)]
start_time = time.time()
for msg in small_messages:
rat_quickmem.encode(msg)
pool_test_time = time.time() - start_time
final_stats = rat_quickmem.get_pool_stats()
print(f" 处理 {len(small_messages)} 个小消息: {pool_test_time*1000:.3f}ms")
print(f" 平均每消息: {pool_test_time/len(small_messages)*1000000:.2f}μs")
if final_stats.total_buffers > 0:
reuse_efficiency = min(100.0, (final_stats.total_buffers / max(1, len(small_messages) // 10)) * 100)
print(f" 内存池效率: {reuse_efficiency:.1f}% (池中缓冲区: {final_stats.total_buffers})")
else:
print(f" 内存池效率: 0.0% (无缓冲区复用)")
print(f"\n💡 适用场景分析:")
print(f" ✅ RatQuickMem 优势场景:")
print(f" • Rust ↔ Python 数据交换")
print(f" • 大批量结构化数据传输")
print(f" • 内存敏感的高频操作")
print(f" • 网络传输(数据更紧凑)")
print(f" ⚠️ JSON 更适合:")
print(f" • 人类可读的配置文件")
print(f" • Web API 接口")
print(f" • 调试和开发阶段")
print(f"\n🎯 总结: RatQuickMem 专注于高性能二进制数据交换,不是 JSON 的替代品")
def pool_management_example():
print("\n🏊 内存池管理示例:")
initial_stats = rat_quickmem.get_pool_stats()
print(f"📈 初始池统计:")
print(f" 小缓冲区: {initial_stats.small_buffers}")
print(f" 中等缓冲区: {initial_stats.medium_buffers}")
print(f" 大缓冲区: {initial_stats.large_buffers}")
print(f" 总缓冲区: {initial_stats.total_buffers}")
print("🔥 预热内存池...")
for i in range(10):
test_data = {"warmup": i, "data": "x" * 100}
encoded = rat_quickmem.encode(test_data)
decoded = rat_quickmem.decode(encoded)
warmed_stats = rat_quickmem.get_pool_stats()
print(f"📈 预热后统计:")
print(f" 小缓冲区: {warmed_stats.small_buffers}")
print(f" 中等缓冲区: {warmed_stats.medium_buffers}")
print(f" 大缓冲区: {warmed_stats.large_buffers}")
print(f" 总缓冲区: {warmed_stats.total_buffers}")
encoder = rat_quickmem.QuickEncoder()
test_data = [{"id": i, "data": list(range(100))} for i in range(50)]
start_time = time.time()
for data in test_data:
encoder.encode(data)
operation_time = time.time() - start_time
after_stats = rat_quickmem.get_pool_stats()
print(f"📈 操作后统计:")
print(f" 小缓冲区: {after_stats.small_buffers}")
print(f" 中等缓冲区: {after_stats.medium_buffers}")
print(f" 大缓冲区: {after_stats.large_buffers}")
print(f" 总缓冲区: {after_stats.total_buffers}")
print(f" 操作耗时: {operation_time*1000:.3f}ms")
def bytes_handling_example():
print("\n🔢 字节数据处理示例:")
raw_data = b"\x00\x01\x02\x03" * 1000
start_time = time.time()
encoded_bytes = rat_quickmem.encode(raw_data)
encode_time = time.time() - start_time
print(f"📦 字节编码:")
print(f" 原始大小: {len(raw_data)} 字节")
print(f" 编码大小: {len(encoded_bytes)} 字节")
print(f" 编码耗时: {encode_time*1000:.3f}ms")
start_time = time.time()
decoded_bytes = rat_quickmem.decode(encoded_bytes)
decode_time = time.time() - start_time
print(f"📦 字节解码:")
print(f" 解码耗时: {decode_time*1000:.3f}ms")
print(f" 数据验证: {'通过' if raw_data == decoded_bytes else '失败'}")
def main():
print("🚀 RatQuickMem Python 绑定示例")
print("=" * 50)
try:
basic_example()
codec_class_example()
batch_processing_example()
bincode_showcase()
pool_management_example()
bytes_handling_example()
security_example()
print("\n🎉 所有示例运行完成!")
except Exception as e:
print(f"❌ 运行出错: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
main()