Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Tests/HeuristicLab.Persistence.Attic/UseCases.cs @ 17119

Last change on this file since 17119 was 17024, checked in by mkommend, 5 years ago

#2520: Adapted unit test for loading samples.

File size: 16.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Diagnostics;
25using System.Drawing;
26using System.IO;
27using System.IO.Compression;
28using System.Linq;
29using System.Reflection;
30using System.Threading.Tasks;
31using HEAL.Attic;
32using HeuristicLab.Algorithms.GeneticAlgorithm;
33using HeuristicLab.Analysis;
34using HeuristicLab.Common;
35using HeuristicLab.Core;
36using HeuristicLab.Data;
37using HeuristicLab.Persistence.Core;
38using HeuristicLab.Persistence.Default.Xml;
39using HeuristicLab.Persistence.Interfaces;
40using HeuristicLab.Tests;
41using Microsoft.VisualStudio.TestTools.UnitTesting;
42
43namespace HeuristicLab.Persistence.Attic.Tests {
44  [TestClass]
45  public class UseCases {
46
47    private string tempFile;
48
49    [TestInitialize()]
50    public void CreateTempFile() {
51      tempFile = Path.GetTempFileName();
52    }
53
54    [TestCleanup()]
55    public void ClearTempFile() {
56      StreamReader reader = new StreamReader(tempFile);
57      string s = reader.ReadToEnd();
58      reader.Close();
59      File.Delete(tempFile);
60    }
61
62    [TestMethod]
63    [TestCategory("Persistence.Attic")]
64    [TestProperty("Time", "short")]
65    public void BitmapTest() {
66      Icon icon = System.Drawing.SystemIcons.Hand;
67      Bitmap bitmap = icon.ToBitmap();
68      new ProtoBufSerializer().Serialize(bitmap, tempFile);
69      Bitmap newBitmap = (Bitmap)new ProtoBufSerializer().Deserialize(tempFile);
70
71      Assert.AreEqual(bitmap.Size, newBitmap.Size);
72      for (int i = 0; i < bitmap.Size.Width; i++)
73        for (int j = 0; j < bitmap.Size.Height; j++)
74          Assert.AreEqual(bitmap.GetPixel(i, j), newBitmap.GetPixel(i, j));
75    }
76
77
78    [TestMethod]
79    [TestCategory("Persistence.Attic")]
80    [TestProperty("Time", "short")]
81    public void FontTest() {
82      List<Font> fonts = new List<Font>() {
83        new Font(FontFamily.GenericSansSerif, 12),
84        new Font("Times New Roman", 21, FontStyle.Bold, GraphicsUnit.Pixel),
85        new Font("Courier New", 10, FontStyle.Underline, GraphicsUnit.Document),
86        new Font("Helvetica", 21, FontStyle.Strikeout, GraphicsUnit.Inch, 0, true),
87      };
88      new ProtoBufSerializer().Serialize(fonts, tempFile);
89      var newFonts = (List<Font>)new ProtoBufSerializer().Deserialize(tempFile);
90      Assert.AreEqual(fonts[0], newFonts[0]);
91      Assert.AreEqual(fonts[1], newFonts[1]);
92      Assert.AreEqual(fonts[2], newFonts[2]);
93      Assert.AreEqual(fonts[3], newFonts[3]);
94    }
95
96    [TestMethod]
97    [TestCategory("Persistence.Attic")]
98    [TestProperty("Time", "medium")]
99    public void ConcurrencyTest() {
100      int n = 20;
101      Task[] tasks = new Task[n];
102      for (int i = 0; i < n; i++) {
103        tasks[i] = Task.Factory.StartNew((idx) => {
104          byte[] data;
105          using (var stream = new MemoryStream()) {
106            new ProtoBufSerializer().Serialize(new GeneticAlgorithm(), stream);
107            data = stream.ToArray();
108          }
109        }, i);
110      }
111      Task.WaitAll(tasks);
112    }
113
114    [TestMethod]
115    [TestCategory("Persistence.Attic")]
116    [TestProperty("Time", "medium")]
117    public void ConcurrentBitmapTest() {
118      Bitmap b = new Bitmap(300, 300);
119      System.Random r = new System.Random();
120      for (int x = 0; x < b.Height; x++) {
121        for (int y = 0; y < b.Width; y++) {
122          b.SetPixel(x, y, Color.FromArgb(r.Next()));
123        }
124      }
125      Task[] tasks = new Task[20];
126      byte[][] datas = new byte[tasks.Length][];
127      for (int i = 0; i < tasks.Length; i++) {
128        tasks[i] = Task.Factory.StartNew((idx) => {
129          using (var stream = new MemoryStream()) {
130            new ProtoBufSerializer().Serialize(b, stream);
131            datas[(int)idx] = stream.ToArray();
132          }
133        }, i);
134      }
135      Task.WaitAll(tasks);
136    }
137
138    private void CreateAllSamples() {
139      var asm = this.GetType().Assembly;
140      foreach (var t in asm.GetTypes()) {
141        var attrs = t.GetCustomAttributes<TestClassAttribute>();
142        if (attrs.Any()) {
143          try {
144            var testObj = Activator.CreateInstance(t);
145            foreach (var mi in t.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) {
146              var mAttrs = mi.GetCustomAttributes<TestCategoryAttribute>();
147              var testCategories = mAttrs.SelectMany(mattr => mattr.TestCategories);
148              if (testCategories.Any(tc => tc == "Samples.Create")) {
149                mi.Invoke(testObj, new object[0]);
150              }
151            }
152          } catch (Exception) { }
153        }
154      }
155    }
156
157    [TestMethod]
158    [TestCategory("Persistence.Attic")]
159    [TestProperty("Time", "long")]
160    public void ProfileSamples() {
161      // CreateAllSamples();
162      var path = SamplesUtils.SamplesDirectory;
163      foreach (var fileName in Directory.EnumerateFiles(path, "*.hl")) {
164        //ProfilePersistenceToMemory(fileName);
165        ProfilePersistenceToDisk(fileName);
166      }
167    }
168
169    private void ProfilePersistenceToMemory(string fileName) {
170      var REPS = 5;
171      var oldDeserStopwatch = new Stopwatch();
172      var oldSerStopwatch = new Stopwatch();
173      var newDeserStopwatch = new Stopwatch();
174      var newSerStopwatch = new Stopwatch();
175      long oldSize = 0, newSize = 0;
176      var config = ConfigurationService.Instance.GetConfiguration(new XmlFormat());
177      int[] oldCollections = new int[3];
178      int[] newCollections = new int[3];
179
180      for (int i = 0; i < REPS; i++) {
181        var original = XmlParser.Deserialize(fileName);
182        object deserializedObject1 = null;
183        object deserializedObject2 = null;
184        byte[] buf;
185        System.GC.Collect();
186        var collection0 = System.GC.CollectionCount(0);
187        var collection1 = System.GC.CollectionCount(1);
188        var collection2 = System.GC.CollectionCount(2);
189        using (var s = new MemoryStream()) {
190
191          oldSerStopwatch.Start();
192          // serialize manually so that the stream won't be closed
193          var serializer = new Core.Serializer(original, config);
194          serializer.InterleaveTypeInformation = true;
195          using (StreamWriter writer = new StreamWriter(new GZipStream(s, CompressionMode.Compress))) {
196            XmlGenerator generator = new XmlGenerator();
197            foreach (ISerializationToken token in serializer) {
198              writer.Write(generator.Format(token));
199            }
200            writer.Flush();
201            oldSize += s.Length;
202          }
203
204          oldSerStopwatch.Stop();
205          buf = s.GetBuffer();
206        }
207        using (var s = new MemoryStream(buf)) {
208          oldDeserStopwatch.Start();
209          deserializedObject1 = XmlParser.Deserialize(s);
210          oldDeserStopwatch.Stop();
211        }
212
213        System.GC.Collect();
214        oldCollections[0] += System.GC.CollectionCount(0) - collection0;
215        oldCollections[1] += System.GC.CollectionCount(1) - collection1;
216        oldCollections[2] += System.GC.CollectionCount(2) - collection2;
217
218        collection0 = System.GC.CollectionCount(0);
219        collection1 = System.GC.CollectionCount(1);
220        collection2 = System.GC.CollectionCount(2);
221
222        // Protobuf only uses Deflate
223        using (var m = new MemoryStream()) {
224          //         using (var s = new GZipStream(m, CompressionMode.Compress)) { // same as old persistence
225          using (var s = new DeflateStream(m, CompressionMode.Compress)) { // new format
226            newSerStopwatch.Start();
227            (new ProtoBufSerializer()).Serialize(original, s, false);
228            s.Flush();
229            newSerStopwatch.Stop();
230            newSize += m.Length;
231          }
232          buf = m.GetBuffer();
233        }
234        using (var m = new MemoryStream(buf)) {
235          // using (var s = new GZipStream(m, CompressionMode.Decompress)) { // same as old persistence
236          using (var s = new DeflateStream(m, CompressionMode.Decompress)) { // new format
237            newDeserStopwatch.Start();
238            deserializedObject2 = (new ProtoBufSerializer()).Deserialize(s);
239            newDeserStopwatch.Stop();
240          }
241        }
242        //Assert.AreEqual(deserializedObject1.GetObjectGraphObjects().Count(), deserializedObject2.GetObjectGraphObjects().Count());
243
244        System.GC.Collect();
245        newCollections[0] += System.GC.CollectionCount(0) - collection0;
246        newCollections[1] += System.GC.CollectionCount(1) - collection1;
247        newCollections[2] += System.GC.CollectionCount(2) - collection2;
248      }
249
250      Console.WriteLine($"{fileName} " +
251        $"{oldSize / (double)REPS} " +
252        $"{newSize / (double)REPS} " +
253        $"{oldSerStopwatch.ElapsedMilliseconds / (double)REPS} " +
254        $"{newSerStopwatch.ElapsedMilliseconds / (double)REPS} " +
255        $"{oldDeserStopwatch.ElapsedMilliseconds / (double)REPS} " +
256        $"{newDeserStopwatch.ElapsedMilliseconds / (double)REPS} " +
257        $"{oldCollections[0] / (double)REPS} " +
258        $"{newCollections[0] / (double)REPS} " +
259        $"{oldCollections[1] / (double)REPS} " +
260        $"{newCollections[1] / (double)REPS} " +
261        $"{oldCollections[2] / (double)REPS} " +
262        $"{newCollections[2] / (double)REPS} " +
263        $"");
264    }
265
266    private void ProfilePersistenceToDisk(string fileName) {
267      var REPS = 5;
268      var oldDeserStopwatch = new Stopwatch();
269      var oldSerStopwatch = new Stopwatch();
270      var newDeserStopwatch = new Stopwatch();
271      var newSerStopwatch = new Stopwatch();
272      long oldSize = 0, newSize = 0;
273      int[] oldCollections = new int[3];
274      int[] newCollections = new int[3];
275
276      for (int i = 0; i < REPS; i++) {
277        var original = XmlParser.Deserialize(fileName);
278        System.GC.Collect();
279        var collection0 = System.GC.CollectionCount(0);
280        var collection1 = System.GC.CollectionCount(1);
281        var collection2 = System.GC.CollectionCount(2);
282
283        oldSerStopwatch.Start();
284        XmlGenerator.Serialize(original, tempFile);
285        oldSerStopwatch.Stop();
286
287        oldSize += new FileInfo(tempFile).Length;
288        oldDeserStopwatch.Start();
289        var clone = XmlParser.Deserialize(tempFile);
290        oldDeserStopwatch.Stop();
291        System.GC.Collect();
292        oldCollections[0] += System.GC.CollectionCount(0) - collection0;
293        oldCollections[1] += System.GC.CollectionCount(1) - collection1;
294        oldCollections[2] += System.GC.CollectionCount(2) - collection2;
295
296        collection0 = System.GC.CollectionCount(0);
297        collection1 = System.GC.CollectionCount(1);
298        collection2 = System.GC.CollectionCount(2);
299
300        newSerStopwatch.Start();
301        (new ProtoBufSerializer()).Serialize(original, tempFile);
302        newSerStopwatch.Stop();
303        newSize += new FileInfo(tempFile).Length;
304        newDeserStopwatch.Start();
305        var newClone = (new ProtoBufSerializer()).Deserialize(tempFile);
306        newDeserStopwatch.Stop();
307        System.GC.Collect();
308        newCollections[0] += System.GC.CollectionCount(0) - collection0;
309        newCollections[1] += System.GC.CollectionCount(1) - collection1;
310        newCollections[2] += System.GC.CollectionCount(2) - collection2;
311      }
312      Console.WriteLine($"{fileName} " +
313        $"{oldSize / (double)REPS} " +
314        $"{newSize / (double)REPS} " +
315        $"{oldSerStopwatch.ElapsedMilliseconds / (double)REPS} " +
316        $"{newSerStopwatch.ElapsedMilliseconds / (double)REPS} " +
317        $"{oldDeserStopwatch.ElapsedMilliseconds / (double)REPS} " +
318        $"{newDeserStopwatch.ElapsedMilliseconds / (double)REPS} " +
319        $"{oldCollections[0] / (double)REPS} " +
320        $"{newCollections[0] / (double)REPS} " +
321        $"{oldCollections[1] / (double)REPS} " +
322        $"{newCollections[1] / (double)REPS} " +
323        $"{oldCollections[2] / (double)REPS} " +
324        $"{newCollections[2] / (double)REPS} " +
325        $"");
326    }
327
328
329    [TestMethod]
330    [TestCategory("Persistence.Attic")]
331    [TestProperty("Time", "long")]
332    public void TestLoadingSamples() {
333      CreateAllSamples();
334      var path = SamplesUtils.SamplesDirectory;
335      var serializer = new ProtoBufSerializer();
336      foreach (var fileName in Directory.EnumerateFiles(path, "*.hl")) {
337        var original = serializer.Deserialize(fileName);
338        var ok = true;
339        foreach (var t in original.GetObjectGraphObjects().Select(o => o.GetType())) {
340          if (
341            t.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
342              .Any(ctor => StorableConstructorAttribute.IsStorableConstructor(ctor))) {
343            try {
344              if (t.IsGenericType) {
345                var g = Mapper.StaticCache.GetGuid(t.GetGenericTypeDefinition());
346              } else {
347                var g = Mapper.StaticCache.GetGuid(t);
348              }
349            } catch (Exception e) {
350              Console.WriteLine($"type {t.FullName} in {fileName} is not registered with a GUID in HEAL.Attic");
351              ok = false;
352            }
353          }
354        }
355        if (ok) {
356          serializer.Serialize(original, fileName + ".proto");
357          var newVersion = serializer.Deserialize(fileName + ".proto");
358          Console.WriteLine("File: " + fileName);
359          File.Delete(fileName + ".proto");
360        }
361      }
362    }
363
364    [TestMethod]
365    [TestCategory("Persistence.Attic")]
366    [TestProperty("Time", "long")]
367    public void TestLoadingRunAndStoreSamples() {
368      CreateAllSamples();
369      var path = SamplesUtils.SamplesDirectory;
370      var serializer = new ProtoBufSerializer();
371      foreach (var fileName in Directory.EnumerateFiles(path, "*.hl")) {
372        var original = serializer.Deserialize(fileName);
373
374        var exec = original as IExecutable;
375        if (exec != null) {
376          exec.Paused += (sender, e) => {
377            serializer.Serialize(exec, fileName + "_paused.proto");
378            Console.WriteLine("Serialized paused file: " + fileName);
379            File.Delete(fileName + "_paused.proto");
380          };
381          exec.Stopped += (sender, e) => {
382            serializer.Serialize(exec, fileName + "_stopped.proto");
383            Console.WriteLine("Serialized stopped file: " + fileName);
384            File.Delete(fileName + "_stopped.proto");
385          };
386          var t = exec.StartAsync();
387          System.Threading.Thread.Sleep(20000); // wait 20 secs
388          if (exec.ExecutionState == ExecutionState.Started) { // only if not already stopped
389            exec.Pause();
390          }
391        }
392      }
393    }
394
395
396    [TestMethod]
397    [TestCategory("Persistence.Attic")]
398    [TestProperty("Time", "short")]
399    public void TestIndexedDataTable() {
400      var dt = new IndexedDataTable<int>("test", "test description");
401      var dr = new IndexedDataRow<int>("test row");
402      dr.Values.Add(Tuple.Create(1, 1.0));
403      dr.Values.Add(Tuple.Create(2, 2.0));
404      dr.Values.Add(Tuple.Create(3, 3.0));
405      dt.Rows.Add(dr);
406      var ser = new ProtoBufSerializer();
407      ser.Serialize(dt, tempFile);
408      var dt2 = (IndexedDataTable<int>)ser.Deserialize(tempFile);
409      Assert.AreEqual(dt.Rows["test row"].Values[0], dt2.Rows["test row"].Values[0]);
410      Assert.AreEqual(dt.Rows["test row"].Values[1], dt2.Rows["test row"].Values[1]);
411      Assert.AreEqual(dt.Rows["test row"].Values[2], dt2.Rows["test row"].Values[2]);
412    }
413
414    [TestMethod]
415    [TestCategory("Persistence.Attic")]
416    [TestProperty("Time", "short")]
417    public void TestPoint2d() {
418      var tag = new IntValue(10);
419      var p = new Point2D<double>(1.0, 2.0, tag);
420      var ser = new ProtoBufSerializer();
421      ser.Serialize(p, tempFile);
422      var p2 = (Point2D<double>)ser.Deserialize(tempFile);
423      Assert.AreEqual(p.X, p2.X);
424      Assert.AreEqual(p.Y, p2.Y);
425      var tag2 = (IntValue)p2.Tag;
426      Assert.AreEqual(tag.Value, tag2.Value);
427    }
428
429    [ClassInitialize]
430    public static void Initialize(TestContext testContext) {
431      ConfigurationService.Instance.Reset();
432    }
433  }
434}
Note: See TracBrowser for help on using the repository browser.