using System; using System.Collections; using System.Collections.Generic; using System.IO; using HeuristicLab.Persistence.Interfaces; namespace HeuristicLab.Persistence.Test { public class RootBase { [Storable] private string baseString = "Serial"; } public class Root : RootBase { [Storable] public int[] i = new[] { 3, 4, 5, 6 }; [Storable] public string s; [Storable] public ArrayList intArray = new ArrayList(new[] { 1, 2, 3 }); [Storable] public List intList = new List(new[] { 321, 312, 321 }); [Storable] public Custom c; [Storable] public List selfReferences; [Storable] public double[,] multiDimArray = new double[,] { { 1, 2, 3 }, { 3, 4, 5 } }; [Storable] public bool boolean = true; [Storable] public DateTime dateTime; [Storable] public KeyValuePair kvp = new KeyValuePair("Serial", 123); [Storable] public Dictionary dict = new Dictionary(); [Storable(DefaultValue = "default")] public string uninitialized; } public class Custom { [Storable] public int i; [Storable] public Root r; [Storable] public string name = "Serial"; } public class CloneableRoot { public int[] i = new[] { 3, 4, 5, 6 }; public string s; public ArrayList intArray = new ArrayList(new[] { 1, 2, 3 }); public List intList = new List(new[] { 321, 312, 321 }); public CloneableCustom c; public List selfReferences; public double[,] multiDimArray = new double[,] { { 1, 2, 3 }, { 3, 4, 5 } }; public bool boolean = true; public DateTime dateTime; public KeyValuePair kvp = new KeyValuePair("test key", 123); public Dictionary dict = new Dictionary(); public object Clone(Dictionary twins) { if (twins.ContainsKey(this)) return twins[this]; CloneableRoot cr = new CloneableRoot(); twins.Add(this, cr); cr.i = i; cr.s = s; cr.intArray = new ArrayList(intArray); cr.intList = new List(intList); cr.c = (CloneableCustom)c.Clone(twins); cr.selfReferences = new List(); for (int j = 0; j < selfReferences.Count; j++) { cr.selfReferences.Add(this); } cr.multiDimArray = (double[,])multiDimArray.Clone(); cr.dateTime = new DateTime(dateTime.Ticks); cr.kvp = new KeyValuePair(kvp.Key, kvp.Value); cr.dict = new Dictionary(dict); return cr; } } public class CloneableCustom { public int i; public CloneableRoot r; public string name = "Serial"; public object Clone(Dictionary twins) { if (twins.ContainsKey(this)) return twins[this]; CloneableCustom cc = new CloneableCustom(); twins.Add(this, cc); cc.i = i; cc.r = (CloneableRoot)r.Clone(twins); cc.name = name; return cc; } } public class Manager { private DateTime lastLoadTime; [Storable] private DateTime lastLoadTimePersistence { get { return lastLoadTime; } // ReSharper disable ValueParameterNotUsed set { lastLoadTime = DateTime.Now; } // ReSharper restore ValueParameterNotUsed } [Storable] private double? dbl; } public class StorableObject { [Storable] Dictionary dict; public void Init() { dict = new Dictionary(); for (int i = 0; i < 1000000; i++) { dict.Add(i, i.ToString()); } } } public class CloneableObject : ICloneable { Dictionary dict; public void Init() { dict = new Dictionary(); for (int i = 0; i < 1000000; i++) { dict.Add(i, i.ToString()); } } public object Clone() { CloneableObject clone = new CloneableObject { dict = new Dictionary(dict) }; return clone; } } public class NewSerializationTest { public static void Test1() { Root r = new Root(); r.selfReferences = new List {r, r}; r.c = new Custom {r = r}; r.dict.Add("one", 1); r.dict.Add("two", 2); r.dict.Add("three", 3); Serializer s = new Serializer(r, ConfigurationService.Instance.GetDefaultConfig(XmlFormat.Instance)); Persistence.XmlFormatter xmlFormatter = new XmlFormatter(); StreamWriter writer = new StreamWriter("test.xml"); foreach (ISerializationToken token in s) { string line = xmlFormatter.Format(token); writer.Write(line); Console.Out.Write(line); } writer.Close(); writer = new StreamWriter("test-types.xml"); foreach (string line in xmlFormatter.Format(s.TypeCache)) { writer.WriteLine(line); Console.Out.WriteLine(line); } writer.Close(); XmlParser parser = new XmlParser(new StreamReader("test.xml")); DeSerializer deSerializer = new DeSerializer( XmlParser.ParseTypeCache(new StreamReader("test-types.xml")), ConfigurationService.Instance.GetDefaultConfig(XmlFormat.Instance)); object o = deSerializer.DeSerialize(parser); Root t = CloningFactory.DefaultClone(r); Console.Out.WriteLine(Util.AutoFormat(o, true)); } public static void Test2() { Manager m = new Manager(); Serializer s = new Serializer(m, ConfigurationService.Instance.GetDefaultConfig(XmlFormat.Instance)); Persistence.XmlFormatter xmlFormatter = new XmlFormatter(); StreamWriter writer = new StreamWriter("test2.xml"); foreach (ISerializationToken token in s) { string line = xmlFormatter.Format(token); writer.Write(line); Console.Out.Write(line); } writer.Close(); XmlParser parser = new XmlParser(new StreamReader("test2.xml")); DeSerializer deSerializer = new DeSerializer( XmlParser.ParseTypeCache(new StreamReader("test-types.xml")), ConfigurationService.Instance.GetDefaultConfig(XmlFormat.Instance)); object o = deSerializer.DeSerialize(parser); Manager n = CloningFactory.DefaultClone(m); Console.Out.WriteLine(Util.AutoFormat(o, true)); } public static void SpeedTest() { StorableObject storable = new StorableObject(); CloneableObject cloneable = new CloneableObject(); Console.Write("initializing..."); storable.Init(); cloneable.Init(); Console.WriteLine("done"); List storableClones = new List(); List clonableClones = new List(); CloningFactory cloningFactory = new CloningFactory(); for (int i = 0; i < 100000; i++) { Console.Write("cloning storable.. "); storableClones.Add(cloningFactory.Clone(storable)); Console.WriteLine(); Console.Write("cloning cloneable.. "); clonableClones.Add((CloneableObject)cloneable.Clone()); Console.WriteLine(); } } public static void SpeedTest2() { Root r = new Root(); r.selfReferences = new List {r, r}; r.c = new Custom {r = r}; r.dict.Add("one", 1); r.dict.Add("two", 2); r.dict.Add("three", 3); CloneableRoot cr = new CloneableRoot(); cr.selfReferences = new List {cr, cr}; cr.c = new CloneableCustom {r = cr}; cr.dict.Add("one", 1); cr.dict.Add("two", 2); cr.dict.Add("three", 3); List storableClones = new List(); List clonableClones = new List(); CloningFactory cloningFactory = new CloningFactory(); DateTime start = DateTime.Now; Console.Write("cloning storable.. "); for (int i = 0; i < 100000; i++) { storableClones.Add(cloningFactory.Clone(r)); } Console.WriteLine(new TimeSpan(DateTime.Now.Ticks - start.Ticks)); start = DateTime.Now; Console.Write("cloning storable again.. "); for (int i = 0; i < 100000; i++) { storableClones.Add(cloningFactory.Clone(r)); } Console.WriteLine(new TimeSpan(DateTime.Now.Ticks - start.Ticks)); start = DateTime.Now; Console.Write("cloning cloneable.. "); for (int i = 0; i < 100000; i++) { clonableClones.Add((CloneableRoot)cr.Clone(new Dictionary())); } Console.WriteLine(new TimeSpan(DateTime.Now.Ticks - start.Ticks)); clonableClones.Clear(); } public class Base { [Storable] private int baseInt = 3; } public class Derived: Base { [Storable] private int derivedInt = 5; } public static void Main() { Test1(); //Test2(); //SpeedTest(); //SpeedTest2(); Console.In.ReadLine(); } } }