|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text; |
| 4 | +using Akka.Actor; |
| 5 | +using Akka.Configuration; |
| 6 | +using Akka.Util.Internal; |
| 7 | +using BenchmarkDotNet.Attributes; |
| 8 | +using MessagePack; |
| 9 | + |
| 10 | +namespace Akka.Serialization.MessagePack.Benchmarks |
| 11 | +{ |
| 12 | + [Config(typeof(MyConfig))] |
| 13 | + [MemoryDiagnoser] |
| 14 | + public class SerializationBenchmarks |
| 15 | + { |
| 16 | + private MsgPackSerializer MsgPackSerializer; |
| 17 | + private HyperionSerializer HyperionSerializer; |
| 18 | + private NewtonSoftJsonSerializer NewtonSoftJsonSerializer; |
| 19 | + |
| 20 | + private const string TestString = "simple long string"; |
| 21 | + |
| 22 | + private SimpleObject TestSimpleObject = new SimpleObject() |
| 23 | + { |
| 24 | + MyProperty1 = 1, MyProperty2 = 2, MyProperty3 = 3, MyProperty4 = 4, MyProperty5 = 5 |
| 25 | + }; |
| 26 | + |
| 27 | + private SimpleOptimizedObject TestSimpleObjectOptimized = new SimpleOptimizedObject |
| 28 | + { |
| 29 | + MyProperty1 = 1, MyProperty2 = 2, MyProperty3 = 3, MyProperty4 = 4, MyProperty5 = 5 |
| 30 | + }; |
| 31 | + |
| 32 | + private TypelessObject TestTypelessObject = new TypelessObject |
| 33 | + { |
| 34 | + MyProperty1 = Int16.MaxValue, MyProperty2 = Single.MaxValue, MyProperty3 = TestString, MyProperty4 = TimeSpan.FromDays(3), MyProperty5 = new Uri("http://somesite.com") |
| 35 | + }; |
| 36 | + |
| 37 | + public SerializationBenchmarks() |
| 38 | + |
| 39 | + { |
| 40 | + var config = ConfigurationFactory.ParseString("akka.suppress-json-serializer-warning=true"); |
| 41 | + var system = ActorSystem.Create("SerializationBenchmarks", config); |
| 42 | + |
| 43 | + MsgPackSerializer = new MsgPackSerializer(system.AsInstanceOf<ExtendedActorSystem>()); |
| 44 | + HyperionSerializer = new HyperionSerializer( |
| 45 | + system.AsInstanceOf<ExtendedActorSystem>(), |
| 46 | + new HyperionSerializerSettings(false, false, typeof(SimpleTypesProvider))); |
| 47 | + NewtonSoftJsonSerializer = new NewtonSoftJsonSerializer(system.AsInstanceOf<ExtendedActorSystem>()); |
| 48 | + } |
| 49 | + |
| 50 | + [Benchmark] |
| 51 | + public string MsgPack_serialize_string() |
| 52 | + { |
| 53 | + var bytes = MsgPackSerializer.ToBinary(TestString); |
| 54 | + return MsgPackSerializer.FromBinary<string>(bytes); |
| 55 | + } |
| 56 | + |
| 57 | + [Benchmark] |
| 58 | + public string Hyperion_serialize_string() |
| 59 | + { |
| 60 | + var bytes = HyperionSerializer.ToBinary(TestString); |
| 61 | + return HyperionSerializer.FromBinary<string>(bytes); |
| 62 | + } |
| 63 | + |
| 64 | + [Benchmark] |
| 65 | + public string JsonNet_serialize_string() |
| 66 | + { |
| 67 | + var bytes = NewtonSoftJsonSerializer.ToBinary(TestString); |
| 68 | + return NewtonSoftJsonSerializer.FromBinary<string>(bytes); |
| 69 | + } |
| 70 | + |
| 71 | + [Benchmark] |
| 72 | + public SimpleObject MsgPack_serialize_SimpleObject() |
| 73 | + { |
| 74 | + var bytes = MsgPackSerializer.ToBinary(TestSimpleObject); |
| 75 | + return MsgPackSerializer.FromBinary<SimpleObject>(bytes); |
| 76 | + } |
| 77 | + |
| 78 | + [Benchmark] |
| 79 | + public SimpleObject Hyperion_serialize_SimpleObject() |
| 80 | + { |
| 81 | + var bytes = HyperionSerializer.ToBinary(TestSimpleObject); |
| 82 | + return HyperionSerializer.FromBinary<SimpleObject>(bytes); |
| 83 | + } |
| 84 | + |
| 85 | + [Benchmark] |
| 86 | + public SimpleObject JsonNet_serialize_SimpleObject() |
| 87 | + { |
| 88 | + var bytes = NewtonSoftJsonSerializer.ToBinary(TestSimpleObject); |
| 89 | + return NewtonSoftJsonSerializer.FromBinary<SimpleObject>(bytes); |
| 90 | + } |
| 91 | + |
| 92 | + [Benchmark] |
| 93 | + public SimpleOptimizedObject MsgPack_serialize_SimpleOptimizedObject_int_keys() |
| 94 | + { |
| 95 | + var bytes = MsgPackSerializer.ToBinary(TestSimpleObjectOptimized); |
| 96 | + return MsgPackSerializer.FromBinary<SimpleOptimizedObject>(bytes); |
| 97 | + } |
| 98 | + |
| 99 | + [Benchmark] |
| 100 | + public SimpleOptimizedObject Hyperion_serialize_SimpleOptimizedObject_preregistered() |
| 101 | + { |
| 102 | + var bytes = HyperionSerializer.ToBinary(TestSimpleObjectOptimized); |
| 103 | + return HyperionSerializer.FromBinary<SimpleOptimizedObject>(bytes); |
| 104 | + } |
| 105 | + |
| 106 | + [Benchmark] |
| 107 | + public TypelessObject MsgPack_serialize_TypelessObject() |
| 108 | + { |
| 109 | + var bytes = MsgPackSerializer.ToBinary(TestTypelessObject); |
| 110 | + return MsgPackSerializer.FromBinary<TypelessObject>(bytes); |
| 111 | + } |
| 112 | + |
| 113 | + [Benchmark] |
| 114 | + public TypelessObject Hyperion_serialize_TypelessObject() |
| 115 | + { |
| 116 | + var bytes = HyperionSerializer.ToBinary(TestTypelessObject); |
| 117 | + return HyperionSerializer.FromBinary<TypelessObject>(bytes); |
| 118 | + } |
| 119 | + |
| 120 | + [Benchmark] |
| 121 | + public TypelessObject JsonNet_serialize_TypelessObject() |
| 122 | + { |
| 123 | + var bytes = NewtonSoftJsonSerializer.ToBinary(TestTypelessObject); |
| 124 | + return NewtonSoftJsonSerializer.FromBinary<TypelessObject>(bytes); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + public class SimpleObject |
| 129 | + { |
| 130 | + public int MyProperty1 { get; set; } |
| 131 | + public int MyProperty2 { get; set; } |
| 132 | + public int MyProperty3 { get; set; } |
| 133 | + public int MyProperty4 { get; set; } |
| 134 | + public int MyProperty5 { get; set; } |
| 135 | + } |
| 136 | + |
| 137 | + [MessagePackObject] |
| 138 | + public class SimpleOptimizedObject |
| 139 | + { |
| 140 | + [Key(0)] |
| 141 | + public int MyProperty1 { get; set; } |
| 142 | + |
| 143 | + [Key(1)] |
| 144 | + public int MyProperty2 { get; set; } |
| 145 | + |
| 146 | + [Key(2)] |
| 147 | + public int MyProperty3 { get; set; } |
| 148 | + |
| 149 | + [Key(3)] |
| 150 | + public int MyProperty4 { get; set; } |
| 151 | + |
| 152 | + [Key(4)] |
| 153 | + public int MyProperty5 { get; set; } |
| 154 | + } |
| 155 | + |
| 156 | + public class TypelessObject |
| 157 | + { |
| 158 | + public object MyProperty1 { get; set; } |
| 159 | + public object MyProperty2 { get; set; } |
| 160 | + public object MyProperty3 { get; set; } |
| 161 | + public object MyProperty4 { get; set; } |
| 162 | + public object MyProperty5 { get; set; } |
| 163 | + } |
| 164 | + |
| 165 | + public class SimpleTypesProvider : IKnownTypesProvider |
| 166 | + { |
| 167 | + public SimpleTypesProvider(ExtendedActorSystem system) |
| 168 | + { |
| 169 | + if (system == null) |
| 170 | + throw new ArgumentNullException(nameof(system)); |
| 171 | + } |
| 172 | + |
| 173 | + public IEnumerable<Type> GetKnownTypes() => new Type[] { typeof(SimpleOptimizedObject) }; |
| 174 | + } |
| 175 | +} |
0 commit comments