Inheritance: BinaryStreamPointerBase
        public void BenchmarkOld2(int pointCount)
        {
            SortedPointBuffer<HistorianKey, HistorianValue> points = new SortedPointBuffer<HistorianKey, HistorianValue>(pointCount, true);

            HistorianKey key = new HistorianKey();
            HistorianValue value = new HistorianValue();

            for (int x = 0; x < pointCount; x++)
            {
                key.PointID = (ulong)x;
                points.TryEnqueue(key, value);
            }

            points.IsReadingMode = true;

            var sw = new Stopwatch();
            sw.Start();
            using (var bs = new BinaryStream(true))
            {
                var st = SortedTree<HistorianKey, HistorianValue>.Create(bs, 4096, HistorianFileEncodingDefinition.TypeGuid);

                st.AddRange(points);

                //SequentialSortedTreeWriter<HistorianKey, HistorianValue>.Create(bs, 4096, SortedTree.FixedSizeNode, points);
            }
            sw.Stop();

            System.Console.WriteLine("Points {0}: {1}MPPS", pointCount, (pointCount / sw.Elapsed.TotalSeconds / 1000000).ToString("0.0"));
        }
        public void TestSequentialWriteAmplification()
        {
            MemoryPoolTest.TestMemoryLeak();
            double size;
            Stats.ChecksumCount = 0;
            DiskIoSession.WriteCount = 0;
            DiskIoSession.ReadCount = 0;

            using (TransactionalFileStructure file = TransactionalFileStructure.CreateInMemory(4096))
            using (TransactionalEdit edit = file.BeginEdit())
            using (SubFileStream stream = edit.CreateFile(SubFileName.CreateRandom()))
            using (BinaryStream bs = new BinaryStream(stream))
            {
                Stats.ChecksumCount = 0;
                DiskIoSession.WriteCount = 0;
                DiskIoSession.ReadCount = 0;

                //Write 8 million
                for (long s = 0; s < 1000000; s++)
                {
                    bs.Write(s);
                }
                size = bs.Position / 4096.0;
            }

            System.Console.WriteLine("Read: " + (DiskIoSession.ReadCount / size).ToString("0.0"));
            System.Console.WriteLine("Write: " + (DiskIoSession.WriteCount / size).ToString("0.0"));
            System.Console.WriteLine("Checksums: " + (Stats.ChecksumCount / size).ToString("0.0"));
            MemoryPoolTest.TestMemoryLeak();
        }
 public void RunMemoryStreamTest()
 {
     MemoryPoolTest.TestMemoryLeak();
     MemoryPoolStream ms = new MemoryPoolStream();
     BinaryStream bs = new BinaryStream(ms);
     Run(bs, false);
     ms.Dispose();
     MemoryPoolTest.TestMemoryLeak();
 }
        public static void Run(BinaryStream bs, bool clipboard = true)
        {
            const int count = 100;
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(RunInserts(count, bs));
            sb.AppendLine(RunSeeks(count, bs));
            sb.AppendLine(GetWritePointer(bs));
            sb.AppendLine(GetReadPointer(count, bs));
            sb.AppendLine("Type\tRead\tWrite");
            sb.AppendLine(RunByte(count, bs));
            sb.AppendLine(RunSByte(count, bs));
            sb.AppendLine(RunUShort(count, bs));
            sb.AppendLine(RunShort(count, bs));
            sb.AppendLine(RunUInt(count, bs));
            sb.AppendLine(RunInt(count, bs));
            sb.AppendLine(RunSingle(count, bs));
            sb.AppendLine(RunULong(count, bs));
            sb.AppendLine(RunLong(count, bs));
            sb.AppendLine(RunDouble(count, bs));
            sb.AppendLine(Run7Bit32(count, bs, 10));
            sb.AppendLine(Run7Bit32(count, bs, 128 + 10));
            sb.AppendLine(Run7Bit32(count, bs, 128 * 128 + 10));
            sb.AppendLine(Run7Bit32(count, bs, 128 * 128 * 128 + 10));
            sb.AppendLine(Run7Bit32(count, bs, 128 * 128 * 128 * 128 + 10));
            sb.AppendLine(Run7Bit64(count, bs, 10));
            sb.AppendLine(Run7Bit64(count, bs, 128 + 10));
            sb.AppendLine(Run7Bit64(count, bs, 128 * 128 + 10));
            sb.AppendLine(Run7Bit64(count, bs, 128 * 128 * 128 + 10));
            sb.AppendLine(Run7Bit64(count, bs, 128L * 128L * 128L * 128L + 10));
            sb.AppendLine(Run7Bit64(count, bs, 128L * 128L * 128L * 128L * 128L + 10));
            sb.AppendLine(Run7Bit64(count, bs, 128L * 128L * 128L * 128L * 128L * 128L + 10));
            sb.AppendLine(Run7Bit64(count, bs, 128L * 128L * 128L * 128L * 128L * 128L * 128L + 10));
            sb.AppendLine(Run7Bit64(count, bs, 128L * 128L * 128L * 128L * 128L * 128L * 128L * 128L + 10));
            sb.AppendLine(Run7Bit64(count, bs, ulong.MaxValue));

            if (clipboard)
            {
                Clipboard.SetText(sb.ToString());
                MessageBox.Show(sb.ToString());
            }
            else
            {
                System.Console.WriteLine(sb.ToString());
            }
        }
        public void TestBitArray()
        {
            var list = new List<ulong>();
            var pointId = PointIdMatchFilter.CreateFromList<HistorianKey, HistorianValue>(list);

            if (!pointId.GetType().FullName.Contains("BitArrayFilter"))
                throw new Exception("Wrong type");

            using (var bs = new BinaryStream(allocatesOwnMemory: true))
            {
                bs.Write(pointId.FilterType);
                pointId.Save(bs);
                bs.Position = 0;

                var filter = Library.Filters.GetMatchFilter<HistorianKey, HistorianValue>(bs.ReadGuid(), bs);

                if (!filter.GetType().FullName.Contains("BitArrayFilter"))
                    throw new Exception("Wrong type");
            }
        }
        public void TestSubFileStream()
        {
            const int BlockSize = 256;
            MemoryPoolTest.TestMemoryLeak();
            //string file = Path.GetTempFileName();
            //System.IO.File.Delete(file);
            try
            {
                //using (FileSystemSnapshotService service = FileSystemSnapshotService.CreateFile(file))
                using (TransactionalFileStructure service = TransactionalFileStructure.CreateInMemory(BlockSize))
                {
                    using (TransactionalEdit edit = service.BeginEdit())
                    {
                        SubFileStream fs = edit.CreateFile(SubFileName.Empty);
                        BinaryStream bs = new BinaryStream(fs);

                        for (int x = 0; x < 20000000; x++)
                            bs.Write(1L);

                        bs.Position = 0;

                        BinaryStreamBenchmark.Run(bs, false);

                        bs.Dispose();
                        fs.Dispose();
                        edit.CommitAndDispose();
                    }
                }
            }
            finally
            {
                //System.IO.File.Delete(file);
            }

            MemoryPoolTest.TestMemoryLeak();
        }
        public void Test()
        {
            MemoryPoolTest.TestMemoryLeak();
            const int count = 100;
            MemoryPoolStream ms = new MemoryPoolStream();
            //ms.Write(new byte[100000], 0, 100000);
            //ms.Write(new byte[100000], 0, 100000);
            //ms.Position = 0;
            BinaryStream bs = new BinaryStream(ms);
            Stopwatch sw = new Stopwatch();
            //DateTime b = DateTime.UtcNow;
            long b = 10;
            //Guid b = Guid.NewGuid() ;
            for (int x2 = 0; x2 < count; x2++)
            {
                bs.Position = 0;
                for (int x = 0; x < count; x++)
                {
                    bs.Write(b);
                    bs.Write(b);
                    bs.Write(b);
                    bs.Write(b);
                    bs.Write(b);
                    bs.Write(b);
                    bs.Write(b);
                    bs.Write(b);
                    bs.Write(b);
                    bs.Write(b);
                }
            }

            sw.Start();
            for (int x2 = 0; x2 < count; x2++)
            {
                bs.Position = 0;
                for (int x = 0; x < count; x++)
                {
                    bs.Write(b);
                    bs.Write(b);
                    bs.Write(b);
                    bs.Write(b);
                    bs.Write(b);
                    bs.Write(b);
                    bs.Write(b);
                    bs.Write(b);
                    bs.Write(b);
                    bs.Write(b);
                    //bs.ReadDecimal();
                    //bs.ReadDecimal();
                    //bs.ReadDecimal();
                    //bs.ReadDecimal();
                    //bs.ReadDecimal();
                    //bs.ReadDecimal();
                    //bs.ReadDecimal();
                    //bs.ReadDecimal();
                    //bs.ReadDecimal();
                    //bs.ReadDecimal();
                    //bs.ReadInt64();
                    //bs.ReadInt64();
                    //bs.ReadInt64();
                    //bs.ReadInt64();
                    //bs.ReadInt64();
                    //bs.ReadInt64();
                    //bs.ReadInt64();
                    //bs.ReadInt64();
                    //bs.ReadInt64();
                    //bs.ReadInt64();
                    //bs.ReadInt32();
                    //bs.ReadInt32();
                    //bs.ReadInt32();
                    //bs.ReadInt32();
                    //bs.ReadInt32();
                    //bs.ReadInt32();
                    //bs.ReadInt32();
                    //bs.ReadInt32();
                    //bs.ReadInt32();
                    //bs.ReadInt32();
                    //bs.ReadInt16();
                    //bs.ReadInt16();
                    //bs.ReadInt16();
                    //bs.ReadInt16();
                    //bs.ReadInt16();
                    //bs.ReadInt16();
                    //bs.ReadInt16();
                    //bs.ReadInt16();
                    //bs.ReadInt16();
                    //bs.ReadInt16();
                    //bs.ReadByte();
                    //bs.ReadByte();
                    //bs.ReadByte();
                    //bs.ReadByte();
                    //bs.ReadByte();
                    //bs.ReadByte();
                    //bs.ReadByte();
                    //bs.ReadByte();
                    //bs.ReadByte();
                    //bs.ReadByte();
                    //bs.Write7Bit(b);
                    //bs.Write7Bit(b);
                    //bs.Write7Bit(b);
                    //bs.Write7Bit(b);
                    //bs.Write7Bit(b);
                    //bs.Write7Bit(b);
                    //bs.Write7Bit(b);
                    //bs.Write7Bit(b);
                    //bs.Write7Bit(b);
                    //bs.Write7Bit(b);
                }
            }
            sw.Stop();
            Assert.IsTrue(true);
            ms.Dispose();
            MemoryPoolTest.TestMemoryLeak();
            //MessageBox.Show((count * count * 10 / sw.Elapsed.TotalSeconds / 1000000).ToString());
        }
        public static unsafe void Test(ISupportsBinaryStream stream)
        {
            BinaryStream bs = new BinaryStream(stream);
            Random rand = new Random();
            int seed = rand.Next();
            rand = new Random(seed);
            byte[] data = new byte[16];
            byte[] data2 = new byte[16];

            fixed (byte* lp = data)
            {
                for (int x = 0; x < 10000; x++)
                {
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) bs.Write(*(byte*)lp);
                    int skip = rand.Next(40) + 1;
                    bs.Position += skip;
                    bs.Position -= rand.Next(skip);
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) bs.Write(*(sbyte*)lp);
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) bs.Write(*(short*)lp);
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) bs.Write(*(int*)lp);
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) bs.Write(*(long*)lp);
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) bs.Write(*(ushort*)lp);
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) bs.Write(*(uint*)lp);
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) bs.Write(*(ulong*)lp);

                    for (int i = 0; i < 9; i++)
                    {
                        rand.NextBytes(data);
                        while (rand.Next(4) < 2) bs.WriteUInt(*(ulong*)lp, i);
                    }

                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) bs.Write(*(decimal*)lp);
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) bs.Write(*(Guid*)lp);
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) bs.Write(NextDate(data, rand));
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) bs.Write(NextSingle(data, rand));
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) bs.Write(NextDouble(data, rand));
                    rand.NextBytes(data);
                    bool value = (*lp != 0);
                    while (rand.Next(4) < 2) bs.Write(value);

                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) bs.Write7Bit(*(uint*)lp);
                    data[3] = 0;
                    while (rand.Next(4) < 2) bs.Write7Bit(*(uint*)lp);
                    data[2] = 0;
                    while (rand.Next(4) < 2) bs.Write7Bit(*(uint*)lp);
                    data[1] = 0;
                    while (rand.Next(4) < 2) bs.Write7Bit(*(uint*)lp);

                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) bs.Write7Bit(*(ulong*)lp);
                    data[7] = 0;
                    while (rand.Next(4) < 2) bs.Write7Bit(*(ulong*)lp);
                    data[6] = 0;
                    while (rand.Next(4) < 2) bs.Write7Bit(*(ulong*)lp);
                    data[5] = 0;
                    while (rand.Next(4) < 2) bs.Write7Bit(*(ulong*)lp);
                    data[4] = 0;
                    while (rand.Next(4) < 2) bs.Write7Bit(*(ulong*)lp);
                    data[3] = 0;
                    while (rand.Next(4) < 2) bs.Write7Bit(*(ulong*)lp);
                    data[2] = 0;
                    while (rand.Next(4) < 2) bs.Write7Bit(*(ulong*)lp);
                    data[1] = 0;
                    while (rand.Next(4) < 2) bs.Write7Bit(*(ulong*)lp);

                    rand.NextBytes(data);
                    bs.Write(data, 0, data.Length);

                    while (rand.Next(4) < 2)
                    {
                        if (bs.Position > 100)
                        {
                            bs.Position -= 100;
                            int insertCount = rand.Next(16) + 1;
                            bs.InsertBytes(insertCount, 100);
                            bs.Write(data, 0, insertCount);
                            bs.Position -= insertCount;
                            bs.ReadAll(data2, 0, insertCount);
                            bs.Position -= insertCount;
                            bs.RemoveBytes(insertCount, 100);
                            bs.Position += 100;

                            for (int y = 0; y < insertCount; y++)
                            {
                                if (data[y] != data2[y])
                                    throw new Exception();
                            }
                        }
                    }
                }
                rand = new Random(seed);

                bs.Position = 0;

                for (int x = 0; x < 10000; x++)
                {
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) if (bs.ReadUInt8() != (*(byte*)lp)) throw new Exception();
                    int skip = rand.Next(40) + 1;
                    bs.Position += skip;
                    bs.Position -= rand.Next(skip);
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) if (bs.ReadInt8() != (*(sbyte*)lp)) throw new Exception();
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) if (bs.ReadInt16() != (*(short*)lp)) throw new Exception();
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) if (bs.ReadInt32() != (*(int*)lp)) throw new Exception();
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) if (bs.ReadInt64() != (*(long*)lp)) throw new Exception();
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) if (bs.ReadUInt16() != (*(ushort*)lp)) throw new Exception();
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) if (bs.ReadUInt32() != (*(uint*)lp)) throw new Exception();
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) if (bs.ReadUInt64() != (*(ulong*)lp)) throw new Exception();

                    for (int i = 0; i < 9; i++)
                    {
                        rand.NextBytes(data);
                        while (rand.Next(4) < 2) if (bs.ReadUInt(i) != (mask(i) & *(ulong*)lp)) throw new Exception();
                    }

                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) if (bs.ReadDecimal() != (*(decimal*)lp)) throw new Exception();
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) if (bs.ReadGuid() != (*(Guid*)lp)) throw new Exception();
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) if (bs.ReadDateTime() != NextDate(data, rand)) throw new Exception();
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) if (bs.ReadSingle() != NextSingle(data, rand)) throw new Exception();
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) if (bs.ReadDouble() != NextDouble(data, rand)) throw new Exception();
                    rand.NextBytes(data);
                    bool b2 = (*lp != 0);
                    while (rand.Next(4) < 2) if (bs.ReadBoolean() != b2) throw new Exception();

                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) if (bs.Read7BitUInt32() != (*(uint*)lp)) throw new Exception();
                    data[3] = 0;
                    while (rand.Next(4) < 2) if (bs.Read7BitUInt32() != (*(uint*)lp)) throw new Exception();
                    data[2] = 0;
                    while (rand.Next(4) < 2) if (bs.Read7BitUInt32() != (*(uint*)lp)) throw new Exception();
                    data[1] = 0;
                    while (rand.Next(4) < 2) if (bs.Read7BitUInt32() != (*(uint*)lp)) throw new Exception();

                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) if (bs.Read7BitUInt64() != (*(ulong*)lp)) throw new Exception();
                    data[7] = 0;
                    while (rand.Next(4) < 2) if (bs.Read7BitUInt64() != (*(ulong*)lp)) throw new Exception();
                    data[6] = 0;
                    while (rand.Next(4) < 2) if (bs.Read7BitUInt64() != (*(ulong*)lp)) throw new Exception();
                    data[5] = 0;
                    while (rand.Next(4) < 2) if (bs.Read7BitUInt64() != (*(ulong*)lp)) throw new Exception();
                    data[4] = 0;
                    while (rand.Next(4) < 2) if (bs.Read7BitUInt64() != (*(ulong*)lp)) throw new Exception();
                    data[3] = 0;
                    while (rand.Next(4) < 2) if (bs.Read7BitUInt64() != (*(ulong*)lp)) throw new Exception();
                    data[2] = 0;
                    while (rand.Next(4) < 2) if (bs.Read7BitUInt64() != (*(ulong*)lp)) throw new Exception();
                    data[1] = 0;
                    while (rand.Next(4) < 2) if (bs.Read7BitUInt64() != (*(ulong*)lp)) throw new Exception();

                    rand.NextBytes(data);
                    bs.ReadAll(data2, 0, 16);
                    if (!data2.SequenceEqual<byte>(data)) throw new Exception();

                    while (rand.Next(4) < 2)
                    {
                        if (bs.Position > 100)
                        {
                            int insertCount = rand.Next(16);
                        }
                    }
                }
            }
        }
        internal static void TestCustomSizeRead(SubFileStream ds, int seed)
        {
            using (BinaryStream bs = new BinaryStream(ds))
            {
                Random r = new Random(seed);

                byte[] buffer = new byte[25];
                byte[] buffer2 = new byte[25];
                bs.Position = 0;
                for (int x = 0; x < 1000; x++)
                {
                    for (int i = 0; i < buffer.Length; i++)
                    {
                        buffer[i] = (byte)r.Next();
                    }
                    int length = r.Next(25);
                    bs.ReadAll(buffer2, 0, length);

                    for (int i = 0; i < length; i++)
                    {
                        if (buffer[i] != buffer2[i])
                            throw new Exception();
                    }
                }
            }
        }
        internal static void TestCustomSizeWrite(SubFileStream ds, int length)
        {
            using (BinaryStream bs = new BinaryStream(ds))
            {
                Random r = new Random(length);

                bs.Position = 0;
                byte[] buffer = new byte[25];

                for (int x = 0; x < 1000; x++)
                {
                    for (int i = 0; i < buffer.Length; i++)
                    {
                        buffer[i] = (byte)r.Next();
                    }
                    bs.Write(buffer, 0, r.Next(25));
                }
            }
        }
 internal static void TestSingleByteRead(SubFileStream ds)
 {
     using (BinaryStream bs = new BinaryStream(ds))
     {
         bs.Position = 0;
         for (int x = 0; x < 10000; x++)
         {
             if ((byte)x != bs.ReadUInt8())
                 throw new Exception();
         }
     }
 }
 internal static void TestSingleByteWrite(SubFileStream ds)
 {
     using (BinaryStream bs = new BinaryStream(ds))
     {
         bs.Position = 0;
         for (int x = 0; x < 10000; x++)
         {
             bs.Write((byte)x);
         }
     }
 }
        public static string RunByte(int thousands, BinaryStream bs)
        {
            Stopwatch sw1 = new Stopwatch();
            Stopwatch sw2 = new Stopwatch();
            byte b = 10;

            for (int x2 = 0; x2 < thousands; x2++)
            {
                bs.Position = 0;
                for (int x = 0; x < 100; x++)
                {
                    bs.Write(b);
                    bs.Write(b);
                    bs.Write(b);
                    bs.Write(b);
                    bs.Write(b);
                    bs.Write(b);
                    bs.Write(b);
                    bs.Write(b);
                    bs.Write(b);
                    bs.Write(b);
                }
            }

            sw1.Start();
            for (int x2 = 0; x2 < thousands; x2++)
            {
                bs.Position = 0;
                for (int x = 0; x < 100; x++)
                {
                    bs.Write(b);
                    bs.Write(b);
                    bs.Write(b);
                    bs.Write(b);
                    bs.Write(b);
                    bs.Write(b);
                    bs.Write(b);
                    bs.Write(b);
                    bs.Write(b);
                    bs.Write(b);
                }
            }
            sw1.Stop();

            sw2.Start();
            for (int x2 = 0; x2 < thousands; x2++)
            {
                bs.Position = 0;
                for (int x = 0; x < 100; x++)
                {
                    bs.ReadUInt8();
                    bs.ReadUInt8();
                    bs.ReadUInt8();
                    bs.ReadUInt8();
                    bs.ReadUInt8();
                    bs.ReadUInt8();
                    bs.ReadUInt8();
                    bs.ReadUInt8();
                    bs.ReadUInt8();
                    bs.ReadUInt8();
                }
            }
            sw2.Stop();
            return "Byte\t" + (thousands * 1000 / sw2.Elapsed.TotalSeconds / 1000000) + "\t" +
                   (thousands * 1000 / sw1.Elapsed.TotalSeconds / 1000000);
        }
        public static string RunInserts(int thousands, BinaryStream bs)
        {
            const int insertBytes = 1;
            const int moveSize = 512;
            Stopwatch sw1 = new Stopwatch();
            byte b = 10;

            for (int x2 = 0; x2 < thousands; x2++)
            {
                bs.Position = 0;
                bs.Write(b);

                for (int x = 0; x < 100; x++)
                {
                    bs.InsertBytes(insertBytes, moveSize);
                    bs.InsertBytes(insertBytes, moveSize);
                    bs.InsertBytes(insertBytes, moveSize);
                    bs.InsertBytes(insertBytes, moveSize);
                    bs.InsertBytes(insertBytes, moveSize);
                    bs.InsertBytes(insertBytes, moveSize);
                    bs.InsertBytes(insertBytes, moveSize);
                    bs.InsertBytes(insertBytes, moveSize);
                    bs.InsertBytes(insertBytes, moveSize);
                    bs.InsertBytes(insertBytes, moveSize);
                }
            }

            sw1.Start();
            for (int x2 = 0; x2 < thousands; x2++)
            {
                bs.Position = 0;
                bs.Write(b);

                for (int x = 0; x < 100; x++)
                {
                    bs.InsertBytes(insertBytes, moveSize);
                    bs.InsertBytes(insertBytes, moveSize);
                    bs.InsertBytes(insertBytes, moveSize);
                    bs.InsertBytes(insertBytes, moveSize);
                    bs.InsertBytes(insertBytes, moveSize);
                    bs.InsertBytes(insertBytes, moveSize);
                    bs.InsertBytes(insertBytes, moveSize);
                    bs.InsertBytes(insertBytes, moveSize);
                    bs.InsertBytes(insertBytes, moveSize);
                    bs.InsertBytes(insertBytes, moveSize);
                }
            }
            sw1.Stop();

            return "Inserts\t" + (thousands * 1000 / sw1.Elapsed.TotalSeconds / 1000000);
        }
        public static string Run7Bit32(int thousands, BinaryStream bs, uint b)
        {
            Stopwatch sw1 = new Stopwatch();
            Stopwatch sw2 = new Stopwatch();

            for (int x2 = 0; x2 < thousands; x2++)
            {
                bs.Position = 0;
                for (int x = 0; x < 100; x++)
                {
                    bs.Write7Bit(b);
                    bs.Write7Bit(b);
                    bs.Write7Bit(b);
                    bs.Write7Bit(b);
                    bs.Write7Bit(b);
                    bs.Write7Bit(b);
                    bs.Write7Bit(b);
                    bs.Write7Bit(b);
                    bs.Write7Bit(b);
                    bs.Write7Bit(b);
                }
            }

            sw1.Start();
            for (int x2 = 0; x2 < thousands; x2++)
            {
                bs.Position = 0;
                for (int x = 0; x < 100; x++)
                {
                    bs.Write7Bit(b);
                    bs.Write7Bit(b);
                    bs.Write7Bit(b);
                    bs.Write7Bit(b);
                    bs.Write7Bit(b);
                    bs.Write7Bit(b);
                    bs.Write7Bit(b);
                    bs.Write7Bit(b);
                    bs.Write7Bit(b);
                    bs.Write7Bit(b);
                }
            }
            sw1.Stop();

            sw2.Start();
            for (int x2 = 0; x2 < thousands; x2++)
            {
                bs.Position = 0;
                for (int x = 0; x < 100; x++)
                {
                    bs.Read7BitUInt32();
                    bs.Read7BitUInt32();
                    bs.Read7BitUInt32();
                    bs.Read7BitUInt32();
                    bs.Read7BitUInt32();
                    bs.Read7BitUInt32();
                    bs.Read7BitUInt32();
                    bs.Read7BitUInt32();
                    bs.Read7BitUInt32();
                    bs.Read7BitUInt32();
                }
            }
            sw2.Stop();
            return "7Bit32 " + Encoding7Bit.GetSize(b) + "\t" + (thousands * 1000 / sw2.Elapsed.TotalSeconds / 1000000) + "\t" +
                   (thousands * 1000 / sw1.Elapsed.TotalSeconds / 1000000);
        }
        public static string RunSeeks(int thousands, BinaryStream bs)
        {
            Stopwatch sw1 = new Stopwatch();
            byte b = 10;

            for (int x2 = 0; x2 < thousands; x2++)
            {
                bs.Position = 100;
                bs.Write(b);

                for (int x = 0; x < 100; x++)
                {
                    bs.Position = 0;
                    bs.Position = 1;
                    bs.Position = 2;
                    bs.Position = 3;
                    bs.Position = 4;
                    bs.Position = 5;
                    bs.Position = 6;
                    bs.Position = 7;
                    bs.Position = 8;
                    bs.Position = 9;
                }
            }

            sw1.Start();
            for (int x2 = 0; x2 < thousands; x2++)
            {
                bs.Position = 100;
                bs.Write(b);

                for (int x = 0; x < 100; x++)
                {
                    bs.Position = 0;
                    bs.Position = 1;
                    bs.Position = 2;
                    bs.Position = 3;
                    bs.Position = 4;
                    bs.Position = 5;
                    bs.Position = 6;
                    bs.Position = 7;
                    bs.Position = 8;
                    bs.Position = 9;
                }
            }
            sw1.Stop();

            return "Seeks\t" + (thousands * 1000 / sw1.Elapsed.TotalSeconds / 1000000);
        }
 public static unsafe string GetWritePointer(BinaryStream bs)
 {
     return "GetWritePointer\t" +
            StepTimer.Time(10, () =>
            {
                bs.GetWritePointer(40 * 1000000, 1);
                bs.GetWritePointer(80 * 1000000, 1);
                bs.GetWritePointer(120 * 1000000, 1);
                bs.GetWritePointer(50 * 1000000, 1);
                bs.GetWritePointer(90 * 1000000, 1);
                bs.GetWritePointer(130 * 1000000, 1);
                bs.GetWritePointer(60 * 1000000, 1);
                bs.GetWritePointer(100 * 1000000, 1);
                bs.GetWritePointer(140 * 1000000, 1);
                bs.GetWritePointer(110 * 1000000, 1);
            });
 }
        public void Test()
        {
            Assert.AreEqual(Globals.MemoryPool.AllocatedBytes, 0L);
            //string file = Path.GetTempFileName();
            //System.IO.File.Delete(file);
            try
            {
                //using (FileSystemSnapshotService service = FileSystemSnapshotService.CreateFile(file))
                using (TransactionalFileStructure service = TransactionalFileStructure.CreateInMemory(BlockSize))
                {
                    using (TransactionalEdit edit = service.BeginEdit())
                    {
                        SubFileStream fs = edit.CreateFile(SubFileName.CreateRandom());
                        BinaryStream bs = new BinaryStream(fs);
                        bs.Write((byte)1);
                        bs.Dispose();
                        fs.Dispose();
                        edit.CommitAndDispose();
                    }
                    {
                        ReadSnapshot read = service.Snapshot;
                        SubFileStream f1 = read.OpenFile(0);
                        BinaryStream bs1 = new BinaryStream(f1);
                        if (bs1.ReadUInt8() != 1)
                            throw new Exception();

                        using (TransactionalEdit edit = service.BeginEdit())
                        {
                            SubFileStream f2 = edit.OpenFile(0);
                            BinaryStream bs2 = new BinaryStream(f2);
                            if (bs2.ReadUInt8() != 1)
                                throw new Exception();
                            bs2.Write((byte)3);
                            bs2.Dispose();
                        } //rollback should be issued;
                        if (bs1.ReadUInt8() != 0)
                            throw new Exception();
                        bs1.Dispose();

                        {
                            ReadSnapshot read2 = service.Snapshot;
                            SubFileStream f2 = read2.OpenFile(0);
                            BinaryStream bs2 = new BinaryStream(f2);
                            if (bs2.ReadUInt8() != 1)
                                throw new Exception();
                            if (bs2.ReadUInt8() != 0)
                                throw new Exception();
                            bs2.Dispose();
                        }
                    }
                    using (TransactionalEdit edit = service.BeginEdit())
                    {
                        SubFileStream f2 = edit.OpenFile(0);
                        BinaryStream bs2 = new BinaryStream(f2);
                        bs2.Write((byte)13);
                        bs2.Write((byte)23);
                        bs2.Dispose();
                        edit.RollbackAndDispose();
                    } //rollback should be issued;
                }
            }
            finally
            {
                //System.IO.File.Delete(file);
            }

            Assert.AreEqual(Globals.MemoryPool.AllocatedBytes, 0L);
            Assert.IsTrue(true);
        }
        public void Test(int pointCount)
        {
            SortedPointBuffer<HistorianKey, HistorianValue> points = new SortedPointBuffer<HistorianKey, HistorianValue>(pointCount, true);
            var r = new Random(1);

            HistorianKey key = new HistorianKey();
            HistorianValue value = new HistorianValue();

            for (int x = 0; x < pointCount; x++)
            {
                key.PointID = (ulong)r.Next();
                key.Timestamp = (ulong)r.Next();
                value.Value1 = key.PointID;
                points.TryEnqueue(key, value);
            }

            points.IsReadingMode = true;

            using (var bs = new BinaryStream(true))
            {
                //var tree = new SequentialSortedTreeWriter<HistorianKey, HistorianValue>(bs, 256, SortedTree.FixedSizeNode);
                //SequentialSortedTreeWriter<HistorianKey, HistorianValue>.Create(bs, 512, CreateTsCombinedEncoding.TypeGuid, points);
                SequentialSortedTreeWriter<HistorianKey, HistorianValue>.Create(bs, 512, EncodingDefinition.FixedSizeCombinedEncoding, points);

                var sts = SortedTree<HistorianKey, HistorianValue>.Open(bs);
                r = new Random(1);

                for (int x = 0; x < pointCount; x++)
                {
                    key.PointID = (ulong)r.Next();
                    key.Timestamp = (ulong)r.Next();
                    sts.Get(key, value);
                    if (value.Value1 != key.PointID)
                        throw new Exception();
                }

            }
        }
        public void Test_CreateRootNode_Get()
        {
            uint rootKey = 0;
            byte rootLevel = 0;

            uint nextKeyIndex = 0;
            Func<uint> getNextKey = () =>
            {
                nextKeyIndex++;
                return nextKeyIndex - 1;
            };
            Action<SnapUInt32, uint, byte> addToParent = (int32, u, arg3) => int32 = int32;
            Func<SnapUInt32, uint> findLeafNode = int32 => 0;

            Stopwatch swWrite = new Stopwatch();
            Stopwatch swRead = new Stopwatch();
            using (BinaryStream bs = new BinaryStream())
            {
                uint k, v;
                FixedSizeNode<SnapUInt32, SnapUInt32> node = new FixedSizeNode<SnapUInt32, SnapUInt32>(0);

                node.Initialize(bs, 1024, getNextKey, null);

                node.CreateEmptyNode(0);
                node.Insert(1, 100);
                node.Insert(2, 200);

                Assert.AreEqual(0u, node.NodeIndex);
                Assert.AreEqual(uint.MaxValue, node.LeftSiblingNodeIndex);
                Assert.AreEqual(uint.MaxValue, node.RightSiblingNodeIndex);

                Assert.AreEqual(1u, node.GetFirstKey());
                Assert.AreEqual(100u, node.GetFirstValue());
                Assert.AreEqual(2u, node.GetLastKey());
                Assert.AreEqual(200u, node.GetLastValue());

                Assert.AreEqual(100u, node.Get(1));
                Assert.AreEqual(200u, node.Get(2));
                Assert.AreEqual(100u, node.GetOrGetNext(1));
                Assert.AreEqual(200u, node.GetOrGetNext(2));
                Assert.AreEqual(200u, node.GetOrGetNext(3));

                node.SetNodeIndex(0);

                Assert.AreEqual(0u, node.NodeIndex);
                Assert.AreEqual(uint.MaxValue, node.LeftSiblingNodeIndex);
                Assert.AreEqual(uint.MaxValue, node.RightSiblingNodeIndex);

                Assert.AreEqual(1u, node.GetFirstKey());
                Assert.AreEqual(100u, node.GetFirstValue());
                Assert.AreEqual(2u, node.GetLastKey());
                Assert.AreEqual(200u, node.GetLastValue());

                Assert.AreEqual(100u, node.Get(1));
                Assert.AreEqual(200u, node.Get(2));
                Assert.AreEqual(100u, node.GetOrGetNext(1));
                Assert.AreEqual(200u, node.GetOrGetNext(2));
                Assert.AreEqual(200u, node.GetOrGetNext(3));
            }
        }