forked from Alois-xx/SerializerTests
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest_O_N_Behavior.cs
More file actions
137 lines (120 loc) · 5.77 KB
/
Test_O_N_Behavior.cs
File metadata and controls
137 lines (120 loc) · 5.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using SerializerTests.Serializers;
using SerializerTests.TypesToSerialize;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace SerializerTests
{
class Test_O_N_Behavior
{
List<ISerializeDeserializeTester> SerializersToTest;
// capture first time init effects by executing serialize/deserialize with N=1 two times
static int[] ObjectCountToTest = new int[]
{
1, 1, 10, 100, 500, 1000, 10*1000, 50*1000, 100*1000, 200*1000, 500*1000, 800*1000, 1000*1000
};
public Test_O_N_Behavior(List<ISerializeDeserializeTester> serializersToTest)
{
SerializersToTest = serializersToTest;
}
public void TestSerialize(int maxNObjects = 1000 * 1000, int nRuns = 5)
{
Console.WriteLine(GetHeader(Format.Serialize));
foreach (var formatter in SerializersToTest)
{
int NBooks = 0;
for (int i = 0; i < ObjectCountToTest.Length && ObjectCountToTest[i] <= maxNObjects; i++)
{
NBooks = ObjectCountToTest[i];
var times = formatter.TestSerialize(nTimes: nRuns, nObjectsToCreate: NBooks);
Print(formatter, NBooks, times);
if (maxNObjects == 1)
{
break;
}
}
}
}
public void TestDeserialize(int maxNObjects=1000*1000, int nRuns=5)
{
Console.WriteLine(GetHeader(Format.Deserialize));
foreach (var formatter in SerializersToTest)
{
int NBooks = 0;
for (int i = 0; i < ObjectCountToTest.Length && ObjectCountToTest[i] <= maxNObjects; i++)
{
NBooks = ObjectCountToTest[i];
var times = formatter.TestDeserialize(nTimes: nRuns, nObjectsToCreate: NBooks);
Print(formatter, NBooks, times);
if (maxNObjects == 1)
{
break;
}
}
}
}
public void TestCombined(int maxNObjects = 1000 * 1000, int nRuns = 5)
{
Console.WriteLine(GetHeader(Format.Combined));
foreach (var formatter in SerializersToTest)
{
int NBooks = 0;
for (int i = 0; i < ObjectCountToTest.Length && ObjectCountToTest[i] <= maxNObjects; i++)
{
NBooks = ObjectCountToTest[i];
var timesSerialize = formatter.TestSerialize(nTimes: nRuns, nObjectsToCreate: NBooks);
var timesDeserialize = formatter.TestDeserialize(nTimes: nRuns, nObjectsToCreate: NBooks);
Print(formatter, NBooks, timesSerialize, timesDeserialize);
}
}
}
private void Print(ISerializeDeserializeTester formatter, int NBooks, (double firstS, double averageS, long serializedSize) times)
{
string typeName = formatter.GetType().Name;
typeName = typeName.Substring(0, typeName.Length - 2); // omit generic arg `1 of typename
Console.WriteLine($"{typeName}<{formatter.GetType().GetGenericArguments()[0].Name}>\t{NBooks}\t{times.averageS:F3}\t{times.serializedSize}\t{formatter.FileVersion}\t{GetNetCoreVersion() ?? System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription}");
}
private void Print(ISerializeDeserializeTester formatter, int NBooks, (double firstS, double averageS, long serializedSize) timesSerialize, (double firstS, double averageS, long dataSize) timesDeserialize)
{
string typeName = formatter.GetType().Name;
typeName = typeName.Substring(0, typeName.Length - 2); // omit generic arg `1 of typename
Console.WriteLine($"{typeName}<{formatter.GetType().GetGenericArguments()[0].Name}>\t{NBooks}\t{timesSerialize.averageS:F3}\t{timesDeserialize.averageS:F3}\t{timesSerialize.serializedSize}\t{formatter.FileVersion}\t{GetNetCoreVersion() ?? System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription}");
}
enum Format
{
Deserialize,
Serialize,
Combined,
}
string GetHeader(Format format)
{
switch(format)
{
case Format.Combined:
return "Serializer\tObjects\t\"Time to serialize in s\"\t\"Time to deserialize in s\"\t\"Size in bytes\"\tFileVersion\tFramework";
case Format.Deserialize:
return "Serializer\tObjects\t\"Time to deserialize in s\"\t\"Size in bytes\"\tFileVersion\tFramework";
case Format.Serialize:
return "Serializer\tObjects\t\"Time to serialize in s\"\t\"Size in bytes\"\tFileVersion\tFramework";
default:
throw new NotSupportedException($"Output format {format} is not supported.");
}
}
/// <summary>
/// Taken from https://github.com/dotnet/BenchmarkDotNet/issues/448
/// </summary>
/// <returns></returns>
public static string GetNetCoreVersion()
{
var assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly;
var assemblyPath = assembly.CodeBase.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
int netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2)
return ".NET Core " + assemblyPath[netCoreAppIndex + 1];
return null;
}
}
}