コード例 #1
0
        /// <summary>
        /// Saves the settings (i.e custom configurations).
        /// </summary>
        protected void SaveSettings()
        {
            Serializer serializer = new Serializer(
                customConfigurations,
                GetDefaultConfig(new XmlFormat()),
                "CustomConfigurations");
            XmlGenerator  generator           = new XmlGenerator();
            StringBuilder configurationString = new StringBuilder();

            foreach (ISerializationToken token in serializer)
            {
                configurationString.Append(generator.Format(token));
            }
            StringBuilder configurationTypeCacheString = new StringBuilder();

            foreach (string s in generator.Format(serializer.TypeCache))
            {
                configurationTypeCacheString.Append(s);
            }
            Properties.Settings.Default.CustomConfigurations =
                configurationString.ToString();
            Properties.Settings.Default.CustomConfigurationsTypeCache =
                configurationTypeCacheString.ToString();
            Properties.Settings.Default.Save();
        }
コード例 #2
0
ファイル: UseCases.cs プロジェクト: lulzzz/HeuristicLab
        private void ProfilePersistenceToMemory(string fileName)
        {
            var  REPS = 5;
            var  oldDeserStopwatch = new Stopwatch();
            var  oldSerStopwatch = new Stopwatch();
            var  newDeserStopwatch = new Stopwatch();
            var  newSerStopwatch = new Stopwatch();
            long oldSize = 0, newSize = 0;
            var  config = ConfigurationService.Instance.GetConfiguration(new XmlFormat());

            int[] oldCollections = new int[3];
            int[] newCollections = new int[3];

            for (int i = 0; i < REPS; i++)
            {
                var    original            = XmlParser.Deserialize(fileName);
                object deserializedObject1 = null;
                object deserializedObject2 = null;
                byte[] buf;
                System.GC.Collect();
                var collection0 = System.GC.CollectionCount(0);
                var collection1 = System.GC.CollectionCount(1);
                var collection2 = System.GC.CollectionCount(2);
                using (var s = new MemoryStream()) {
                    oldSerStopwatch.Start();
                    // serialize manually so that the stream won't be closed
                    var serializer = new Core.Serializer(original, config);
                    serializer.InterleaveTypeInformation = true;
                    using (StreamWriter writer = new StreamWriter(new GZipStream(s, CompressionMode.Compress))) {
                        XmlGenerator generator = new XmlGenerator();
                        foreach (ISerializationToken token in serializer)
                        {
                            writer.Write(generator.Format(token));
                        }
                        writer.Flush();
                        oldSize += s.Length;
                    }

                    oldSerStopwatch.Stop();
                    buf = s.GetBuffer();
                }
                using (var s = new MemoryStream(buf)) {
                    oldDeserStopwatch.Start();
                    deserializedObject1 = XmlParser.Deserialize(s);
                    oldDeserStopwatch.Stop();
                }

                System.GC.Collect();
                oldCollections[0] += System.GC.CollectionCount(0) - collection0;
                oldCollections[1] += System.GC.CollectionCount(1) - collection1;
                oldCollections[2] += System.GC.CollectionCount(2) - collection2;

                collection0 = System.GC.CollectionCount(0);
                collection1 = System.GC.CollectionCount(1);
                collection2 = System.GC.CollectionCount(2);

                // Protobuf only uses Deflate
                using (var m = new MemoryStream()) {
                    //         using (var s = new GZipStream(m, CompressionMode.Compress)) { // same as old persistence
                    using (var s = new DeflateStream(m, CompressionMode.Compress)) { // new format
                        newSerStopwatch.Start();
                        (new ProtoBufSerializer()).Serialize(original, s, false);
                        s.Flush();
                        newSerStopwatch.Stop();
                        newSize += m.Length;
                    }
                    buf = m.GetBuffer();
                }
                using (var m = new MemoryStream(buf)) {
                    // using (var s = new GZipStream(m, CompressionMode.Decompress)) { // same as old persistence
                    using (var s = new DeflateStream(m, CompressionMode.Decompress)) { // new format
                        newDeserStopwatch.Start();
                        deserializedObject2 = (new ProtoBufSerializer()).Deserialize(s);
                        newDeserStopwatch.Stop();
                    }
                }
                //Assert.AreEqual(deserializedObject1.GetObjectGraphObjects().Count(), deserializedObject2.GetObjectGraphObjects().Count());

                System.GC.Collect();
                newCollections[0] += System.GC.CollectionCount(0) - collection0;
                newCollections[1] += System.GC.CollectionCount(1) - collection1;
                newCollections[2] += System.GC.CollectionCount(2) - collection2;
            }

            Console.WriteLine($"{fileName} " +
                              $"{oldSize / (double)REPS} " +
                              $"{newSize / (double)REPS} " +
                              $"{oldSerStopwatch.ElapsedMilliseconds / (double)REPS} " +
                              $"{newSerStopwatch.ElapsedMilliseconds / (double)REPS} " +
                              $"{oldDeserStopwatch.ElapsedMilliseconds / (double)REPS} " +
                              $"{newDeserStopwatch.ElapsedMilliseconds / (double)REPS} " +
                              $"{oldCollections[0] / (double)REPS} " +
                              $"{newCollections[0] / (double)REPS} " +
                              $"{oldCollections[1] / (double)REPS} " +
                              $"{newCollections[1] / (double)REPS} " +
                              $"{oldCollections[2] / (double)REPS} " +
                              $"{newCollections[2] / (double)REPS} " +
                              $"");
        }