1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Collections;
|
---|
4 | using System.IO;
|
---|
5 |
|
---|
6 | namespace Persistence.Test {
|
---|
7 |
|
---|
8 | public class Root {
|
---|
9 | [Storable]
|
---|
10 | public int[] i = new[] { 3, 4, 5, 6 };
|
---|
11 | [Storable]
|
---|
12 | public string s;
|
---|
13 | [Storable]
|
---|
14 | public ArrayList intArray = new ArrayList(new[] { 1, 2, 3 });
|
---|
15 | [Storable]
|
---|
16 | public List<int> intList = new List<int>(new[] { 321, 312, 321 });
|
---|
17 | [Storable]
|
---|
18 | public Custom c;
|
---|
19 | [Storable]
|
---|
20 | public List<Root> selfReferences;
|
---|
21 | [Storable]
|
---|
22 | public double[,] multiDimArray = new double[,] { { 1, 2, 3 }, { 3, 4, 5 } };
|
---|
23 | [Storable]
|
---|
24 | public bool boolean = true;
|
---|
25 | [Storable]
|
---|
26 | public DateTime dateTime;
|
---|
27 | [Storable]
|
---|
28 | public KeyValuePair<string, int> kvp = new KeyValuePair<string, int>("Serial", 123);
|
---|
29 | [Storable]
|
---|
30 | public Dictionary<string, int> dict = new Dictionary<string, int>();
|
---|
31 | }
|
---|
32 |
|
---|
33 | public class Custom {
|
---|
34 | [Storable]
|
---|
35 | public int i;
|
---|
36 | [Storable]
|
---|
37 | public Root r;
|
---|
38 | [Storable]
|
---|
39 | public string name = "Serial";
|
---|
40 | }
|
---|
41 |
|
---|
42 | public class CloneableRoot {
|
---|
43 | public int[] i = new[] { 3, 4, 5, 6 };
|
---|
44 | public string s;
|
---|
45 | public ArrayList intArray = new ArrayList(new[] { 1, 2, 3 });
|
---|
46 | public List<int> intList = new List<int>(new[] { 321, 312, 321 });
|
---|
47 | public CloneableCustom c;
|
---|
48 | public List<CloneableRoot> selfReferences;
|
---|
49 | public double[,] multiDimArray = new double[,] { { 1, 2, 3 }, { 3, 4, 5 } };
|
---|
50 | public bool boolean = true;
|
---|
51 | public DateTime dateTime;
|
---|
52 | public KeyValuePair<string, int> kvp = new KeyValuePair<string, int>("test key", 123);
|
---|
53 | public Dictionary<string, int> dict = new Dictionary<string, int>();
|
---|
54 | public object Clone(Dictionary<object, object> twins) {
|
---|
55 | if (twins.ContainsKey(this))
|
---|
56 | return twins[this];
|
---|
57 | CloneableRoot cr = new CloneableRoot();
|
---|
58 | twins.Add(this, cr);
|
---|
59 | cr.i = i;
|
---|
60 | cr.s = s;
|
---|
61 | cr.intArray = new ArrayList(intArray);
|
---|
62 | cr.intList = new List<int>(intList);
|
---|
63 | cr.c = (CloneableCustom)c.Clone(twins);
|
---|
64 | cr.selfReferences = new List<CloneableRoot>();
|
---|
65 | for (int j = 0; j < selfReferences.Count; j++) {
|
---|
66 | cr.selfReferences.Add(this);
|
---|
67 | }
|
---|
68 | cr.multiDimArray = (double[,])multiDimArray.Clone();
|
---|
69 | cr.dateTime = new DateTime(dateTime.Ticks);
|
---|
70 | cr.kvp = new KeyValuePair<string, int>(kvp.Key, kvp.Value);
|
---|
71 | cr.dict = new Dictionary<string, int>(dict);
|
---|
72 | return cr;
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | public class CloneableCustom {
|
---|
77 | public int i;
|
---|
78 | public CloneableRoot r;
|
---|
79 | public string name = "Serial";
|
---|
80 | public object Clone(Dictionary<object, object> twins) {
|
---|
81 | if (twins.ContainsKey(this))
|
---|
82 | return twins[this];
|
---|
83 | CloneableCustom cc = new CloneableCustom();
|
---|
84 | twins.Add(this, cc);
|
---|
85 | cc.i = i;
|
---|
86 | cc.r = (CloneableRoot)r.Clone(twins);
|
---|
87 | cc.name = name;
|
---|
88 | return cc;
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | public class Manager {
|
---|
93 |
|
---|
94 | private DateTime lastLoadTime;
|
---|
95 | [Storable]
|
---|
96 | private DateTime lastLoadTimePersistence {
|
---|
97 | get { return lastLoadTime; }
|
---|
98 | // ReSharper disable ValueParameterNotUsed
|
---|
99 | set { lastLoadTime = DateTime.Now; }
|
---|
100 | // ReSharper restore ValueParameterNotUsed
|
---|
101 | }
|
---|
102 | [Storable]
|
---|
103 | private double? dbl;
|
---|
104 | }
|
---|
105 |
|
---|
106 | public class StorableObject {
|
---|
107 |
|
---|
108 | [Storable]
|
---|
109 | Dictionary<int, string> dict;
|
---|
110 |
|
---|
111 | public void Init() {
|
---|
112 | dict = new Dictionary<int, string>();
|
---|
113 | for (int i = 0; i < 1000000; i++) {
|
---|
114 | dict.Add(i, i.ToString());
|
---|
115 | }
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | public class CloneableObject : ICloneable {
|
---|
120 |
|
---|
121 | Dictionary<int, string> dict;
|
---|
122 |
|
---|
123 | public void Init() {
|
---|
124 | dict = new Dictionary<int, string>();
|
---|
125 | for (int i = 0; i < 1000000; i++) {
|
---|
126 | dict.Add(i, i.ToString());
|
---|
127 | }
|
---|
128 | }
|
---|
129 | public object Clone() {
|
---|
130 | CloneableObject clone = new CloneableObject{
|
---|
131 | dict = new Dictionary<int, string>(dict)
|
---|
132 | };
|
---|
133 | return clone;
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | public class NewSerializationTest {
|
---|
138 |
|
---|
139 | public static void Test1() {
|
---|
140 | Root r = new Root();
|
---|
141 | r.selfReferences = new List<Root> {r, r};
|
---|
142 | r.c = new Custom {r = r};
|
---|
143 | r.dict.Add("one", 1);
|
---|
144 | r.dict.Add("two", 2);
|
---|
145 | r.dict.Add("three", 3);
|
---|
146 | Serializer s = new Serializer(r);
|
---|
147 | Persistence.XmlFormatter xmlFormatter = new XmlFormatter();
|
---|
148 | StreamWriter writer = new StreamWriter("test.xml");
|
---|
149 | foreach (ISerializationToken token in s) {
|
---|
150 | string line = xmlFormatter.Format(token);
|
---|
151 | writer.Write(line);
|
---|
152 | Console.Out.Write(line);
|
---|
153 | }
|
---|
154 | writer.Close();
|
---|
155 | XmlParser parser = new XmlParser(new StreamReader("test.xml"));
|
---|
156 | DeSerializer deSerializer = new DeSerializer();
|
---|
157 | object o = deSerializer.DeSerialize(parser);
|
---|
158 | Root t = CloningFactory.DefaultClone(r);
|
---|
159 | Console.Out.WriteLine(Util.AutoFormat(o, true));
|
---|
160 | }
|
---|
161 |
|
---|
162 | public static void Test2() {
|
---|
163 | Manager m = new Manager();
|
---|
164 | Serializer s = new Serializer(m);
|
---|
165 | Persistence.XmlFormatter xmlFormatter = new XmlFormatter();
|
---|
166 | StreamWriter writer = new StreamWriter("test2.xml");
|
---|
167 | foreach (ISerializationToken token in s) {
|
---|
168 | string line = xmlFormatter.Format(token);
|
---|
169 | writer.Write(line);
|
---|
170 | Console.Out.Write(line);
|
---|
171 | }
|
---|
172 | writer.Close();
|
---|
173 | XmlParser parser = new XmlParser(new StreamReader("test2.xml"));
|
---|
174 | DeSerializer deSerializer = new DeSerializer();
|
---|
175 | object o = deSerializer.DeSerialize(parser);
|
---|
176 | Manager n = CloningFactory.DefaultClone(m);
|
---|
177 | Console.Out.WriteLine(Util.AutoFormat(o, true));
|
---|
178 | }
|
---|
179 |
|
---|
180 | public static void SpeedTest() {
|
---|
181 | StorableObject storable = new StorableObject();
|
---|
182 | CloneableObject cloneable = new CloneableObject();
|
---|
183 | Console.Write("initializing...");
|
---|
184 | storable.Init();
|
---|
185 | cloneable.Init();
|
---|
186 | Console.WriteLine("done");
|
---|
187 | List<StorableObject> storableClones = new List<StorableObject>();
|
---|
188 | List<CloneableObject> clonableClones = new List<CloneableObject>();
|
---|
189 | CloningFactory cloningFactory = new CloningFactory();
|
---|
190 | for (int i = 0; i < 100000; i++) {
|
---|
191 | Console.Write("cloning storable.. ");
|
---|
192 | storableClones.Add(cloningFactory.Clone(storable));
|
---|
193 | Console.WriteLine();
|
---|
194 | Console.Write("cloning cloneable.. ");
|
---|
195 | clonableClones.Add((CloneableObject)cloneable.Clone());
|
---|
196 | Console.WriteLine();
|
---|
197 | }
|
---|
198 | }
|
---|
199 |
|
---|
200 | public static void SpeedTest2() {
|
---|
201 | Root r = new Root();
|
---|
202 | r.selfReferences = new List<Root> {r, r};
|
---|
203 | r.c = new Custom {r = r};
|
---|
204 | r.dict.Add("one", 1);
|
---|
205 | r.dict.Add("two", 2);
|
---|
206 | r.dict.Add("three", 3);
|
---|
207 |
|
---|
208 | CloneableRoot cr = new CloneableRoot();
|
---|
209 | cr.selfReferences = new List<CloneableRoot> {cr, cr};
|
---|
210 | cr.c = new CloneableCustom {r = cr};
|
---|
211 | cr.dict.Add("one", 1);
|
---|
212 | cr.dict.Add("two", 2);
|
---|
213 | cr.dict.Add("three", 3);
|
---|
214 | List<Root> storableClones = new List<Root>();
|
---|
215 | List<CloneableRoot> clonableClones = new List<CloneableRoot>();
|
---|
216 | CloningFactory cloningFactory = new CloningFactory();
|
---|
217 |
|
---|
218 | DateTime start = DateTime.Now;
|
---|
219 | Console.Write("cloning storable.. ");
|
---|
220 | for (int i = 0; i < 100000; i++) {
|
---|
221 | storableClones.Add(cloningFactory.Clone(r));
|
---|
222 | }
|
---|
223 | Console.WriteLine(new TimeSpan(DateTime.Now.Ticks - start.Ticks));
|
---|
224 |
|
---|
225 | start = DateTime.Now;
|
---|
226 | Console.Write("cloning storable again.. ");
|
---|
227 | for (int i = 0; i < 100000; i++) {
|
---|
228 | storableClones.Add(cloningFactory.Clone(r));
|
---|
229 | }
|
---|
230 | Console.WriteLine(new TimeSpan(DateTime.Now.Ticks - start.Ticks));
|
---|
231 |
|
---|
232 | start = DateTime.Now;
|
---|
233 | Console.Write("cloning cloneable.. ");
|
---|
234 | for (int i = 0; i < 100000; i++) {
|
---|
235 | clonableClones.Add((CloneableRoot)cr.Clone(new Dictionary<object, object>()));
|
---|
236 | }
|
---|
237 | Console.WriteLine(new TimeSpan(DateTime.Now.Ticks - start.Ticks));
|
---|
238 | clonableClones.Clear();
|
---|
239 |
|
---|
240 | }
|
---|
241 |
|
---|
242 | public static void Main() {
|
---|
243 | Test1();
|
---|
244 | //Test2();
|
---|
245 | //SpeedTest();
|
---|
246 | //SpeedTest2();
|
---|
247 | Console.In.ReadLine();
|
---|
248 | }
|
---|
249 | }
|
---|
250 | }
|
---|