public void LongKeyCASHashTableCheckUpdate() { var hashTable = new CAS.LongKeyCASHashTable(arrayLength, keyByteArrayLength, contentByteArrayLength); var result = Utility.GetDataString().Result; var u8 = Encoding.UTF8; string key = null; byte[] content = null; foreach (var record in result.EmptyIfNull()) { key = record.linkId.ToString() + "-" + record.clcId.ToString() + "-" + record.sbp; content = u8.GetBytes(record.url); Assert.AreEqual(hashTable.TrySet(key, content), 0); } var result2 = new List <RecordString>(); foreach (var record in result.EmptyIfNull()) { result2.Add(new RecordString { linkId = record.linkId, clcId = record.clcId, sbp = record.sbp, url = "hahahahahahaha" }); } key = null; content = null; foreach (var record in result2.EmptyIfNull()) { key = record.linkId.ToString() + "-" + record.clcId.ToString() + "-" + record.sbp; Assert.AreEqual(hashTable.TryGet(key, out content), 0); Assert.AreNotEqual(record.url, u8.GetString(content)); content = null; } key = null; content = null; foreach (var record in result2.EmptyIfNull()) { key = record.linkId.ToString() + "-" + record.clcId.ToString() + "-" + record.sbp; content = u8.GetBytes(record.url); Assert.AreEqual(hashTable.TrySet(key, content), 1); } key = null; content = null; foreach (var record in result2.EmptyIfNull()) { key = record.linkId.ToString() + "-" + record.clcId.ToString() + "-" + record.sbp; Assert.AreEqual(hashTable.TryGet(key, out content), 0); Assert.AreEqual(record.url, u8.GetString(content)); content = null; } }
public void LongKeyCASHashTableCheckUpdateBringDeletedBack() { var hashTable = new CAS.LongKeyCASHashTable(arrayLength, keyByteArrayLength, contentByteArrayLength); var result = Utility.GetDataString().Result; var u8 = Encoding.UTF8; string key = null; byte[] content = null; foreach (var record in result.EmptyIfNull()) { key = record.linkId.ToString() + "-" + record.clcId.ToString() + "-" + record.sbp; content = u8.GetBytes(record.url); Assert.AreEqual(hashTable.TrySet(key, content), 0); } key = null; content = null; foreach (var record in result.EmptyIfNull()) { key = record.linkId.ToString() + "-" + record.clcId.ToString() + "-" + record.sbp; Assert.AreEqual(hashTable.TryDelete(key), 0); Assert.AreEqual(hashTable.TryGet(key, out content), -3); Assert.IsNull(content); content = null; } //Update all the records again. key = null; content = null; foreach (var record in result.EmptyIfNull()) { key = record.linkId.ToString() + "-" + record.clcId.ToString() + "-" + record.sbp; content = u8.GetBytes(record.url); Assert.AreEqual(hashTable.TrySet(key, content), 1); } //We should get all the URL again. key = null; content = null; foreach (var record in result) { key = record.linkId.ToString() + "-" + record.clcId.ToString() + "-" + record.sbp; Assert.AreEqual(hashTable.TryGet(key, out content), 0); Assert.AreEqual(record.url, u8.GetString(content)); content = null; } }
public void LongKeyCASHashTableCheckGet() { var hashTable = new CAS.LongKeyCASHashTable(arrayLength, keyByteArrayLength, contentByteArrayLength); var result = Utility.GetDataString().Result; var u8 = Encoding.UTF8; string key = null; byte[] content = null; foreach (var record in result.EmptyIfNull()) { key = record.linkId.ToString() + "-" + record.clcId.ToString() + "-" + record.sbp; content = u8.GetBytes(record.url); Assert.AreEqual(hashTable.TrySet(key, content), 0); } content = null; foreach (var record in result) { key = record.linkId.ToString() + "-" + record.clcId.ToString() + "-" + record.sbp; Assert.AreEqual(hashTable.TryGet(key, out content), 0); Assert.AreEqual(record.url, u8.GetString(content)); content = null; } }
public void LongKeyCASHashTableCheckDeleteAgain() { var stopWatch = new Stopwatch(); var hashTable = new CAS.LongKeyCASHashTable(arrayLength, keyByteArrayLength, contentByteArrayLength); var result = Utility.GetDataString().Result; var u8 = Encoding.UTF8; string key = null; byte[] content = null; foreach (var record in result.EmptyIfNull()) { key = record.linkId.ToString() + "-" + record.clcId.ToString() + "-" + record.sbp; content = u8.GetBytes(record.url); Assert.AreEqual(hashTable.TrySet(key, content), 0); } key = null; content = null; foreach (var record in result.EmptyIfNull()) { key = record.linkId.ToString() + "-" + record.clcId.ToString() + "-" + record.sbp; Assert.AreEqual(hashTable.TryDelete(key), 0); Assert.AreEqual(hashTable.TryGet(key, out content), -3); Assert.IsNull(content); content = null; } key = null; content = null; //Delete all the records again and nothing should happens. foreach (var record in result.EmptyIfNull()) { key = record.linkId.ToString() + "-" + record.clcId.ToString() + "-" + record.sbp; Assert.AreEqual(hashTable.TryDelete(key), -3); Assert.AreEqual(hashTable.TryGet(key, out content), -3); Assert.IsNull(content); content = null; } }
public void LongKeyCASHashTableCheckGetNotFound() { var hashTable = new CAS.LongKeyCASHashTable(arrayLength, keyByteArrayLength, contentByteArrayLength); string message = ""; byte[] content = null; Assert.AreEqual(hashTable.TryGet(message, out content), -2); Assert.IsNull(content); }
public void LongKeyCASHashTableCheckGetInvlidInput() { var hashTable = new CAS.LongKeyCASHashTable(arrayLength, keyByteArrayLength, contentByteArrayLength); string message = null; byte[] content = null; try { hashTable.TryGet(message, out content); } catch (ArgumentNullException e) { message = e.Message; } Assert.IsTrue(message.Contains("key")); message = Encoding.UTF8.GetString(new byte[keyByteArrayLength + 1]); content = null; try { hashTable.TryGet(message, out content); } catch (ArgumentOutOfRangeException e) { message = e.Message; } Assert.IsTrue(message.Contains("Key length shouldn't longer than init value when you create hash table.")); }
public void LongKeyCASHashTableCheckGetDeleted() { var hashTable = new CAS.LongKeyCASHashTable(arrayLength, keyByteArrayLength, contentByteArrayLength); string message = ""; byte[] content = new byte[] { 0xff }; Assert.AreEqual(hashTable.TrySet(message, content), 0); Assert.AreEqual(hashTable.TryDelete(message), 0); content = null; Assert.AreEqual(hashTable.TryGet(message, out content), -3); Assert.IsNull(content); }
public void LongKeyCASHashTableCheckAddTotalSync() { var stopWatch = new Stopwatch(); var hashTable = new CAS.LongKeyCASHashTable(arrayLength, keyByteArrayLength, contentByteArrayLength); var ts = stopWatch.Elapsed; Console.WriteLine("Create hash table" + "\t" + ts.ToString()); stopWatch.Restart(); var result = Utility.GetDataString().Result; stopWatch.Stop(); Console.WriteLine("Get " + result.Count + " data from database" + "\t" + stopWatch.Elapsed.ToString()); var u8 = Encoding.UTF8; string key = null; byte[] content = null; stopWatch.Restart(); foreach (var record in result.EmptyIfNull()) { key = record.linkId.ToString() + "-" + record.clcId.ToString() + "-" + record.sbp; content = u8.GetBytes(record.url); //stopWatch.Restart(); Assert.AreEqual(hashTable.TrySet(key, content), 0); //stopWatch.Stop(); //Console.WriteLine("Add one records" + "\t" + stopWatch.Elapsed.ToString()); } stopWatch.Stop(); Console.WriteLine("Add all records" + "\t" + stopWatch.Elapsed.ToString()); content = null; foreach (var record in result.EmptyIfNull()) { key = record.linkId.ToString() + "-" + record.clcId.ToString() + "-" + record.sbp; Assert.AreEqual(hashTable.TryGet(key, out content), 0); Assert.AreEqual(record.url, u8.GetString(content)); content = null; } }
private void LongKeyperfGetOneRecordVerifyResult(ManualResetEvent mre, CAS.LongKeyCASHashTable hashTable, List <RecordString> record, int index) { mre.WaitOne(); var u8 = Encoding.UTF8; int InputParametersError = 0; int NoExists = 0; int IsDeleted = 0; int ExceptionError = 0; int GetSuccessfully = 0; int ResultMatch = 0; int ResultNotMatch = 0; int Zero = 0; int result; var externalStopWatch = new Stopwatch(); externalStopWatch.Start(); var internalStopWatch = new Stopwatch(); string key = null; string url = null; byte[] content = null; key = record[index].linkId.ToString() + "-" + record[index].clcId.ToString() + "-" + record[index].sbp; url = record[index].url; for (var i = 0; i < perfTestAttempts; i++) { content = null; internalStopWatch.Start(); result = hashTable.TryGet(key, out content); internalStopWatch.Stop(); switch (result) { case 0: Interlocked.Increment(ref GetSuccessfully); if (String.Compare(url, u8.GetString(content)) == 0) { Interlocked.Increment(ref ResultMatch); } else { Interlocked.Increment(ref ResultNotMatch); } break; case -1: Interlocked.Increment(ref InputParametersError); break; case -2: Interlocked.Increment(ref NoExists); break; case -3: Interlocked.Increment(ref IsDeleted); break; case -4: Interlocked.Increment(ref ExceptionError); break; default: Interlocked.Increment(ref Zero); break; } ; } var internalTs = internalStopWatch.Elapsed; externalStopWatch.Stop(); var externalTs = externalStopWatch.Elapsed; Console.WriteLine( "TaskId" + "\t" + Task.CurrentId + "\t" + "perfGetOneRecordVerifyResult" + "\t" + perfTestAttempts + "\t" + "InternalAPICallTotalTime" + "\t" + internalTs.ToString() + "\t" + "TestingTotalTime" + "\t" + externalTs.ToString() + "\t" + "GetSuccessfully" + "\t" + GetSuccessfully + "\t" + "ResultMatch" + "\t" + ResultMatch + "\t" + "ResultNotMatch" + "\t" + ResultNotMatch + "\t" + "InputParametersError" + "\t" + InputParametersError + "\t" + "NoExists" + "\t" + NoExists + "\t" + "IsDeleted" + "\t" + IsDeleted + "\t" + "ExceptionError" + "\t" + ExceptionError + "\t" + "Zero" + "\t" + Zero + "\t" ); }