Free cookie consent management tool by TermsFeed Policy Generator

Changeset 16594


Ignore:
Timestamp:
02/07/19 17:50:02 (5 years ago)
Author:
gkronber
Message:

#2520 added unit tests for profiling old vs new persistence with samples

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/HeuristicLab.Tests/HeuristicLab.Persistence.Attic/UseCases.cs

    r16578 r16594  
    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;
     
    151154      }
    152155    }
     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        byte[] buf;
     279        System.GC.Collect();
     280        var collection0 = System.GC.CollectionCount(0);
     281        var collection1 = System.GC.CollectionCount(1);
     282        var collection2 = System.GC.CollectionCount(2);
     283
     284        oldSerStopwatch.Start();
     285        XmlGenerator.Serialize(original, tempFile);
     286        oldSerStopwatch.Stop();
     287
     288        oldSize += new FileInfo(tempFile).Length;
     289        oldDeserStopwatch.Start();
     290        var clone = XmlParser.Deserialize(tempFile);
     291        oldDeserStopwatch.Stop();
     292        System.GC.Collect();
     293        oldCollections[0] += System.GC.CollectionCount(0) - collection0;
     294        oldCollections[1] += System.GC.CollectionCount(1) - collection1;
     295        oldCollections[2] += System.GC.CollectionCount(2) - collection2;
     296
     297        collection0 = System.GC.CollectionCount(0);
     298        collection1 = System.GC.CollectionCount(1);
     299        collection2 = System.GC.CollectionCount(2);
     300
     301        newSerStopwatch.Start();
     302        (new ProtoBufSerializer()).Serialize(original, tempFile);
     303        newSerStopwatch.Stop();
     304        newSize += new FileInfo(tempFile).Length;
     305        newDeserStopwatch.Start();
     306        var newClone = (new ProtoBufSerializer()).Deserialize(tempFile);
     307        newDeserStopwatch.Stop();
     308        System.GC.Collect();
     309        newCollections[0] += System.GC.CollectionCount(0) - collection0;
     310        newCollections[1] += System.GC.CollectionCount(1) - collection1;
     311        newCollections[2] += System.GC.CollectionCount(2) - collection2;
     312      }
     313      Console.WriteLine($"{fileName} " +
     314        $"{oldSize / (double)REPS} " +
     315        $"{newSize / (double)REPS} " +
     316        $"{oldSerStopwatch.ElapsedMilliseconds / (double)REPS} " +
     317        $"{newSerStopwatch.ElapsedMilliseconds / (double)REPS} " +
     318        $"{oldDeserStopwatch.ElapsedMilliseconds / (double)REPS} " +
     319        $"{newDeserStopwatch.ElapsedMilliseconds / (double)REPS} " +
     320        $"{oldCollections[0] / (double)REPS} " +
     321        $"{newCollections[0] / (double)REPS} " +
     322        $"{oldCollections[1] / (double)REPS} " +
     323        $"{newCollections[1] / (double)REPS} " +
     324        $"{oldCollections[2] / (double)REPS} " +
     325        $"{newCollections[2] / (double)REPS} " +
     326        $"");
     327    }
     328
    153329
    154330    [TestMethod]
Note: See TracChangeset for help on using the changeset viewer.