forked from KLab/protobuf-net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
103 lines (96 loc) · 3.99 KB
/
Program.cs
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
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using DAL;
using Examples.SimpleStream;
using ProtoBuf;
using ProtoBuf.Meta;
namespace Examples
{
class Program
{
static void Main() {
Console.WriteLine("CLR: " + Environment.Version);
new NWindTests().PerfTestDb();
}
static void Main2() {
SimpleStreamDemo demo = new SimpleStreamDemo();
//const int COUNT = 1000000;
const bool RUN_LEGACY = true;
//demo.PerfTestSimple(COUNT, RUN_LEGACY);
//demo.PerfTestString(COUNT, RUN_LEGACY);
//demo.PerfTestEmbedded(COUNT, RUN_LEGACY);
//demo.PerfTestEnum(COUNT, true);
//demo.PerfTestArray(COUNT, true);
const int NWIND_COUNT = 1000;
DAL.Database db = DAL.NWindTests.LoadDatabaseFromFile<DAL.Database>(RuntimeTypeModel.Default);
Console.WriteLine("Sub-object format: {0}", DAL.Database.SubObjectFormat);
SimpleStreamDemo.LoadTestItem(db, NWIND_COUNT, NWIND_COUNT, false, false, false, true, false, false, null);
DatabaseCompat compat = DAL.NWindTests.LoadDatabaseFromFile<DatabaseCompat>(RuntimeTypeModel.Default);
SimpleStreamDemo.LoadTestItem(compat, NWIND_COUNT, NWIND_COUNT, RUN_LEGACY, false, RUN_LEGACY, true, false, true, null);
DatabaseCompatRem compatRem = DAL.NWindTests.LoadDatabaseFromFile<DatabaseCompatRem>(RuntimeTypeModel.Default);
SimpleStreamDemo.LoadTestItem(compatRem, NWIND_COUNT, NWIND_COUNT, true, false, true, false, false, false, null);
}
public static string GetByteString(byte[] buffer)
{
if (buffer == null) return "[null]";
if (buffer.Length == 0) return "[empty]";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < buffer.Length; i++)
{
sb.Append(buffer[i].ToString("X2")).Append(' ');
}
sb.Length -= 1;
return sb.ToString();
}
public static string GetByteString<T>(T item) where T : class,new()
{
using (MemoryStream ms = new MemoryStream())
{
Serializer.Serialize(ms, item);
byte[] actual = ms.ToArray();
return GetByteString(actual);
}
}
public static bool CheckBytes<T>(T item, TypeModel model, params byte[] expected)
{
if (model == null) model = RuntimeTypeModel.Default;
using (MemoryStream ms = new MemoryStream())
{
model.Serialize(ms, item);
byte[] actual = ms.ToArray();
bool equal = Program.ArraysEqual(actual, expected);
if (!equal)
{
string exp = GetByteString(expected), act = GetByteString(actual);
Console.WriteLine("Expected: {0}", exp);
Console.WriteLine("Actual: {0}", act);
}
return equal;
}
}
public static bool CheckBytes<T>(T item, params byte[] expected)
{
return CheckBytes<T>(item, null, expected);
}
public static T Build<T>(params byte[] raw) where T : class, new()
{
using (MemoryStream ms = new MemoryStream(raw))
{
return Serializer.Deserialize<T>(ms);
}
}
public static bool ArraysEqual(byte[] actual, byte[] expected)
{
if (ReferenceEquals(actual, expected)) return true;
if (actual == null || expected == null) return false;
if (actual.Length != expected.Length) return false;
for (int i = 0; i < actual.Length; i++)
{
if (actual[i] != expected[i]) return false;
}
return true;
}
}
}