Пример #1
0
        public void TestObjectMapLimits()
        {
            using (var stream = new MemoryStream())
            {
                var instances = new List <TypeWithTwoMembers <int, string> >();

                // We need enough items to exercise all sizes of ObjectRef
                for (int i = 0; i < ushort.MaxValue + 1; i++)
                {
                    instances.Add(new TypeWithTwoMembers <int, string>(i, i.ToString()));
                }

                var binder = new RecordingObjectBinder();
                var writer = new StreamObjectWriter(stream, binder: binder);
                // Write each instance twice. The second time around, they'll become ObjectRefs
                for (int pass = 0; pass < 2; pass++)
                {
                    foreach (var instance in instances)
                    {
                        writer.WriteValue(instance);
                    }
                }

                writer.Dispose();

                stream.Position = 0;
                using (var reader = new StreamObjectReader(stream, binder: binder))
                {
                    for (int pass = 0; pass < 2; pass++)
                    {
                        foreach (var instance in instances)
                        {
                            var obj = reader.ReadValue();
                            Assert.NotNull(obj);
                            Assert.True(Equalish(obj, instance));
                        }
                    }
                }
            }
        }
        private TimeSpan TestReaderPerf(int iterations, bool recursive)
        {
            int id    = 0;
            var graph = ConstructGraph(ref id, 5, 3);

            var stream = new MemoryStream();
            var binder = new RecordingObjectBinder();
            var writer = new StreamObjectWriter(stream, binder: binder, recursive: recursive);

            writer.WriteValue(graph);
            writer.Dispose();

            var start = DateTime.Now;

            for (int i = 0; i < iterations; i++)
            {
                stream.Position = 0;
                var reader = new StreamObjectReader(stream, binder: binder);
                var item   = reader.ReadValue();
            }

            return(DateTime.Now - start);
        }