Skip to content

Instantly share code, notes, and snippets.

@Aaronontheweb
Last active September 19, 2024 19:43
Show Gist options
  • Save Aaronontheweb/ac3a8b2d9e5705a10c0cb2e9d4943154 to your computer and use it in GitHub Desktop.
Save Aaronontheweb/ac3a8b2d9e5705a10c0cb2e9d4943154 to your computer and use it in GitHub Desktop.
MaterializedViewState Serialization
void Main()
{
var system = ActorSystem.Create("Spec");
var toSerialize = new MaterializedViewState(Offset.Sequence(1));
var serializerFor = system.Serialization.FindSerializerForType(typeof(MaterializedViewState));
var bytes = serializerFor.ToBinary(toSerialize);
string formattedArray = "new byte[] { " + string.Join(", ", Array.ConvertAll(bytes, b => "0x" + b.ToString("X2"))) + " };";
Console.WriteLine(formattedArray);
var deserialized = serializerFor.FromBinary(MaterializedViewStateBenchmarks.SerializedInitialOffset, typeof(MaterializedViewState));
Console.WriteLine(deserialized);
}
// Note that you must explicitly enable compiler optimizations:
#LINQPad optimize+
void Main()
{
Util.AutoScrollResults = true;
BenchmarkRunner.Run<MaterializedViewStateBenchmarks>();
}
// You can define other methods, fields, classes and namespaces here
/// <summary>
/// Used by a materialized view to store its offsets inside an Akka.Persistence buffer.
/// </summary>
/// <param name="LastOffset">The last successful offset recorded during a projection.</param>
public record MaterializedViewState(Offset LastOffset);
[ShortRunJob]
public class MaterializedViewStateBenchmarks{
public static readonly MaterializedViewState InitialOffset = new MaterializedViewState(Offset.Sequence(1));
public static readonly byte[] SerializedInitialOffset = new byte[] { 0x7B, 0x22, 0x24, 0x69, 0x64, 0x22, 0x3A, 0x22, 0x31, 0x22, 0x2C, 0x22, 0x24, 0x74, 0x79, 0x70, 0x65, 0x22, 0x3A, 0x22, 0x55, 0x73, 0x65, 0x72, 0x51, 0x75, 0x65, 0x72, 0x79, 0x2B, 0x4D, 0x61, 0x74, 0x65, 0x72, 0x69, 0x61, 0x6C, 0x69, 0x7A, 0x65, 0x64, 0x56, 0x69, 0x65, 0x77, 0x53, 0x74, 0x61, 0x74, 0x65, 0x2C, 0x20, 0x4C, 0x49, 0x4E, 0x51, 0x50, 0x61, 0x64, 0x51, 0x75, 0x65, 0x72, 0x79, 0x22, 0x2C, 0x22, 0x4C, 0x61, 0x73, 0x74, 0x4F, 0x66, 0x66, 0x73, 0x65, 0x74, 0x22, 0x3A, 0x7B, 0x22, 0x24, 0x69, 0x64, 0x22, 0x3A, 0x22, 0x32, 0x22, 0x2C, 0x22, 0x24, 0x74, 0x79, 0x70, 0x65, 0x22, 0x3A, 0x22, 0x41, 0x6B, 0x6B, 0x61, 0x2E, 0x50, 0x65, 0x72, 0x73, 0x69, 0x73, 0x74, 0x65, 0x6E, 0x63, 0x65, 0x2E, 0x51, 0x75, 0x65, 0x72, 0x79, 0x2E, 0x53, 0x65, 0x71, 0x75, 0x65, 0x6E, 0x63, 0x65, 0x2C, 0x20, 0x41, 0x6B, 0x6B, 0x61, 0x2E, 0x50, 0x65, 0x72, 0x73, 0x69, 0x73, 0x74, 0x65, 0x6E, 0x63, 0x65, 0x2E, 0x51, 0x75, 0x65, 0x72, 0x79, 0x22, 0x2C, 0x22, 0x56, 0x61, 0x6C, 0x75, 0x65, 0x22, 0x3A, 0x31, 0x7D, 0x7D };
public ActorSystem System {get; private set;}
public Akka.Serialization.Serializer Serializer {get; private set;}
public static readonly Type TypeOfMsg = typeof(MaterializedViewState);
[GlobalSetup]
public void GlobalSetup(){
System = ActorSystem.Create("Spec");
Serializer = System.Serialization.FindSerializerForType(TypeOfMsg);
}
[Benchmark]
public byte[] SerializeViewState(){
return Serializer.ToBinary(InitialOffset);
}
[Benchmark]
public object DeserializeViewState(){
return Serializer.FromBinary(SerializedInitialOffset, TypeOfMsg);
}
[GlobalCleanup]
public void GlobalCleanup(){
System.Terminate().Wait();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment