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