コード例 #1
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;
            }
        }
コード例 #2
0
        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;
            }
        }
コード例 #3
0
        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;
            }
        }
コード例 #4
0
        public void LongKeyCASHashTableCheckAddInvildInput()
        {
            var hashTable = new CAS.LongKeyCASHashTable(arrayLength, keyByteArrayLength, contentByteArrayLength);

            string message = null;

            byte[] content = new byte[] { 0xff };

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

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

            try { hashTable.TrySet(message, content); }
            catch (ArgumentOutOfRangeException e)
            { message = e.Message; }
            Assert.IsTrue(message.Contains("Key length shouldn't longer than init value when you create hash table."));

            message = "";
            content = null;

            try { hashTable.TrySet(message, content); }
            catch (ArgumentNullException e)
            { message = e.Message; }
            Assert.IsTrue(message.Contains("value"));

            message = "";
            content = new byte[contentByteArrayLength + 1];

            try { hashTable.TrySet(message, content); }
            catch (ArgumentOutOfRangeException e)
            { message = e.Message; }
            Assert.IsTrue(message.Contains("Content length shouldn't longer than init value when you create hash table."));
        }
コード例 #5
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);
        }
コード例 #6
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;
            }
        }
コード例 #7
0
        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;
            }
        }
コード例 #8
0
        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"
                );
        }
コード例 #9
0
        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());
        }