Skip to content

Commit 438f1b3

Browse files
committed
Add Benchmarks
1 parent ba83447 commit 438f1b3

17 files changed

Lines changed: 479 additions & 2 deletions

File tree

System.Text.Json.Protobuf.sln

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Text.Json.Protobuf",
44
EndProject
55
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Text.Json.Protobuf.Tests", "test\System.Text.Json.Protobuf.Tests\System.Text.Json.Protobuf.Tests.csproj", "{97FF3953-1E5F-4D06-95A3-1956448C8E6F}"
66
EndProject
7+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Benchmarks", "Benchmarks", "{618C7565-3ABA-429E-BCAE-69D7F671AF5B}"
8+
EndProject
9+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Text.Json.Protobuf.Benchmark.SimpleMessage", "benchmark\System.Text.Json.Protobuf.Benchmark.SimpleMessage\System.Text.Json.Protobuf.Benchmark.SimpleMessage.csproj", "{DC053104-9F7E-493E-AB1E-2376BB5B3029}"
10+
EndProject
11+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Text.Json.Protobuf.Benchmark.Shared", "benchmark\System.Text.Json.Protobuf.Benchmark.Shared\System.Text.Json.Protobuf.Benchmark.Shared.csproj", "{055A5060-09B3-4693-91D1-D526A44CD739}"
12+
EndProject
13+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Text.Json.Protobuf.Benchmark.MessageWithOneOfs", "benchmark\System.Text.Json.Protobuf.Benchmark.MessageWithOneOfs\System.Text.Json.Protobuf.Benchmark.MessageWithOneOfs.csproj", "{41D196D1-724E-4607-90E7-9564C7493E39}"
14+
EndProject
15+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Text.Json.Protobuf.Benchmark.MessageWithRepeatedFields", "benchmark\System.Text.Json.Protobuf.Benchmark.MessageWithRepeatedFields\System.Text.Json.Protobuf.Benchmark.MessageWithRepeatedFields.csproj", "{FD123B7C-200D-4EB1-85AF-688A38E5EF4D}"
16+
EndProject
717
Global
818
GlobalSection(SolutionConfigurationPlatforms) = preSolution
919
Debug|Any CPU = Debug|Any CPU
@@ -18,5 +28,27 @@ Global
1828
{97FF3953-1E5F-4D06-95A3-1956448C8E6F}.Debug|Any CPU.Build.0 = Debug|Any CPU
1929
{97FF3953-1E5F-4D06-95A3-1956448C8E6F}.Release|Any CPU.ActiveCfg = Release|Any CPU
2030
{97FF3953-1E5F-4D06-95A3-1956448C8E6F}.Release|Any CPU.Build.0 = Release|Any CPU
31+
{DC053104-9F7E-493E-AB1E-2376BB5B3029}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
32+
{DC053104-9F7E-493E-AB1E-2376BB5B3029}.Debug|Any CPU.Build.0 = Debug|Any CPU
33+
{DC053104-9F7E-493E-AB1E-2376BB5B3029}.Release|Any CPU.ActiveCfg = Release|Any CPU
34+
{DC053104-9F7E-493E-AB1E-2376BB5B3029}.Release|Any CPU.Build.0 = Release|Any CPU
35+
{055A5060-09B3-4693-91D1-D526A44CD739}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
36+
{055A5060-09B3-4693-91D1-D526A44CD739}.Debug|Any CPU.Build.0 = Debug|Any CPU
37+
{055A5060-09B3-4693-91D1-D526A44CD739}.Release|Any CPU.ActiveCfg = Release|Any CPU
38+
{055A5060-09B3-4693-91D1-D526A44CD739}.Release|Any CPU.Build.0 = Release|Any CPU
39+
{41D196D1-724E-4607-90E7-9564C7493E39}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
40+
{41D196D1-724E-4607-90E7-9564C7493E39}.Debug|Any CPU.Build.0 = Debug|Any CPU
41+
{41D196D1-724E-4607-90E7-9564C7493E39}.Release|Any CPU.ActiveCfg = Release|Any CPU
42+
{41D196D1-724E-4607-90E7-9564C7493E39}.Release|Any CPU.Build.0 = Release|Any CPU
43+
{FD123B7C-200D-4EB1-85AF-688A38E5EF4D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
44+
{FD123B7C-200D-4EB1-85AF-688A38E5EF4D}.Debug|Any CPU.Build.0 = Debug|Any CPU
45+
{FD123B7C-200D-4EB1-85AF-688A38E5EF4D}.Release|Any CPU.ActiveCfg = Release|Any CPU
46+
{FD123B7C-200D-4EB1-85AF-688A38E5EF4D}.Release|Any CPU.Build.0 = Release|Any CPU
47+
EndGlobalSection
48+
GlobalSection(NestedProjects) = preSolution
49+
{DC053104-9F7E-493E-AB1E-2376BB5B3029} = {618C7565-3ABA-429E-BCAE-69D7F671AF5B}
50+
{055A5060-09B3-4693-91D1-D526A44CD739} = {618C7565-3ABA-429E-BCAE-69D7F671AF5B}
51+
{41D196D1-724E-4607-90E7-9564C7493E39} = {618C7565-3ABA-429E-BCAE-69D7F671AF5B}
52+
{FD123B7C-200D-4EB1-85AF-688A38E5EF4D} = {618C7565-3ABA-429E-BCAE-69D7F671AF5B}
2153
EndGlobalSection
2254
EndGlobal
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System.Text.Json.Protobuf.Benchmark.Shared;
2+
using BenchmarkDotNet.Attributes;
3+
using Newtonsoft.Json;
4+
5+
namespace System.Text.Json.Protobuf.Benchmark.MessageWithOneOfs;
6+
7+
public class Benchmarks
8+
{
9+
private TestMessage _testMessage = new()
10+
{
11+
DoublePropertyValue = 2.5d,
12+
FloatPropertyValue = 3.5f,
13+
Int32PropertyValue = 1,
14+
Int64PropertyValue = 2,
15+
Uint32PropertyValue = 3,
16+
Uint64PropertyValue = 4,
17+
Sint32PropertyValue = 5,
18+
Sint64PropertyValue = 6,
19+
Fixed32PropertyValue = 7,
20+
Fixed64PropertyValue = 8,
21+
Sfixed32PropertyValue = 9,
22+
Sfixed64PropertyValue = 10,
23+
BoolPropertyValue = true,
24+
};
25+
26+
[Benchmark]
27+
public void SystemTextJson()
28+
{
29+
var serialized = JsonSerializer.Serialize(_testMessage, BenchmarkHelper.JsonSerializerOptions);
30+
var _ = JsonSerializer.Deserialize<TestMessage>(serialized, BenchmarkHelper.JsonSerializerOptions);
31+
}
32+
33+
[Benchmark]
34+
public void NewtonsoftJsonWithProtobufParserAndFormatter()
35+
{
36+
var serialized = JsonConvert.SerializeObject(_testMessage, BenchmarkHelper.JsonSerializerSettings);
37+
var _ = JsonConvert.DeserializeObject<TestMessage>(serialized, BenchmarkHelper.JsonSerializerSettings);
38+
}
39+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
using BenchmarkDotNet.Running;
2+
3+
var summary = BenchmarkRunner.Run(typeof(Program).Assembly);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="BenchmarkDotNet" Version="0.13.1" />
12+
<PackageReference Include="Google.Protobuf" Version="3.19.4" />
13+
<PackageReference Include="Google.Protobuf.Tools" Version="3.19.4" />
14+
<PackageReference Include="Grpc.Tools" Version="2.44.0">
15+
<PrivateAssets>all</PrivateAssets>
16+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
17+
</PackageReference>
18+
</ItemGroup>
19+
20+
<ItemGroup>
21+
<Protobuf Include="**/*.proto" />
22+
</ItemGroup>
23+
24+
<ItemGroup>
25+
<ProjectReference Include="..\System.Text.Json.Protobuf.Benchmark.Shared\System.Text.Json.Protobuf.Benchmark.Shared.csproj" />
26+
<ProjectReference Include="..\..\src\System.Text.Json.Protobuf\System.Text.Json.Protobuf.csproj" />
27+
</ItemGroup>
28+
29+
</Project>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
syntax = "proto3";
2+
3+
option csharp_namespace = "System.Text.Json.Protobuf.Benchmark";
4+
5+
message TestMessage {
6+
oneof double_property{
7+
double double_property_value = 1;
8+
}
9+
oneof float_property {
10+
float float_property_value = 2;
11+
}
12+
13+
oneof int_32_property {
14+
int32 int_32_property_value = 3;
15+
16+
}
17+
18+
oneof int_64_property {
19+
int64 int_64_property_value = 4;
20+
}
21+
22+
oneof uint_32_property {
23+
uint32 uint_32_property_value = 5;
24+
25+
}
26+
27+
oneof uint_64_property {
28+
uint32 uint_64_property_value = 6;
29+
30+
}
31+
32+
oneof sint_32_property {
33+
sint32 sint_32_property_value = 7;
34+
}
35+
36+
oneof sint_64_property {
37+
sint64 sint_64_property_value = 8;
38+
}
39+
40+
oneof fixed_32_property {
41+
fixed32 fixed_32_property_value = 9;
42+
}
43+
44+
oneof fixed_64_property {
45+
fixed64 fixed_64_property_value = 10;
46+
}
47+
oneof sfixed_32_property{
48+
sfixed32 sfixed_32_property_value = 11;
49+
}
50+
51+
oneof sfixed_64_property {
52+
sfixed64 sfixed_64_property_value = 12;
53+
}
54+
55+
oneof bool_property {
56+
bool bool_property_value = 13;
57+
}
58+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System.Text.Json.Protobuf.Benchmark.Shared;
2+
using BenchmarkDotNet.Attributes;
3+
using Newtonsoft.Json;
4+
5+
namespace System.Text.Json.Protobuf.Benchmark.MessageWithRepeatedFields;
6+
7+
[MarkdownExporter]
8+
public class Benchmarks
9+
{
10+
private TestMessage _testMessage = new()
11+
{
12+
DoubleProperty = {Enumerable.Range(0, 100).Select(x => x + 2d)},
13+
FloatProperty = {Enumerable.Range(0, 100).Select(x => x + 3.5f)},
14+
Int32Property = {Enumerable.Range(0, 100)},
15+
Int64Property = {Enumerable.Range(0, 100).Select(x => x + 1L)},
16+
Uint32Property = {Enumerable.Range(0, 100).Select(x => (uint) x)},
17+
Uint64Property = {Enumerable.Range(0, 100).Select(x => (ulong) x)},
18+
Sint32Property = {Enumerable.Range(0, 100)},
19+
Sint64Property = {Enumerable.Range(0, 100).Select(x => x + 1L)},
20+
Fixed32Property = {Enumerable.Range(0, 100).Select(x => (uint) x)},
21+
Fixed64Property = {Enumerable.Range(0, 100).Select(x => (ulong) x)},
22+
Sfixed32Property = {Enumerable.Range(0, 100).Select(x => x)},
23+
Sfixed64Property = {Enumerable.Range(0, 100).Select(x => (long) x)},
24+
BoolProperty = {Enumerable.Range(0, 100).Select(x => x % 2 == 0)},
25+
StringProperty = {Enumerable.Range(0, 100).Select(x => "hello" + x)}
26+
};
27+
28+
[Benchmark]
29+
public void SystemTextJson()
30+
{
31+
var serialized = JsonSerializer.Serialize(_testMessage, BenchmarkHelper.JsonSerializerOptions);
32+
var _ = JsonSerializer.Deserialize<TestMessage>(serialized, BenchmarkHelper.JsonSerializerOptions);
33+
}
34+
35+
[Benchmark]
36+
public void NewtonsoftJsonWithProtobufParserAndFormatter()
37+
{
38+
var serialized = JsonConvert.SerializeObject(_testMessage, BenchmarkHelper.JsonSerializerSettings);
39+
var _ = JsonConvert.DeserializeObject<TestMessage>(serialized, BenchmarkHelper.JsonSerializerSettings);
40+
}
41+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
using BenchmarkDotNet.Running;
2+
3+
var summary = BenchmarkRunner.Run(typeof(Program).Assembly);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="BenchmarkDotNet" Version="0.13.1" />
12+
<PackageReference Include="Google.Protobuf" Version="3.19.4" />
13+
<PackageReference Include="Google.Protobuf.Tools" Version="3.19.4" />
14+
<PackageReference Include="Grpc.Tools" Version="2.44.0">
15+
<PrivateAssets>all</PrivateAssets>
16+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
17+
</PackageReference>
18+
</ItemGroup>
19+
20+
<ItemGroup>
21+
<Protobuf Include="**/*.proto" />
22+
</ItemGroup>
23+
24+
<ItemGroup>
25+
<ProjectReference Include="..\System.Text.Json.Protobuf.Benchmark.Shared\System.Text.Json.Protobuf.Benchmark.Shared.csproj" />
26+
<ProjectReference Include="..\..\src\System.Text.Json.Protobuf\System.Text.Json.Protobuf.csproj" />
27+
</ItemGroup>
28+
29+
</Project>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
syntax = "proto3";
2+
3+
option csharp_namespace = "System.Text.Json.Protobuf.Benchmark";
4+
5+
message TestMessage {
6+
7+
repeated double double_property = 1;
8+
9+
repeated float float_property = 2;
10+
11+
repeated int32 int_32_property = 3;
12+
13+
repeated int64 int_64_property = 4;
14+
15+
repeated uint32 uint_32_property = 5;
16+
17+
repeated uint64 uint_64_property = 6;
18+
19+
repeated sint32 sint_32_property = 7;
20+
21+
repeated sint64 sint_64_property = 8;
22+
23+
repeated fixed32 fixed_32_property = 9;
24+
25+
repeated fixed64 fixed_64_property = 10;
26+
27+
repeated sfixed32 sfixed_32_property = 11;
28+
29+
repeated sfixed64 sfixed_64_property = 12;
30+
31+
repeated bool bool_property = 13;
32+
33+
repeated string string_property = 14;
34+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Newtonsoft.Json;
2+
3+
namespace System.Text.Json.Protobuf.Benchmark.Shared;
4+
5+
public static class BenchmarkHelper
6+
{
7+
public static JsonSerializerOptions JsonSerializerOptions = CreateJsonSerializerOptions();
8+
9+
private static JsonSerializerOptions CreateJsonSerializerOptions()
10+
{
11+
var jsonSerializerOptions = new JsonSerializerOptions();
12+
jsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
13+
jsonSerializerOptions.AddProtobufSupport();
14+
return jsonSerializerOptions;
15+
}
16+
17+
public static JsonSerializerSettings JsonSerializerSettings = CreateJsonSerializerSettings();
18+
19+
private static JsonSerializerSettings CreateJsonSerializerSettings()
20+
{
21+
var settings = new JsonSerializerSettings();
22+
settings.Converters.Add(new NewtonsoftProtobufJsonConverter(3, true));
23+
return settings;
24+
}
25+
}

0 commit comments

Comments
 (0)