.Net中使用protobuf序列化:Google.Protobuf vs protobuf-net

旧地址:https://www.shisujie.com/blog/Google-ProtoBuf-vs-ProtoBuf-Net

Protocol Buffers - Google官方教程文档
示例源代码

什么是protobuf?

protobuf全称Protocol Buffers,由Google推出的一种平台、语言无关的数据交互格式。

为什么用protobuf

由于公司项目开发中,使用了WCF,其默认数据序列化是DataContractSerializer,采用xml格式,考虑到性能提升,于是采用了protobuf。

比较选择

protobuf的.net实现主要有两个版本:

  • Google.Protobuf Google.Protobuf
    • Google官方维护,项目前身是protobuf-csharp-port
    • 优势:在多平台协作开发时,定义一份协议,可以在各个语言中共用。
  • protobuf-net protobuf-net
    • 社区维护,主要由Marc Gravell管理维护
    • 优势:相对来说,更符合.net开发习惯;对于纯粹的.NET程序,使用起来更加方便

Google.ProtoBuf 如何使用

  • 安装Nuget包:Google.Protobuf和Google.Protobuf.Tools
1
2
3
4
5
6
7
8
9
10
11

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Google.Protobuf" Version="3.12.3" />
<PackageReference Include="Google.Protobuf.Tools" Version="3.12.3" />
</ItemGroup>
</Project>

  • 定义proto协议描述文件(.proto)

类似于.net中的类结构描述

1
2
3
4
5
6
7
8
9
10

syntax = "proto3";

package Jess.Sample.GoogleProtoBuf;

message Test {
int32 ID = 1;
string Name = 2;
}

  • 基于上面定义的proto文件,生成相应的C#类文件

命令行中执行如下代码:

1
2
3

<自己的protoc.exe文件目录>protoc.exe --proto_path=<proto文件所在目录> --csharp_out=<proto生成的cs文件目录> <定义的proto文件名>

或者参考如下powershell脚本内容

将脚本内容保存到项目目录下,直接执行ps1文件。

1
2
3
4
5
6
7
8
9
10

# 跳转到当前powershell脚本文件目录
$scriptpath = $PSScriptRoot
cd $scriptpath

# 获取用户目录
$userpath=$env:USERPROFILE
# 执行protoc生成类
&$userpath'\.nuget\packages\google.protobuf.tools\3.12.3\tools\windows_x64\protoc.exe' --proto_path=./protofile --csharp_out=./protoclass test.proto

  • 编写序列化相关代码
1
2
3
4
5
6
7
8
9
10
11
12
13

public void SimplestUse()
{
Test test_forProtobuf = new Test();
test_forProtobuf.ID = 2;
test_forProtobuf.Name = "测试 Google.ProtoBuf";

using (MemoryStream memoryStream = new MemoryStream())
{
test_forProtobuf.WriteTo(memoryStream);
}
}

更多使用示例,见本文开头源码链接。


protobuf-net 如何使用

  • 安装Nuget包:protobuf-net
1
2
3
4
5
6
7
8
9
10

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="protobuf-net" Version="3.0.29" />
</ItemGroup>
</Project>

  • 编写待序列化的类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

using ProtoBuf;

namespace Jess.Sample.ProtoBufNet
{
[ProtoContract]
public class Test
{
[ProtoMember(1)]
public int ID { get; set; }

[ProtoMember(2)]
public string Name { get; set; }
}
}

  • 实现序列化方法
1
2
3
4
5
6
7
8
9
10
11
12
13

public void SimplestUse()
{
Test test_forProtobuf = new Test();
test_forProtobuf.ID = 1;
test_forProtobuf.Name = "测试 protobuf-net";

using (MemoryStream memoryStream = new MemoryStream())
{
ProtoBuf.Serializer.Serialize(memoryStream, test_forProtobuf);
}
}

更多使用示例,见本文开头源码链接。

Google.ProtoBuf 和 protobuf-net 性能比较

性能测试工具使用:BenchmarkDotNet

  • MemoryStream序列化

直接传入MemoryStream的WriteTo扩展方法,其实是官方内部封装了CodedOutputStream —— 从性能数据上看,也是无差别的。

1
2
3
4
5
6
7

BenchmarkDotNet=v0.12.1, OS=Windows 10.0.18362.900 (1903/May2019Update/19H1)
Intel Core i7-7700 CPU 3.60GHz (Kaby Lake), 1 CPU, 8 logical and 4 physical cores
[Host] : .NET Framework 4.8 (4.8.4180.0), X86 LegacyJIT

Toolchain=InProcessEmitToolchain

Method Mean Error StdDev Median Rank
Google.ProtoBuf使用MemoryStream 789.7 ns 15.72 ns 46.36 ns 775.3 ns 2
Google.ProtoBuf使用MemoryStream嵌套CodedOutputStream 799.6 ns 16.14 ns 46.81 ns 791.6 ns 2
protobuf-net使用MemoryStream 491.7 ns 7.13 ns 6.32 ns 488.8 ns 1
  • FileStream序列化
1
2
3
4
5
6
7

BenchmarkDotNet=v0.12.1, OS=Windows 10.0.18362.900 (1903/May2019Update/19H1)
Intel Core i7-7700 CPU 3.60GHz (Kaby Lake), 1 CPU, 8 logical and 4 physical cores
[Host] : .NET Framework 4.8 (4.8.4180.0), X86 LegacyJIT

Toolchain=InProcessEmitToolchain

Method Mean Error StdDev Rank
Google.ProtoBuf使用FileStream 460.3 μs 14.65 μs 43.18 μs 1
protobuf-net使用FileStream 457.9 μs 15.37 μs 45.31 μs 1
  • FileStream反序列化
1
2
3
4
5
6
7

BenchmarkDotNet=v0.12.1, OS=Windows 10.0.18362.900 (1903/May2019Update/19H1)
Intel Core i7-7700 CPU 3.60GHz (Kaby Lake), 1 CPU, 8 logical and 4 physical cores
[Host] : .NET Framework 4.8 (4.8.4180.0), X86 LegacyJIT

Toolchain=InProcessEmitToolchain

Method Mean Error StdDev Rank
Google.ProtoBuf使用FileStream 249.8 μs 4.97 μs 10.15 μs 1
protobuf-net使用FileStream 260.5 μs 4.84 μs 9.88 μs 2

上方是简易数据结构性能,下方是相对复杂一些的数据结构性能


注意事项【一些坑】:

  • proto3移除了required,对于required的作用,其实争议很大。proto3更加追求极简。
  • 但在我的之前遇到的情况,bool和enum未赋值的情况下,序列化时,默认值是有问题的。所以,单纯.net环境下,目前还是推荐proto2吧。
  • _:proto文件中下划线表示下划线后一个字母为大写。生成的类中,属性是无法带有下划线的。
  • 官方建议属性定义均用小写
  • FileStream序列化
1
2
3
4
5
6
7

BenchmarkDotNet=v0.12.1, OS=Windows 10.0.18362.900 (1903/May2019Update/19H1)
Intel Core i7-7700 CPU 3.60GHz (Kaby Lake), 1 CPU, 8 logical and 4 physical cores
[Host] : .NET Framework 4.8 (4.8.4180.0), X86 LegacyJIT

Toolchain=InProcessEmitToolchain

Method Mean Error StdDev Rank
Google.ProtoBuf使用FileStream 470.3 μs 17.45 μs 51.47 μs 1
protobuf-net使用FileStream 466.9 μs 14.68 μs 43.28 μs 1
  • FileStream反序列化
1
2
3
4
5
6
7

BenchmarkDotNet=v0.12.1, OS=Windows 10.0.18362.900 (1903/May2019Update/19H1)
Intel Core i7-7700 CPU 3.60GHz (Kaby Lake), 1 CPU, 8 logical and 4 physical cores
[Host] : .NET Framework 4.8 (4.8.4180.0), X86 LegacyJIT

Toolchain=InProcessEmitToolchain

Method Mean Error StdDev Rank
Google.ProtoBuf使用FileStream 281.3 μs 7.42 μs 21.88 μs 1
protobuf-net使用FileStream 283.1 μs 5.64 μs 13.29 μs 1

由此可见,两个库直接性能差距并不是很大,鉴于个人的使用场景,更加推荐protobuf-net。


掘:奇葩史

WiX Toolset 教程索引页

注意:虽然WiX Toolset功能强大,但其学习曲线相对较高。请慎重选择
若没有足够时间、没心思搞的请绕行至inno setup、installshield、nisi、setupfactory。。。

WiX Toolset 3.x 手册目录

入门

基础介绍

WixUI对话框库

WiX内置了一套Windows Installer安装包的用户界面库。本部分介绍关于使用WixUI对话框库的内容:

安装包捆绑包基础

使用技巧

本节包括怎样处理常见的WiX任务

文件、快捷方式和注册表

可再分发和安装检查

用户界面及本地化

产品更新

其他一般性处理

参考

  • Wix Toolset的nuget包:Wix Toolset
    • wix-nuget 源码
    • 之前Wix的nuget包Owner一直是kzu,大概2019年底修改成了wixtoolset和rob —— 这个nuget包目前应该还是kzu在维护。

掘:奇葩史

Wix Toolset基础 —— 环境变量设置示例

Wix Toolset基础 —— 环境变量设置示例

返回目录索引

本文由简至繁介绍如何在安装包中配置环境变量。
关于Environment元素的属性说明见:Wix Toolset基础 —— 环境变量设置

本文示例代码:Jess.Sample.Setup.Environment

基础配置示例

本文环境变量设置功能实现的是:将软件安装目录添加到系统环境变量 Path末尾处

首先先看下最简配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"
xmlns:util="http://schemas.microsoft.com/wix/UtilExtension" >

<Product Id="*" Name="环境变量设置Sample"
Language="2052" Codepage="936"
Version="1.0.0.0" Manufacturer="Jess"
UpgradeCode="{6C0C1D6D-80CF-4074-AC42-5ADBD7E4087F}">
<Package InstallerVersion="300" Compressed="yes" InstallScope="perMachine" SummaryCodepage="936" />

<MediaTemplate EmbedCab="yes" />

<UIRef Id="WixUI_Minimal"/>
<Property Id="WIXUI_INSTALLDIR" Value="INSTALLLOCATION" />

<Feature Id="ProductFeature" Title="Wix环境变量设置" Level="1">
<ComponentGroupRef Id="ProductComponents" />
</Feature>
</Product>

<Fragment>
<Directory Id="TARGETDIR" Name="SourceDir">
<Directory Id="ProgramFiles64Folder">
<Directory Id="INSTALLLOCATION" Name="EnvSample">
</Directory>
</Directory>
</Directory>
</Fragment>

<Fragment>
<ComponentGroup Id="ProductComponents" >
<Component Id="cmpEnvSample" Guid="{63B3AF3B-94BB-4D3D-8D24-A0772CA5CFAA}" Directory="INSTALLLOCATION">
<CreateFolder />
<!--系统环境变量Path中添加路径-->
<Environment Id="EnvSamplePath" Action="set" Part="last" Name="Path" Permanent="no" System="yes" Value="[INSTALLLOCATION]" />
</Component>
</ComponentGroup>
</Fragment>

</Wix>

其核心就一句话:<Environment Id="EnvSamplePath" Action="set" Part="last" Name="Path" Permanent="no" System="yes" Value="[INSTALLLOCATION]" />

控制环境变量设置时机

软件安装过程中,环境变量的设置,可能需要在某一步骤之后,即需要控制Wix安装的Action顺序。
例如环境变量的写入需要在卸载之后,配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

<?define ProductVersion="1.0.0.0" ?>

<Product Id="*" Name="环境变量设置Sample"
Language="2052" Codepage="936"
Version="1.0.0.0" Manufacturer="Jess"
UpgradeCode="{6C0C1D6D-80CF-4074-AC42-5ADBD7E4087F}">
<Package InstallerVersion="300" Compressed="yes" InstallScope="perMachine" SummaryCodepage="936" />

<MediaTemplate EmbedCab="yes" />

<UIRef Id="WixUI_Minimal"/>
<Property Id="WIXUI_INSTALLDIR" Value="INSTALLLOCATION" />

<Feature Id="ProductFeature" Title="Wix环境变量设置" Level="1">
<ComponentGroupRef Id="ProductComponents" />
</Feature>

<Upgrade Id="{6C0C1D6D-80CF-4074-AC42-5ADBD7E4087F}">
<UpgradeVersion OnlyDetect='no' Property='SELFFOUND'
Minimum='$(var.ProductVersion)' IncludeMinimum='yes'
Maximum='$(var.ProductVersion)' IncludeMaximum='yes' />
<UpgradeVersion OnlyDetect='no' Property='PREVIOUSFOUND'
Minimum='0.0.0.1' IncludeMinimum='yes'
Maximum='$(var.ProductVersion)' IncludeMaximum='yes' />
<UpgradeVersion OnlyDetect='no' Property='NEWERFOUND'
Minimum='$(var.ProductVersion)' IncludeMinimum='yes' />
</Upgrade>

<InstallExecuteSequence>
<RemoveExistingProducts Before='InstallInitialize' />
<WriteEnvironmentStrings />
</InstallExecuteSequence>
</Product>

核心就是在InstallExecuteSequence中添加WriteEnvironmentStrings执行步骤,也可以直接设置Sequence属性:<WriteEnvironmentStrings Sequence="1" />

控制环境变量设置条件

在某些情况下,环境变量的设置需要添加条件判断,如通过Bootstrap安装时,外部传入的参数,标记不需要添加环境变量。

以下配置添加了EnableEnv属性控制是否设置环境变量:

1
2
3
4
5
6
7

<InstallExecuteSequence>
<WriteEnvironmentStrings>EnableEnv=1</WriteEnvironmentStrings>
</InstallExecuteSequence>

<Property Id="EnableEnv" Value="1"></Property>

另外,也可以直接设置Suppress属性禁用环境变量设置<WriteEnvironmentStrings Suppress="yes" />


理:奇葩史

[译]:Wix Toolset基础 —— 环境变量设置

[译]:Wix Toolset基础 —— 环境变量设置

返回目录索引

参考链接:

示例参考:

Environment简易说明

安装过程中要添加环境变量,需要在组件中使用Environment标签:

1
<Environment Id='UpdatePath' Name='PATH' Action='set' Permanent='yes' System='yes' Part='last' Value='[INSTALLDIR]' />
  • Action属性指定组件安装时,所执行的操作 —— createsetremove
  • Part属性控制设置新值的方式:all替换之前的值,first在现有值之前添加,last添加到现有值之后;
  • Permanent属性控制产品卸载时,环境变量的处理方式:yes保留环境变量值,no在产品卸载时,同时删除环境变量值;
  • System属性指定环境变量值是系统变量还是用户变量
  • 所有名称使用大写。 —— 经测试,目前小写名称也支持。

Environment元素结构

父级需要是Component

属性列表:

属性 类型 说明 是否必需
Id 字符串 Environment条目的唯一标识
Action 枚举

在安装父级组件时,指定环境变量的操作:createdsetremoved。此属性必须为以下的值:

  • create
    • 安装期间,若不存在此环境变量,则创建;若存在,则不影响已有的值。
  • set
    • 安装期间,若不存在此环境变量,则创建;若存在,则修改为新设置的值。
  • remove
    • 安装期间,移除环境变量。仅当环境变量的name和value均匹配时,才移除。若需要移除环境变量(无论它的值是什么),则不要设置Value属性。
Name 字符串 环境变量的名称
Part 枚举

此属性必须为以下的值:

  • all
    • 替换整个环境变量的值。此值为默认值。
  • first
    • 在现有值之前插入
  • last
    • 在现有值之后附加
Permanent YesNoType 指定在卸载时是否保留环境变量。
Separator 字符串 环境变量Value值的分隔符,默认时分号分隔。
System YesNoType 指定环境变量是否添加到系统环境变量。默认值为`no`,表示环境变量添加到用户变量中。
Value 字符串 待设置到环境变量中的值。若此属性为设置,则在安装过程中移除已存在的同名环境变量。

译:奇葩史

Git 2.27.0 使用Git-SVN时,引发E235000错误问题解决

Git 2.27.0 使用Git-SVN时,引发E235000错误问题解决

缘由

今天更新VSCode 1.47.0之后,一直提示git 2.26.2版本有已知问题,建议更新;本着相信Microsoft的心态,更新了Git 2.27.0版本。

问题

Git更新为2.27.0之后,之前用git-svn连接的库,不能git svn rebasegit svn dcommit了,一直报如下错误:

1
E235000: In file 'subversion/bindings/swig/perl/libsvn_swig_perl/swigutil_pl.c' line 1666: assertion failed (get_current_pool_cb != NULL)

解决

将Git 2.27.0卸载,重新安装2.26.2版本后,问题解决。

下载链接


解:奇葩史