惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

AI
AI
S
Schneier on Security
T
Tenable Blog
A
Arctic Wolf
I
Intezer
博客园 - 司徒正美
A
About on SuperTechFans
The Hacker News
The Hacker News
H
Hacker News: Front Page
Security Archives - TechRepublic
Security Archives - TechRepublic
Attack and Defense Labs
Attack and Defense Labs
Webroot Blog
Webroot Blog
T
The Blog of Author Tim Ferriss
Know Your Adversary
Know Your Adversary
L
Lohrmann on Cybersecurity
D
Docker
T
The Exploit Database - CXSecurity.com
博客园_首页
Microsoft Azure Blog
Microsoft Azure Blog
N
Netflix TechBlog - Medium
H
Help Net Security
Hacker News: Ask HN
Hacker News: Ask HN
Recorded Future
Recorded Future
P
Proofpoint News Feed
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Help Net Security
Help Net Security
L
LINUX DO - 最新话题
AWS News Blog
AWS News Blog
量子位
MyScale Blog
MyScale Blog
博客园 - 聂微东
S
Security @ Cisco Blogs
The Register - Security
The Register - Security
www.infosecurity-magazine.com
www.infosecurity-magazine.com
月光博客
月光博客
V2EX - 技术
V2EX - 技术
T
Troy Hunt's Blog
SecWiki News
SecWiki News
L
LangChain Blog
B
Blog
博客园 - 三生石上(FineUI控件)
Cyberwarzone
Cyberwarzone
H
Heimdal Security Blog
Scott Helme
Scott Helme
O
OpenAI News
B
Blog RSS Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
M
MIT News - Artificial intelligence
Google DeepMind News
Google DeepMind News
雷峰网
雷峰网

又见苍岚

COLMAP PatchMatch Stereo 算法详解 事件驱动的状态机框架:从理论到工程实践 Git 在国内网络环境下无法 Push 的排查与修复 —— 配置 Clash 代理 分段五次多项式插值原理详解 路径插值方法深度对比研究 Claude Code 使用指南 OpenClaw 记忆管理与技能创建指南 CBS(Conflict-Based Search)算法详解 A* 算法及其变种详解 OpenClaw 配置多 Agents Windows Powershell 无法加载文件,因为在此系统上禁止运行脚本问题的解决方案 MaxClaw 安装流程 大模型 AI 名词介绍 AList 网盘聚合工具简介 Claude Code 简介以及 GLM 4.7 模型接入 Github 歌词下载工具 163MusicLyrics Python __getattr__ 懒加载 Python TypedDict 机器人仿真平台 Gazebo 安装记录 机器人仿真平台 Gazebo 简介 多机器人路径规划问题(Multi-Agent Path Finding, MAPF)简介 Python exifread 读取修改过的 jpeg 信息错误问题修复 3D 坐标系变换的理解 3D 旋转矩阵基本概念 MongoDB Compass 介绍 Python 环境管理工具 uv Flutter 开发指南 Snipaste 安装下载与黑屏问题解决方案 全局路径规划算法记录 2025 Python 版本性能测试 Flutter Hello World Flutter 安装环境配置 Ubuntu VMware 硬盘扩容后 SMBus Host controller not enabled 报错问题解决 Python NetworkX 教程 Docker GPU 报错 - Failed to initialize NVML Unknown Error 解决方案 Python matplotlib 图表绘制 cuda-toolkit 安装替代 Cuda 与 Cudnn Jinja2 Python 利用 docxtpl 和 Jinja2 生成基于模板的 Word 文档 Docker 实现 CPU 核心隔离 LoFTR 基于 Transformer 的特征提取匹配算法 OmniGlue 特征匹配 SuperGlue 使用图神经网络学习特征匹配 Ubuntu 下将 xlsx 文件按照 sheet 转换为 图片 Python 使用 SQLAlchemy Python FastAPI 教程 openwrt 软路由配置安装 Nav2 地图文件(PGM/YAML)规范标准 3D OBJ 模型转换为 glb 瓦片格式 Python 源码 Redis 数据库介绍 Ubuntu 22.04 内核自动升级导致 MongoDB 7.0.12 错误记录 ubuntu 20.04 安装 ROS Noetic ubuntu 18.04 安装 ROS Melodic VMware Workstation Pro 个人免费版下载、安装、使用指南 Hybrid A-star 路径规划 Reeds-Shepp 曲线 Dubins 曲线 Linux kvm 虚拟机网络不通的问题解决方法 Ubuntu 自动内存清理 BiliBili 缓存视频转 mp4 Python 求解线性规划 3D Gaussian Splatting 官方源码实践记录 ImageMagick 教程 Ubuntu 22.04 安装 Colmap 对数几率 odds Ubuntu nmcli 网络管理工具使用指南 SuperPoint 自监督深度学习特征点提取 SyncTV Music Tag Web 在线音乐信息整理工具 ncm 格式转 mp3 MusicBrainz 音乐元数据百科数据库 Ubuntu 网络流量监控工具 私人云音乐平台 Navidrome 入门 手眼标定 四元数(Quaternions) OHTTPS 实现免费自动 https 证书申请、更新、部署 ubuntu 22.04 安装 CloudCompare 单机 KVM 虚拟机冷迁移 Ubuntu 22.04 使用 mdadm 实现软 raid 小鱼 一键安装 ROS-humble Fluid -46- 基于 Simpletex API 构建公式识别页面 公式识别 API 简介 -- Simpletex 使用 Python web 部署库 waitress 3D Gaussian Splatting for Real-Time Radiance Field Rendering Ubuntu Swap 简介与空间扩展 Ubuntu 24.04 安装 forticlient Clash Verge 使用 MongoDB 7.0.17 集群 Docker 构建源码 Error code - 2013. Lost connection to MySQL server during query 问题解决 Python 日志记录库 loguru 使用指北 Python 实现 Web 日志查看服务 MySQL LOAD DATA LOCAL INFILE 极速数据加载 Image size exceeds limit of 89478485 pixels 解决方案 Docker 使用 NVIDIA GPU 驱动错误解决 阿里云 docker 镜像仓库 Ubuntu中没有wired connected的解决方案 MinIO 简介 subconverter 代理订阅格式转换 修复 node –openssl-legacy-provider is not allowed in NODE_OPTIONS 错误 Ubuntu 使用 Clash For Linux 客户端
Protobuf 简介与测试
Yiwei Zhang · 2026-01-17 · via 又见苍岚
import json
import xml.etree.ElementTree as ET
import time
import sys
from google.protobuf.json_format import MessageToJson, Parse
from person_pb2 import Person as ProtobufPerson

# 导入protobuf生成类(需先编译person.proto)
# 生成命令:protoc --python_out=. person.proto

# 定义相同的数据结构
sample_data = {
"name": "张三",
"id": 12345,
"email": "zhangsan@example.com",
"phones": [
{"number": "13800138000", "type": "MOBILE"},
{"number": "010-12345678", "type": "WORK"}
],
"address": {
"street": "科技园路123号",
"city": "北京",
"zip_code": "100000"
}
}

def json_serialization_demo():
"""JSON序列化演示"""
print("=== JSON演示 ===")

# 序列化
start = time.perf_counter_ns()
json_str = json.dumps(sample_data, ensure_ascii=False)
json_time = time.perf_counter_ns() - start
json_size = len(json_str.encode('utf-8'))

# 反序列化
start = time.perf_counter_ns()
decoded_data = json.loads(json_str)
json_decode_time = time.perf_counter_ns() - start

print(f"数据大小: {json_size} 字节")
print(f"序列化时间: {json_time} ns")
print(f"反序列化时间: {json_decode_time} ns")
print(f"序列化后数据预览: {json_str[:80]}...")
return json_size, json_time, json_decode_time, json_str

def xml_serialization_demo():
"""XML序列化演示"""
print("\n=== XML演示 ===")

# 创建XML结构
person = ET.Element("person")
ET.SubElement(person, "name").text = sample_data["name"]
ET.SubElement(person, "id").text = str(sample_data["id"])
ET.SubElement(person, "email").text = sample_data["email"]

phones = ET.SubElement(person, "phones")
for phone in sample_data["phones"]:
phone_elem = ET.SubElement(phones, "phone")
ET.SubElement(phone_elem, "number").text = phone["number"]
ET.SubElement(phone_elem, "type").text = phone["type"]

address = ET.SubElement(person, "address")
ET.SubElement(address, "street").text = sample_data["address"]["street"]
ET.SubElement(address, "city").text = sample_data["address"]["city"]
ET.SubElement(address, "zip_code").text = sample_data["address"]["zip_code"]

# 序列化
start = time.perf_counter_ns()
xml_str = ET.tostring(person, encoding='unicode', method='xml')
xml_time = time.perf_counter_ns() - start
xml_size = len(xml_str.encode('utf-8'))

# 反序列化
start = time.perf_counter_ns()
root = ET.fromstring(xml_str)
# 解析XML数据
xml_data = {
"name": root.find("name").text,
"id": int(root.find("id").text),
"email": root.find("email").text,
"phones": [],
"address": {}
}
for phone_elem in root.find("phones").findall("phone"):
xml_data["phones"].append({
"number": phone_elem.find("number").text,
"type": phone_elem.find("type").text
})
address_elem = root.find("address")
xml_data["address"]["street"] = address_elem.find("street").text
xml_data["address"]["city"] = address_elem.find("city").text
xml_data["address"]["zip_code"] = address_elem.find("zip_code").text

xml_decode_time = time.perf_counter_ns() - start

print(f"数据大小: {xml_size} 字节")
print(f"序列化时间: {xml_time} ns")
print(f"反序列化时间: {xml_decode_time} ns")
print(f"序列化后数据预览: {xml_str[:80]}...")
return xml_size, xml_time, xml_decode_time, xml_str

def protobuf_serialization_demo():
"""Protobuf序列化演示"""
print("\n=== Protobuf演示 ===")

# 创建Protobuf对象
person = ProtobufPerson()
person.name = sample_data["name"]
person.id = sample_data["id"]
person.email = sample_data["email"]

# 添加电话
for phone in sample_data["phones"]:
phone_entry = person.phones.add()
phone_entry.number = phone["number"]
if phone["type"] == "MOBILE":
phone_entry.type = ProtobufPerson.PhoneType.MOBILE
else:
phone_entry.type = ProtobufPerson.PhoneType.WORK

# 设置地址
person.address.street = sample_data["address"]["street"]
person.address.city = sample_data["address"]["city"]
person.address.zip_code = sample_data["address"]["zip_code"]

# 序列化
start = time.perf_counter_ns()
binary_data = person.SerializeToString()
pb_time = time.perf_counter_ns() - start
pb_size = len(binary_data)

# 反序列化
start = time.perf_counter_ns()
decoded_person = ProtobufPerson()
decoded_person.ParseFromString(binary_data)
pb_decode_time = time.perf_counter_ns() - start

# 转换为字典以便比较
pb_dict = {
"name": decoded_person.name,
"id": decoded_person.id,
"email": decoded_person.email,
"phones": [
{
"number": phone.number,
"type": ProtobufPerson.PhoneType.Name(phone.type)
}
for phone in decoded_person.phones
],
"address": {
"street": decoded_person.address.street,
"city": decoded_person.address.city,
"zip_code": decoded_person.address.zip_code
}
}

print(f"数据大小: {pb_size} 字节")
print(f"序列化时间: {pb_time} ns")
print(f"反序列化时间: {pb_decode_time} ns")
print(f"序列化后数据预览: 二进制数据,不可直接阅读")

# 将protobuf转换为JSON用于显示
json_from_pb = MessageToJson(decoded_person, preserving_proto_field_name=True)
print(f"转换为JSON后预览: {json_from_pb[:80]}...")

return pb_size, pb_time, pb_decode_time, binary_data

def performance_comparison():
"""性能对比总结"""
print("\n" + "="*60)
print("性能对比总结")
print("="*60)

# 运行所有演示
json_results = json_serialization_demo()
xml_results = xml_serialization_demo()
pb_results = protobuf_serialization_demo()

print("\n" + "="*60)
print("详细对比表")
print("="*60)
print(f"{'格式':<10} | {'大小(字节)':<12} | {'序列化时间(ns)':<16} | {'反序列化时间(ns)':<18} | {'压缩率'}")
print("-"*80)

formats = ["JSON", "XML", "Protobuf"]
results = [json_results, xml_results, pb_results]

for i in range(3):
size, ser_time, deser_time, _ = results[i]
compression_rate = f"{size/json_results[0]*100:.1f}%"
print(f"{formats[i]:<10} | {size:<12} | {ser_time:<16} | {deser_time:<18} | {compression_rate}")

print("\n" + "="*60)
print("Protobuf优势总结")
print("="*60)
print(f"1. 数据大小:比JSON小{json_results[0]/pb_results[0]:.1f}倍,比XML小{xml_results[0]/pb_results[0]:.1f}倍")
print(f"2. 序列化速度:比JSON快{json_results[1]/pb_results[1]:.1f}倍,比XML快{xml_results[1]/pb_results[1]:.1f}倍")
print(f"3. 反序列化速度:比JSON快{json_results[2]/pb_results[2]:.1f}倍,比XML快{xml_results[2]/pb_results[2]:.1f}倍")
print(f"4. 类型安全:有严格的类型检查和编译时验证")
print(f"5. 向后兼容:支持字段的添加和删除而不破坏旧版本")

# 验证数据一致性
print("\n" + "="*60)
print("数据一致性验证")
print("="*60)

# 重新解析所有格式并比较
json_data = json.loads(json_results[3])
root = ET.fromstring(xml_results[3])

# 解析XML数据(简化版)
xml_data = {
"name": root.find("name").text,
"id": int(root.find("id").text),
"email": root.find("email").text
}

decoded_person = ProtobufPerson()
decoded_person.ParseFromString(pb_results[3])
pb_dict = {
"name": decoded_person.name,
"id": decoded_person.id,
"email": decoded_person.email
}

print("所有格式解析出的核心数据一致:")
print(f" JSON: name={json_data['name']}, id={json_data['id']}")
print(f" XML: name={xml_data['name']}, id={xml_data['id']}")
print(f" Proto: name={pb_dict['name']}, id={pb_dict['id']}")

if __name__ == "__main__":
# 首先需要定义并编译person.proto文件
# person.proto内容如下:
"""
syntax = "proto3";

package demo;

message Person {
string name = 1;
int32 id = 2;
string email = 3;

message PhoneNumber {
string number = 1;
PhoneType type = 2;
}

repeated PhoneNumber phones = 4;

message Address {
string street = 1;
string city = 2;
string zip_code = 3;
}

Address address = 5;
}

enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}
"""

print("注意:运行此示例前,请确保:")
print("1. 已安装protobuf: pip install protobuf")
print("2. 已创建person.proto文件")
print("3. 已编译proto文件: protoc --python_out=. person.proto")
print("4. person_pb2.py文件已存在\n")

try:
performance_comparison()
except ImportError as e:
print(f"错误:{e}")
print("请确保已正确编译person.proto文件")
except Exception as e:
print(f"运行时错误:{e}")