Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix benchmarking project #153

Merged
merged 1 commit into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
<Project>
<PropertyGroup>
<RepoRoot>$([System.IO.Path]::GetFullPath('$(MSBuildThisFileDirectory)'))</RepoRoot>
<ArtifactsPath>$(RepoRoot)artifacts/</ArtifactsPath>
<BaseOutputPath>$(ArtifactsPath)bin/$(MSBuildProjectName)/</BaseOutputPath>
<BaseIntermediateOutputPath>$(ArtifactsPath)obj/$(MSBuildProjectName)/</BaseIntermediateOutputPath>
<UseArtifactsOutput>true</UseArtifactsOutput>
<Nullable>enable</Nullable>
<Deterministic>true</Deterministic>
</PropertyGroup>
Expand Down
11 changes: 8 additions & 3 deletions bench/DesrializeFromString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@

namespace Benchmarks
{
[GenericTypeArguments(typeof(LoginViewModel))]
[GenericTypeArguments(typeof(Location))]
public class DeserializeFromString<T> where T : Serde.IDeserialize<T>
[GenericTypeArguments(typeof(LoginViewModel), typeof(LoginViewModel))]
[GenericTypeArguments(typeof(Location), typeof(LocationWrap))]
public class DeserializeFromString<T, U>
where T : Serde.IDeserialize<T>
where U : Serde.IDeserialize<T>
{
private JsonSerializerOptions _options = null!;
private string value = null!;
Expand Down Expand Up @@ -37,6 +39,9 @@ public void Setup()
[Benchmark]
public T SerdeJson() => Serde.Json.JsonSerializer.Deserialize<T>(value);

[Benchmark]
public T SerdeManual() => Serde.Json.JsonSerializer.Deserialize<T, U>(value);

// DataContractJsonSerializer does not provide an API to serialize to string
// so it's not included here (apples vs apples thing)
}
Expand Down
7 changes: 7 additions & 0 deletions bench/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Project>
<!-- See https://aka.ms/dotnet/msbuild/customize for more details on customizing your build -->
<PropertyGroup>


</PropertyGroup>
</Project>
4 changes: 2 additions & 2 deletions bench/JsonToString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Benchmarks
[GenericTypeArguments(typeof(LoginViewModel))]
[GenericTypeArguments(typeof(Location))]
[GenericTypeArguments(typeof(Serde.Test.AllInOne))]
public class JsonToString<T> where T : Serde.ISerialize
public class JsonToString<T> where T : Serde.ISerialize, Serde.ISerialize<T>
{
private JsonSerializerOptions _options = null!;
private T value = default!;
Expand All @@ -21,7 +21,7 @@ public void Setup()
_options = new JsonSerializerOptions();
_options.IncludeFields = true;
value = DataGenerator.GenerateSerialize<T>();
}
}

[Benchmark]
public string JsonNet() => Newtonsoft.Json.JsonConvert.SerializeObject(value);
Expand Down
20 changes: 11 additions & 9 deletions bench/Program.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Text.Json;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Running;
using Benchmarks;

#if DEBUG

const string LocationSample = DataGenerator.LocationSample;
var options = new JsonSerializerOptions()
{
Expand All @@ -19,12 +17,16 @@
var loc1 = System.Text.Json.JsonSerializer.Deserialize<Location>(LocationSample, options);
var loc2 = Serde.Json.JsonSerializer.Deserialize<Location>(LocationSample);

Console.WriteLine(loc1 == loc2);

#else
Console.WriteLine("Checking correctness of serialization: " + (loc1 == loc2));
if (loc1 != loc2)
{
throw new InvalidOperationException(@"""
Serialization is not correct
STJ: {json1}
Serde: {json2}
""");
}

var config = DefaultConfig.Instance.AddDiagnoser(MemoryDiagnoser.Default);
var summary = BenchmarkSwitcher.FromAssembly(typeof(JsonToString<>).Assembly)
.Run(args, config);

#endif
.Run(args, config);
108 changes: 108 additions & 0 deletions bench/SampleTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,112 @@ public partial record Location
public string PhoneNumber { get; set; }
public string Country { get; set; }
}

public partial record LocationWrap : IDeserialize<Location>
{
[GenerateDeserialize]
private enum FieldNames : byte
{
Id = 1,
Address1 = 2,
Address2 = 3,
City = 4,
State = 5,
PostalCode = 6,
Name = 7,
PhoneNumber = 8,
Country = 9,
}

public static Location Deserialize<D>(ref D deserializer) where D : IDeserializer
{
return Deserialize<Location, D>(ref deserializer);
}
private sealed class LocationVisitor : IDeserializeVisitor<Location>
{
public string ExpectedTypeName => nameof(Location);

Location IDeserializeVisitor<Location>.VisitDictionary<D>(ref D d)
{
int _l_id = default!;
string _l_address1 = default!;
string _l_address2 = default!;
string _l_city = default!;
string _l_state = default!;
string _l_postalCode = default!;
string _l_name = default!;
string _l_phoneNumber = default!;
string _l_country = default!;
short _r_assignedValid = 0b0;
while (d.TryGetNextKey<FieldNames, FieldNamesWrap>(out var key))
{
switch (key)
{
case FieldNames.Id:
_l_id = d.GetNextValue<int, Int32Wrap>();
_r_assignedValid |= 1 << 0;
break;
case FieldNames.Address1:
_l_address1 = d.GetNextValue<string, StringWrap>();
_r_assignedValid |= 1 << 1;
break;
case FieldNames.Address2:
_l_address2 = d.GetNextValue<string, StringWrap>();
_r_assignedValid |= 1 << 2;
break;
case FieldNames.City:
_l_city = d.GetNextValue<string, StringWrap>();
_r_assignedValid |= 1 << 3;
break;
case FieldNames.State:
_l_state = d.GetNextValue<string, StringWrap>();
_r_assignedValid |= 1 << 4;
break;
case FieldNames.PostalCode:
_l_postalCode = d.GetNextValue<string, StringWrap>();
_r_assignedValid |= 1 << 5;
break;
case FieldNames.Name:
_l_name = d.GetNextValue<string, StringWrap>();
_r_assignedValid |= 1 << 6;
break;
case FieldNames.PhoneNumber:
_l_phoneNumber = d.GetNextValue<string, StringWrap>();
_r_assignedValid |= 1 << 7;
break;
case FieldNames.Country:
_l_country = d.GetNextValue<string, StringWrap>();
_r_assignedValid |= 1 << 8;
break;
}
}

if (_r_assignedValid != 0b111111111)
{
throw new Serde.InvalidDeserializeValueException("Not all members were assigned");
}

var newType = new Location
{
Id = _l_id,
Address1 = _l_address1,
Address2 = _l_address2,
City = _l_city,
State = _l_state,
PostalCode = _l_postalCode,
Name = _l_name,
PhoneNumber = _l_phoneNumber,
Country = _l_country,
};
return newType;
}
}

private static T Deserialize<T, D>(ref D deserialize)
where T : IDeserialize<T>
where D : IDeserializer
{
return T.Deserialize(ref deserialize);
}
}
}
6 changes: 4 additions & 2 deletions bench/bench.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<Deterministic>true</Deterministic>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.2" />
<PackageReference Include="BenchmarkDotNet" Version="0.13.12" />
<PackageReference Include="Newtonsoft.JSON" Version="13.0.1" />
<ProjectReference Include="../src/serde/Serde.csproj" />
<ProjectReference Include="../src/generator/SerdeGenerator.csproj" OutputItemType="Analyzer" />
Expand Down
22 changes: 22 additions & 0 deletions bench/bench.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "bench", "bench.csproj", "{93A31D0B-CADF-4F0B-86F2-D0E2D073DD81}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{93A31D0B-CADF-4F0B-86F2-D0E2D073DD81}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{93A31D0B-CADF-4F0B-86F2-D0E2D073DD81}.Debug|Any CPU.Build.0 = Debug|Any CPU
{93A31D0B-CADF-4F0B-86F2-D0E2D073DD81}.Release|Any CPU.ActiveCfg = Release|Any CPU
{93A31D0B-CADF-4F0B-86F2-D0E2D073DD81}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
Loading