Exemple #1
0
        public void LongKeyCASHashTableCheckDeleteNotFound()
        {
            var    hashTable = new CAS.LongKeyCASHashTable(arrayLength, keyByteArrayLength, contentByteArrayLength);
            string message   = "";

            Assert.AreEqual(hashTable.TryDelete(message), -2);
        }
Exemple #2
0
        public void LongKeyCASHashTableCheckDelete()
        {
            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;
            }
        }
Exemple #3
0
        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;
            }
        }
Exemple #4
0
        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);
        }
Exemple #5
0
        public void LongKeyCASHashTableInitialize()
        {
            var stopWatch = new Stopwatch();

            stopWatch.Start();

            var hashTable = new CAS.LongKeyCASHashTable(arrayLength, keyByteArrayLength, contentByteArrayLength);

            stopWatch.Stop();
            var ts = stopWatch.Elapsed;

            Console.WriteLine("Create hash table" + "\t" + ts.ToString());
        }
Exemple #6
0
        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);
        }
Exemple #7
0
        public void LongKeyCASHashTableCheckDeleteInvildInput()
        {
            var hashTable = new CAS.LongKeyCASHashTable(arrayLength, keyByteArrayLength, contentByteArrayLength);

            string message = null;

            try { hashTable.TryDelete(message); }
            catch (ArgumentNullException e)
            { message = e.Message; }
            Assert.IsTrue(message.Contains("key"));

            message = Encoding.UTF8.GetString(new byte[keyByteArrayLength + 1]);

            try { hashTable.TryDelete(message); }
            catch (ArgumentOutOfRangeException e)
            { message = e.Message; }
            Assert.IsTrue(message.Contains("Key length shouldn't longer than init value when you create hash table."));
        }
Exemple #8
0
        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 LongKeyperfUpdateOneRecordVerifyResult(ManualResetEvent mre, CAS.LongKeyCASHashTable hashTable, List <RecordString> record, int index)
        {
            mre.WaitOne();
            var u8 = Encoding.UTF8;

            int InputParametersError = 0;
            int ExceptionError       = 0;
            int AddSuccessfully      = 0;
            int UpdateSuccessfully   = 0;
            int Zero = 0;

            int result;

            var externalStopWatch = new Stopwatch();

            externalStopWatch.Start();

            var internalStopWatch = new Stopwatch();

            string key = record[index].linkId.ToString() + "-" + record[index].clcId.ToString() + "-" + record[index].sbp;
            string url = record[index].url;

            for (var i = 0; i < perfTestAttemptsForUpdate; i++)
            {
                internalStopWatch.Start();
                result = hashTable.TrySet(key, u8.GetBytes(url));
                internalStopWatch.Stop();

                switch (result)
                {
                case 0:
                    Interlocked.Increment(ref AddSuccessfully);
                    break;

                case 1:
                    Interlocked.Increment(ref UpdateSuccessfully);
                    break;

                case -1:
                    Interlocked.Increment(ref InputParametersError);
                    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" +
                "perfUpdateOneRecordVerifyResult" + "\t" + perfTestAttemptsForUpdate + "\t" +
                "InternalAPICallTotalTime" + "\t" + internalTs.ToString() + "\t" +
                "TestingTotalTime" + "\t" + externalTs.ToString() + "\t" +
                "AddSuccessfully" + "\t" + AddSuccessfully + "\t" +
                "UpdateSuccessfully" + "\t" + UpdateSuccessfully + "\t" +
                "InputParametersError" + "\t" + InputParametersError + "\t" +
                "ExceptionError" + "\t" + ExceptionError + "\t" +
                "Zero" + "\t" + Zero + "\t"
                );
        }
        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"
                );
        }
        public void LongKeyPerfTestingOneRecordVerifyResult()
        {
            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;

            byte[] content = null;

            stopWatch.Restart();
            foreach (var record in result.EmptyIfNull())
            {
                var key = record.linkId.ToString() + "-" + record.clcId.ToString() + "-" + record.sbp;
                content = u8.GetBytes(record.url);
                Assert.AreEqual(hashTable.TrySet(key, content), 0);
            }
            stopWatch.Stop();
            Console.WriteLine("Add all records" + "\t" + stopWatch.Elapsed.ToString());

            var mre = new ManualResetEvent(false);

            bool getEnabled    = true;
            bool deleteEnabled = false;
            bool updateEnabled = false;

            Task tGet1 = null, tGet2 = null, tGet3 = null, tGet4 = null, tGet5 = null, tGet6 = null, tGet7 = null, tGet8 = null, tGet9 = null, tGet10 = null;
            Task tDelete1 = null, tDelete2 = null, tDelete3 = null, tDelete4 = null, tDelete5 = null, tDelete6 = null, tDelete7 = null, tDelete8 = null, tDelete9 = null, tDelete10 = null;
            Task tUpdate1 = null, tUpdate2 = null, tUpdate3 = null, tUpdate4 = null, tUpdate5 = null, tUpdate6 = null, tUpdate7 = null, tUpdate8 = null, tUpdate9 = null, tUpdate10 = null;

            int totoalRecord = result.Count;
            var rndTemp      = new Random();
            int seed         = rndTemp.Next(totoalRecord);

            var rnd   = new Random(seed);
            var index = rnd.Next(totoalRecord);

            if (getEnabled)
            {
                tGet1 = Task.Run(() => { LongKeyperfGetOneRecordVerifyResult(mre, hashTable, result, index); });
            }

            if (updateEnabled)
            {
                tUpdate1 = Task.Run(() => { LongKeyperfUpdateOneRecordVerifyResult(mre, hashTable, result, index); });
            }

            if (deleteEnabled)
            {
                tDelete1 = Task.Run(() => { LongKeyperfDeleteOneRecordVerifyResult(mre, hashTable, result, index); });
            }


            if (getEnabled)
            {
                tGet2 = Task.Run(() => { LongKeyperfGetOneRecordVerifyResult(mre, hashTable, result, index); });
            }

            if (updateEnabled)
            {
                tUpdate2 = Task.Run(() => { LongKeyperfUpdateOneRecordVerifyResult(mre, hashTable, result, index); });
            }

            if (deleteEnabled)
            {
                tDelete2 = Task.Run(() => { LongKeyperfDeleteOneRecordVerifyResult(mre, hashTable, result, index); });
            }


            if (getEnabled)
            {
                tGet3 = Task.Run(() => { LongKeyperfGetOneRecordVerifyResult(mre, hashTable, result, index); });
            }

            if (updateEnabled)
            {
                tUpdate3 = Task.Run(() => { LongKeyperfUpdateOneRecordVerifyResult(mre, hashTable, result, index); });
            }

            if (deleteEnabled)
            {
                tDelete3 = Task.Run(() => { LongKeyperfDeleteOneRecordVerifyResult(mre, hashTable, result, index); });
            }


            if (getEnabled)
            {
                tGet4 = Task.Run(() => { LongKeyperfGetOneRecordVerifyResult(mre, hashTable, result, index); });
            }

            if (updateEnabled)
            {
                tUpdate4 = Task.Run(() => { LongKeyperfUpdateOneRecordVerifyResult(mre, hashTable, result, index); });
            }

            if (deleteEnabled)
            {
                tDelete4 = Task.Run(() => { LongKeyperfDeleteOneRecordVerifyResult(mre, hashTable, result, index); });
            }


            if (getEnabled)
            {
                tGet5 = Task.Run(() => { LongKeyperfGetOneRecordVerifyResult(mre, hashTable, result, index); });
            }

            if (updateEnabled)
            {
                tUpdate5 = Task.Run(() => { LongKeyperfUpdateOneRecordVerifyResult(mre, hashTable, result, index); });
            }

            if (deleteEnabled)
            {
                tDelete5 = Task.Run(() => { LongKeyperfDeleteOneRecordVerifyResult(mre, hashTable, result, index); });
            }


            if (getEnabled)
            {
                tGet6 = Task.Run(() => { LongKeyperfGetOneRecordVerifyResult(mre, hashTable, result, index); });
            }

            if (updateEnabled)
            {
                tUpdate6 = Task.Run(() => { LongKeyperfUpdateOneRecordVerifyResult(mre, hashTable, result, index); });
            }

            if (deleteEnabled)
            {
                tDelete6 = Task.Run(() => { LongKeyperfDeleteOneRecordVerifyResult(mre, hashTable, result, index); });
            }


            if (getEnabled)
            {
                tGet7 = Task.Run(() => { LongKeyperfGetOneRecordVerifyResult(mre, hashTable, result, index); });
            }

            if (updateEnabled)
            {
                tUpdate7 = Task.Run(() => { LongKeyperfUpdateOneRecordVerifyResult(mre, hashTable, result, index); });
            }

            if (deleteEnabled)
            {
                tDelete7 = Task.Run(() => { LongKeyperfDeleteOneRecordVerifyResult(mre, hashTable, result, index); });
            }


            if (getEnabled)
            {
                tGet8 = Task.Run(() => { LongKeyperfGetOneRecordVerifyResult(mre, hashTable, result, index); });
            }

            if (updateEnabled)
            {
                tUpdate8 = Task.Run(() => { LongKeyperfUpdateOneRecordVerifyResult(mre, hashTable, result, index); });
            }

            if (deleteEnabled)
            {
                tDelete8 = Task.Run(() => { LongKeyperfDeleteOneRecordVerifyResult(mre, hashTable, result, index); });
            }


            if (getEnabled)
            {
                tGet9 = Task.Run(() => { LongKeyperfGetOneRecordVerifyResult(mre, hashTable, result, index); });
            }

            if (updateEnabled)
            {
                tUpdate9 = Task.Run(() => { LongKeyperfUpdateOneRecordVerifyResult(mre, hashTable, result, index); });
            }

            if (deleteEnabled)
            {
                tDelete9 = Task.Run(() => { LongKeyperfDeleteOneRecordVerifyResult(mre, hashTable, result, index); });
            }


            if (getEnabled)
            {
                tGet10 = Task.Run(() => { LongKeyperfGetOneRecordVerifyResult(mre, hashTable, result, index); });
            }

            if (updateEnabled)
            {
                tUpdate10 = Task.Run(() => { LongKeyperfUpdateOneRecordVerifyResult(mre, hashTable, result, index); });
            }

            if (deleteEnabled)
            {
                tDelete10 = Task.Run(() => { LongKeyperfDeleteOneRecordVerifyResult(mre, hashTable, result, index); });
            }


            mre.Set();

            stopWatch.Restart();

            Task.WaitAll(
                tGet1, tGet2, tGet3, tGet4, tGet5, tGet6, tGet7, tGet8, tGet9, tGet10
                //, tDelete1, tDelete2, tDelete3, tDelete4, tDelete5, tDelete6, tDelete7, tDelete8, tDelete9, tDelete10
                //, tUpdate1, tUpdate2, tUpdate3, tUpdate4, tUpdate5, tUpdate6, tUpdate7, tUpdate8, tUpdate9, tUpdate10
                );

            stopWatch.Stop();
            ts = stopWatch.Elapsed;
            Console.WriteLine("Finish all tast in" + "\t" + ts.ToString());
        }
        private void LongKeyperfDeleteRandomVerifyResult(ManualResetEvent mre, CAS.LongKeyCASHashTable hashTable, List <RecordString> record)
        {
            mre.WaitOne();

            int totoalRecord = record.Count;
            var rndTemp      = new Random();
            int seed         = rndTemp.Next(totoalRecord);

            var rnd   = new Random(seed);
            int index = 0;

            int InputParametersError = 0;
            int NoExists             = 0;
            int IsDeleted            = 0;
            int ExceptionError       = 0;
            int DeleteSuccessfully   = 0;
            int Zero = 0;

            int result;

            var externalStopWatch = new Stopwatch();

            externalStopWatch.Start();

            var internalStopWatch = new Stopwatch();

            string key = null;

            for (var i = 0; i < perfTestAttemptsForDelete; i++)
            {
                index = rnd.Next(totoalRecord);
                key   = record[index].linkId.ToString() + "-" + record[index].clcId.ToString() + "-" + record[index].sbp;

                internalStopWatch.Start();
                result = hashTable.TryDelete(key);
                internalStopWatch.Stop();

                switch (result)
                {
                case 0:
                    Interlocked.Increment(ref DeleteSuccessfully);
                    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" +
                "perfDeleteRandomVerifyResult" + "\t" + perfTestAttemptsForDelete + "\t" +
                "InternalAPICallTotalTime" + "\t" + internalTs.ToString() + "\t" +
                "TestingTotalTime" + "\t" + externalTs.ToString() + "\t" +
                "DeleteSuccessfully" + "\t" + DeleteSuccessfully + "\t" +
                "InputParametersError" + "\t" + InputParametersError + "\t" +
                "NoExists" + "\t" + NoExists + "\t" +
                "IsDeleted" + "\t" + IsDeleted + "\t" +
                "ExceptionError" + "\t" + ExceptionError + "\t" +
                "Zero" + "\t" + Zero + "\t"
                );
        }