public void TestRandomWriteAmplification() { MemoryPoolTest.TestMemoryLeak(); double size; 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; bs.Position = 8 * 1000000 - 8; bs.Write(0); Random r = new Random(2425); //Write 8 million for (long s = 0; s < 1000000; s++) { bs.Position = r.Next(8000000 - 8); bs.Write((byte)1); } size = 1000000; } System.Console.WriteLine("Read: " + (DiskIoSession.ReadCount / size).ToString("0.000")); System.Console.WriteLine("Write: " + (DiskIoSession.WriteCount / size).ToString("0.000")); System.Console.WriteLine("Checksums: " + (Stats.ChecksumCount / size).ToString("0.000")); MemoryPoolTest.TestMemoryLeak(); }
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 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 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 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 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 TestSequentialReadAmplification() { 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)) { //Write 8 million for (long s = 0; s < 1000000; s++) { bs.Write(s); } size = bs.Position / 4096.0; bs.Position = 0; Stats.ChecksumCount = 0; DiskIoSession.WriteCount = 0; DiskIoSession.ReadCount = 0; for (long s = 0; s < 1000000; s++) { bs.ReadInt64(); } } 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(); }