55using System . Buffers ;
66using System . Diagnostics ;
77using System . Runtime . InteropServices ;
8- using System . Text . Json ;
9- using System . Text . Json . Serialization ;
108using Backdash . Core ;
119using Backdash . Data ;
1210using Backdash . Network ;
1513
1614namespace Backdash . Benchmarks . Cases ;
1715
18- [ RPlotExporter ]
19- [ InProcess , MemoryDiagnoser , RankColumn ]
16+ [ InProcess , MemoryDiagnoser ]
17+ [ RPlotExporter , RankColumn ]
2018public class SerializationBenchmark
2119{
2220 TestData data = null ! ;
2321 TestData result = null ! ;
2422
2523 readonly ArrayBufferWriter < byte > buffer = new ( ( int ) ByteSize . FromMebiBytes ( 10 ) . ByteCount ) ;
2624
27- Utf8JsonWriter jsonWriter = null ! ;
28-
29- readonly JsonSerializerOptions jsonOptions = new ( )
30- {
31- PreferredObjectCreationHandling = JsonObjectCreationHandling . Populate ,
32- WriteIndented = false ,
33- IncludeFields = true ,
34- } ;
35-
36- static TestData NewTestData ( Random random )
37- {
38- TestData testData = new ( )
39- {
40- Field1 = random . NextBool ( ) ,
41- Field2 = random . Next < ulong > ( ) ,
42- } ;
43-
44- for ( int i = 0 ; i < testData . Field3 . Length ; i ++ )
45- {
46- ref var entry = ref testData . Field3 [ i ] ;
47- entry . Field1 = random . Next ( ) ;
48- entry . Field2 = random . Next < uint > ( ) ;
49- entry . Field3 = random . Next < ulong > ( ) ;
50- entry . Field4 = random . Next < long > ( ) ;
51- entry . Field5 = random . Next < short > ( ) ;
52- entry . Field6 = random . Next < ushort > ( ) ;
53- entry . Field7 = random . Next < byte > ( ) ;
54- entry . Field8 = random . Next < sbyte > ( ) ;
55- random . Next ( entry . Field9 . AsSpan ( ) ) ;
56- }
57-
58- return testData ;
59- }
60-
6125 [ GlobalSetup ]
6226 public void Setup ( )
6327 {
6428 Random random = new ( 42 ) ;
65- data = NewTestData ( random ) ;
66- jsonWriter = new ( buffer ) ;
29+ data = TestData . Generate ( random ) ;
6730 }
6831
6932 [ IterationSetup ]
7033 public void BeforeEach ( )
7134 {
7235 buffer . Clear ( ) ;
73- jsonWriter . Reset ( ) ;
7436 result = new ( ) ;
7537 }
7638
39+ [ IterationCleanup ]
40+ public void AfterEach ( )
41+ {
42+ var size = ByteSize . FromBytes ( buffer . WrittenCount ) ;
43+ Console . WriteLine ( $ "Data Size: { size } ({ size . ByteCount } bytes)") ;
44+ }
45+
7746 [ Benchmark ]
7847 public void Backdash ( )
7948 {
@@ -92,15 +61,6 @@ public void MemoryPack()
9261 MemoryPackSerializer . Deserialize ( buffer . WrittenSpan , ref result ! ) ;
9362 Debug . Assert ( data == result ) ;
9463 }
95-
96- [ Benchmark ]
97- public void SystemJson ( )
98- {
99- JsonSerializer . Serialize ( jsonWriter , data , jsonOptions ) ;
100- Utf8JsonReader reader = new ( buffer . WrittenSpan ) ;
101- result = JsonSerializer . Deserialize < TestData > ( ref reader , jsonOptions ) ! ;
102- Debug . Assert ( data == result ) ;
103- }
10464}
10565
10666[ MemoryPackable ]
@@ -112,7 +72,7 @@ public sealed partial class TestData : IBinarySerializable, IEquatable<TestData>
11272
11373 public TestData ( )
11474 {
115- Field3 = new TestEntryData [ 1_000 ] ;
75+ Field3 = new TestEntryData [ 20_000 ] ;
11676 for ( var i = 0 ; i < Field3 . Length ; i ++ )
11777 Field3 [ i ] . Field9 = new int [ 10_000 ] ;
11878 }
@@ -148,6 +108,31 @@ public static bool Equals(TestData? left, TestData? right)
148108
149109 public static bool operator == ( TestData ? left , TestData ? right ) => Equals ( left , right ) ;
150110 public static bool operator != ( TestData ? left , TestData ? right ) => ! Equals ( left , right ) ;
111+
112+ public static TestData Generate ( Random random )
113+ {
114+ TestData testData = new ( )
115+ {
116+ Field1 = random . NextBool ( ) ,
117+ Field2 = random . Next < ulong > ( ) ,
118+ } ;
119+
120+ for ( int i = 0 ; i < testData . Field3 . Length ; i ++ )
121+ {
122+ ref var entry = ref testData . Field3 [ i ] ;
123+ entry . Field1 = random . Next ( ) ;
124+ entry . Field2 = random . Next < uint > ( ) ;
125+ entry . Field3 = random . Next < ulong > ( ) ;
126+ entry . Field4 = random . Next < long > ( ) ;
127+ entry . Field5 = random . Next < short > ( ) ;
128+ entry . Field6 = random . Next < ushort > ( ) ;
129+ entry . Field7 = random . Next < byte > ( ) ;
130+ entry . Field8 = random . Next < sbyte > ( ) ;
131+ random . Next ( entry . Field9 . AsSpan ( ) ) ;
132+ }
133+
134+ return testData ;
135+ }
151136}
152137
153138[ MemoryPackable ]
0 commit comments