Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/08/19 00:23:12 (5 years ago)
Author:
mkommend
Message:

#2520: Merged 16584, 16585,16594,16595, 16625, 16658, 16659, 16672, 16707, 16729, 16792, 16796, 16797, 16799, 16819, 16906, 16907, 16908, 16933, 16945, 16992, 16994, 16995, 16996, 16997, 17014, 17015, 17017, 17020, 17021, 17022, 17023, 17024, 17029, 17086, 17087, 17088, 17089 into stable.

Location:
stable
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Tests

  • stable/HeuristicLab.Tests/HeuristicLab.Persistence.Attic/UseCases.cs

    r17097 r17105  
    2222using System;
    2323using System.Collections.Generic;
     24using System.Diagnostics;
    2425using System.Drawing;
    2526using System.IO;
     27using System.IO.Compression;
    2628using System.Linq;
    2729using System.Reflection;
     
    3537using HeuristicLab.Persistence.Core;
    3638using HeuristicLab.Persistence.Default.Xml;
     39using HeuristicLab.Persistence.Interfaces;
    3740using HeuristicLab.Tests;
    3841using Microsoft.VisualStudio.TestTools.UnitTesting;
     
    155158    [TestCategory("Persistence.Attic")]
    156159    [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")]
    157332    public void TestLoadingSamples() {
    158333      CreateAllSamples();
     
    160335      var serializer = new ProtoBufSerializer();
    161336      foreach (var fileName in Directory.EnumerateFiles(path, "*.hl")) {
    162         var original = XmlParser.Deserialize(fileName);
     337        var original = serializer.Deserialize(fileName);
    163338        var ok = true;
    164339        foreach (var t in original.GetObjectGraphObjects().Select(o => o.GetType())) {
     
    195370      var serializer = new ProtoBufSerializer();
    196371      foreach (var fileName in Directory.EnumerateFiles(path, "*.hl")) {
    197         var original = XmlParser.Deserialize(fileName);
     372        var original = serializer.Deserialize(fileName);
    198373
    199374        var exec = original as IExecutable;
Note: See TracChangeset for help on using the changeset viewer.