Skip to content

Commit

Permalink
Add TypeFilter enabled benchmark (#283)
Browse files Browse the repository at this point in the history
  • Loading branch information
Arkatufus authored Jan 12, 2022
1 parent d8c7ea1 commit 87ad62d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/Hyperion.Benchmarks/SerializeClassesBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#endregion

using System;
using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes;

namespace Hyperion.Benchmarks
Expand All @@ -20,9 +21,25 @@ public class SerializeClassesBenchmark : HyperionBenchmark
private LargeSealedClass sealedObject;
private GenericClass<int, string, bool, DateTime, Guid> genericObject;

private Serializer _filteredSerializer;

protected override void Init()
{
Serializer = new Serializer(new SerializerOptions(preserveObjectReferences:true));
var baseOptions = new SerializerOptions(preserveObjectReferences: true);
Serializer = new Serializer(baseOptions);

var filteredOptions = baseOptions
.WithTypeFilter(
TypeFilterBuilder.Create()
.Include<CyclicClassA>()
.Include<CyclicClassB>()
.Include<VirtualTestClass>()
.Include<LargeSealedClass>()
.Include<GenericClass<int, string, bool, DateTime, Guid>>()
.Include<TestEnum>()
.Build());
_filteredSerializer = new Serializer(filteredOptions);

var a = new CyclicClassA();
var b = new CyclicClassB();
a.B = b;
Expand All @@ -45,9 +62,23 @@ protected override void Init()
#endregion

[Benchmark] public void Cyclic_References() => SerializeAndDeserialize(cyclic);
[Benchmark] public void Filtered_Cyclic_References() => SerializeAndFilteredDeserialize(cyclic);
[Benchmark] public void Virtual_Classes() => SerializeAndDeserialize(virtualObject);
[Benchmark] public void Filtered_Virtual_Classes() => SerializeAndFilteredDeserialize(virtualObject);
[Benchmark] public void Large_Sealed_Classes() => SerializeAndDeserialize(sealedObject);
[Benchmark] public void Filtered_Large_Sealed_Classes() => SerializeAndFilteredDeserialize(sealedObject);
[Benchmark] public void Generic_Classes() => SerializeAndDeserialize(genericObject);
[Benchmark] public void Filtered_Generic_Classes() => SerializeAndFilteredDeserialize(genericObject);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void SerializeAndFilteredDeserialize<T>(T elem)
{
Serializer.Serialize(elem, Stream);
Stream.Position = 0;

_filteredSerializer.Deserialize<T>(Stream);
Stream.Position = 0;
}
}

#region test data types
Expand Down
26 changes: 26 additions & 0 deletions src/Hyperion.Benchmarks/SerializeStructsBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// -----------------------------------------------------------------------
#endregion

using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using BenchmarkDotNet.Attributes;

Expand All @@ -15,13 +16,25 @@ namespace Hyperion.Benchmarks
public class SerializeStructsBenchmark : HyperionBenchmark
{
#region init
private Serializer _filteredSerializer;

private StandardStruct standardValue;
private BlittableStruct blittableValue;
private TestEnum testEnum;

protected override void Init()
{
base.Init();

var filteredOptions = SerializerOptions.Default
.WithTypeFilter(
TypeFilterBuilder.Create()
.Include<StandardStruct>()
.Include<BlittableStruct>()
.Include<TestEnum>()
.Build());
_filteredSerializer = new Serializer(filteredOptions);

standardValue = new StandardStruct(1, "John", "Doe", isLoggedIn: false);
blittableValue = new BlittableStruct(59, 92);
testEnum = TestEnum.HatesAll;
Expand All @@ -30,8 +43,21 @@ protected override void Init()
#endregion

[Benchmark] public void Enums() => SerializeAndDeserialize(testEnum);
[Benchmark] public void Filtered_Enums() => SerializeAndFilteredDeserialize(testEnum);
[Benchmark] public void Standard_Value_Types() => SerializeAndDeserialize(standardValue);
[Benchmark] public void Filtered_Standard_Value_Types() => SerializeAndFilteredDeserialize(standardValue);
[Benchmark] public void Blittable_Value_Types() => SerializeAndDeserialize(blittableValue);
[Benchmark] public void Filtered_Blittable_Value_Types() => SerializeAndFilteredDeserialize(blittableValue);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void SerializeAndFilteredDeserialize<T>(T elem)
{
Serializer.Serialize(elem, Stream);
Stream.Position = 0;

_filteredSerializer.Deserialize<T>(Stream);
Stream.Position = 0;
}
}

#region test data types
Expand Down

0 comments on commit 87ad62d

Please sign in to comment.