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 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); } } } } }