public void ReadSequential(long keyCount) { var keyLength = 16; var keyEnd = keyCount * 16; var file = GenerateDummyFile(keyCount * 16 * 2); //typical usage scenario is to read keys of 16bytes at a time using var f = File.OpenRead(file); var reader = new BinaryReader(f); long count = 0; Stopwatch sw = new Stopwatch(); while (count < keyCount) { sw.Start(); HashBin hb = new HashBin(f, 16); sw.Stop(); count++; } //need to simulate that we've found the key sw.Start(); f.Position = (((f.Position - keyLength) / keyLength) << 4) + keyEnd; var addr = reader.ReadUInt64(); var len = reader.ReadUInt64(); sw.Stop(); Console.WriteLine($"Sequential read for {count:N0} items took {sw.ElapsedMilliseconds:N10}ms"); File.Delete(file); }
public void ReadNonSequential(long keyCount) { var file = GenerateDummyFile(keyCount * 16 * 2); //typical usage scenario is to read keys of 16bytes at a time using var f = File.OpenRead(file); var reader = new BinaryReader(f); long count = 0; Stopwatch sw = new Stopwatch(); while (count < keyCount) { sw.Start(); HashBin hb = new HashBin(f, 16);//offset here is simulating skipping the addr and length [16b key][8b address][8b len] f.Seek(16, SeekOrigin.Current); sw.Stop(); count++; } //rewind the position (we wouldn't actually need to do this so don't time it) f.Position -= 16; sw.Start(); var addr = reader.ReadUInt64();//we're already positioned to read the addr and len var len = reader.ReadUInt64(); sw.Stop(); Console.WriteLine($"Non-Sequential read for {count:N0} items took {sw.ElapsedMilliseconds:N10}ms"); File.Delete(file); }
public void MemTest() { var buf = new byte[16 * 10000]; var r = new Random(); r.NextBytes(buf); var hashBuf = new HashBin(new byte[16]); hashBuf.SetFromPartialArray(buf, 0, 16, false); var hashBufCompare = new HashBin("e96c9661d2f7887a14264ee5986ea66d"); var swSet = new Stopwatch(); var swCompare = new Stopwatch(); var matches = 0; for (int i = 0; i < 1000000; i++) { var next = r.Next(0, buf.Length - 17); swSet.Start(); hashBuf.SetPartialIndexes(next); swSet.Stop(); swCompare.Start(); var match = hashBuf < hashBufCompare; swCompare.Stop(); if (match) { matches++; } } Console.WriteLine($"Took: {swSet.ElapsedMilliseconds:N} to set partials"); Console.WriteLine($"Took: {swCompare.ElapsedMilliseconds:N} to compare partials"); }
public void ComparisonTest(string a, string b, bool bIsGreater) { var hashBinA = new HashBin(a); var hashBinB = new HashBin(b); if (bIsGreater) { Assert.True(hashBinA < hashBinB); } else { Assert.False(hashBinA < hashBinB); } }
public void EqualityBasic(int keyLen, bool hashBinCopy) { var r = new Random(DateTime.UtcNow.Millisecond); var arrA = new byte[keyLen]; var arrC = new byte[keyLen]; r.NextBytes(arrA); r.NextBytes(arrC); var hashA = new HashBin(arrA, hashBinCopy); var hashB = new HashBin(arrA, hashBinCopy); Assert.True(hashA == hashB); var hashC = new HashBin(arrC, hashBinCopy); Assert.False(hashA == hashC); }
IEnumerable <KeyValuePair <HashBin, byte[]> > GenerateDummyData(ushort keyLength, byte indexKeyLength) { //create some data that we can repeat later for verification //we need multiple items in each key space (index key length) //e.g. //0x0000ffffffffffffffffffffffffffff, 0x0001ffffffffffffffffffffffffffff .. etc //0x00000000000000000000000000000000, 0x00010000000000000000000000000000 .. etc //0x0000aaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0x0001aaaaaaaaaaaaaaaaaaaaaaaaaaaa .. etc //0x00009999999999999999999999999999, 0x00019999999999999999999999999999 .. etc var numIndexes = CbIndex.MaxIndexesForIndexLength(indexKeyLength); string keyspaces = "0123456789abcdef"; // var debugHashBin = new HashBin("0000000000000000000000000000d8f4"); foreach (var keyspace in keyspaces) { var indexBuf = new byte[indexKeyLength]; for (int i = 0; i < numIndexes; i++) { var pattern = new HashBin("".PadLeft(32, keyspace)); //set index on key Array.Copy(indexBuf, 0, pattern.Hash, pattern.Hash.Length - indexBuf.Length, indexBuf.Length); // // if (pattern == debugHashBin) // { // Console.WriteLine("DebugMe"); // } //send it var dummyDat = new DummyData("This is your dummy data", indexBuf, pattern.Hash); yield return(new KeyValuePair <HashBin, byte[]>(pattern, dummyDat.ToJsonBytes())); //increment index bytes IncrementBuf(indexBuf); } } }
public void RandTest(bool hashBinCopy) { int count = 100000; var randHashes = new List <byte[]>(); var hashBins = new HashBin[count]; var hashStrs = new string[count]; var r = new Random(DateTime.UtcNow.Millisecond); Console.WriteLine($"Creating {count:N0} random hashes"); for (int i = 0; i < count; i++) { var buf = new byte[r.Next(16, 256)]; r.NextBytes(buf); randHashes.Add(buf.Md5()); } var sw = new Stopwatch(); Console.WriteLine($"Creating {count:N0} HashBins"); for (int i = 0; i < count; i++) { sw.Start(); hashBins[i] = new HashBin(randHashes[i], hashBinCopy); sw.Stop(); } Console.WriteLine($"Took {sw.ElapsedMilliseconds:N2}ms to create {count:N0} HashBins"); sw.Reset(); Console.WriteLine($"Creating {count:N0} Hash Strings"); for (int i = 0; i < count; i++) { sw.Start(); hashStrs[i] = randHashes[i].ToHexString(); sw.Stop(); } Console.WriteLine($"Took {sw.ElapsedMilliseconds:N2}ms to create {count:N0} Hash Strings"); sw.Reset(); Console.WriteLine($"Comparing {count:N0} HashBins"); int matches = 0; for (int i = 0; i < count; i++) { sw.Start(); if (new HashBin(randHashes[i], hashBinCopy) == hashBins[i]) { matches++; } sw.Stop(); } Console.WriteLine($"Took {sw.ElapsedMilliseconds:N2}ms to compare {count:N0} HashBins (matches={matches:N0})"); Assert.AreEqual(count, matches); sw.Reset(); Console.WriteLine($"Comparing {count:N0} HashStrings"); matches = 0; for (int i = 0; i < count; i++) { sw.Start(); if (randHashes[i].ToHexString() == hashStrs[i]) { matches++; } sw.Stop(); } Console.WriteLine($"Took {sw.ElapsedMilliseconds:N2}ms to compare {count:N0} Hash Strings (matches={matches:N0})"); Assert.AreEqual(count, matches); //less than comparisons: sw.Reset(); Console.WriteLine($"Comparing {count:N0} HashBins with < operator"); matches = 0; for (int i = 0; i < count; i++) { int checkIndex = i + 1 < randHashes.Count - 1 ? i + 1 : i; sw.Start(); if (randHashes[i].ToHashBin(hashBinCopy) < randHashes[checkIndex].ToHashBin(hashBinCopy)) { matches++; } sw.Stop(); } Console.WriteLine($"Took {sw.ElapsedMilliseconds:N2}ms to compare {count:N0} HashBins with < operator (matches={matches:N0})"); }
public void StringConversion(string md5) { var hb = new HashBin(md5); Assert.AreEqual(md5.ToLower(), hb.ToString()); }